Help with a simple Applescript?

Discussion in 'Mac Programming' started by mcdj, Nov 13, 2011.

  1. mcdj macrumors 604

    mcdj

    Joined:
    Jul 10, 2007
    Location:
    NYC
    #1
    I have a NAS with our iTunes library on it. Sometimes the NAS goes offline. If I try to open iTunes without knowing that, I get the missing library warning, then I have to manually mount the NAS share. That gets old. It gets even older for my wife, who doesn't always remember her iTunes library is not on her computer.

    So I have set up the following Applescript…

    Code:
    tell application "Finder"
    	mount volume "afp://xxxx:xxxx@192.168.1.xxx/Stuff"
    end tell
    tell application "iTunes"
           activate
    end tell
    This works great. So I saved it as an app called "iTunes", gave it the actual iTunes icon and dragged it into my dock.

    That's when I found out OSX doesn't like 2 apps with the same name on the dock, even if one of them (the applescript app) isn't currently running. It mounts the share but won't load iTunes.

    So I renamed the script "iTunes Launch". Now it works fine, but I keep clicking the "iTunes Launch" app, thinking it's iTunes, when iTunes is already running.

    I'd rather not have yet another icon on my dock anyhow (and it will confuse the wife), so now I'm wondering if there's a way to make a script that will detect when iTunes is launched, somehow pause the launch to mount the afp share, then continue to load iTunes after the share is mounted, without having 2 icons on my dock.

    Make sense? Is it doable?

    Thanks!

    p.s. I somehow figured out how to set up autofs, thinking it would automatically mount my share when iTunes called for it, but that doesn't seem to happen, at least with Lion. I even tried setting it up in fstab….this doesn't work either.
     
  2. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #2
    Not without a 3rd party process watcher.
    OTOH, checking whether iTunes is running in your script would allow you to act more like the actual iTunes icon in the dock.
    Code:
    if application "iTunes" is running then
    	say " itunes is running" -- just for test purposes
    	activate
    else
    	tell application "Finder"
    		mount volume "afp://xxxx:xxxx@192.168.1.xxx/Stuff"
    	end tell
    	tell application "iTunes"
    		activate
    	end tell
    	beep 1 -- just for test purposes
    end if
    If it were me, I'd configure the script as Automator action, and assign it some command key, or run it under a Script menu like 'FastScripts'. Either way it's out of your dock, but still convenient to use.
     
  3. mcdj thread starter macrumors 604

    mcdj

    Joined:
    Jul 10, 2007
    Location:
    NYC
    #3
    Thanks for that. It's getting close. A keyboard command would be fine for me, but not my wife.

    Thinking about it, all I really need is for that script you made to actually just bring the iTunes window to the front, or maximize if minimized. It would also be cool if the icon didn't bounce on the dock…not sure if that's possible if all other icons are set to bounce by default.
     
  4. Partron22 macrumors 68000

    Partron22

    Joined:
    Apr 13, 2011
    Location:
    Yes
    #4
    Code:
    Tell application "iTunes" 
    	activate
    	-- Get the state
    	set viz to visible of front window
    	set min to minimized of front window
    	
    	-- Set the state
    	set visible of front window to true
    	set minimized of front window to false
    end tell
    return {"visible: ", viz, "minimized: ", min} -- display the initial state in Result
    The Applescript dictionary for iTunes seems rather out of date. Parts of it don't work anymore. Since there's a lot of security pressure on iTunes these days, I won't be too surprised if scripts like this one stop working the very next time Apple re-jiggers iTunes.
     
  5. mcdj thread starter macrumors 604

    mcdj

    Joined:
    Jul 10, 2007
    Location:
    NYC

Share This Page