PDA

View Full Version : Script to quit and launch iTunes




m021478
May 23, 2011, 12:48 PM
I am trying to use Chronosync to sync my iTunes library on one machine to my iTunes library on another machine, and Chronosync has an option to run a script before and after the synchronization occurs, which in my case would be great to run a script that quits the iTunes application prior to syncing it, then re-opens iTunes after syncing has completed.

Can someone please help create two applescripts... one to quit iTunes, and the second to start iTunes.

Thanks!

For reference, here is the ChronoSync pre/post synchronization script interface:

http://dl.dropbox.com/u/54787/Screenshots/img_20110523_014239.png



robbieduncan
May 23, 2011, 01:25 PM
tell application "iTunes"
quit
end tell

tell application "iTunes"
activate
end tell

In all honesty I can't believe Google could not have told you that...

m021478
May 23, 2011, 01:41 PM
Thanks! That was probably brainlessly easy for you :)

I have a bit of a tougher one that perhaps you might be able to give me a hand with...

If I am using ChronoSync to mirror my iTunes library from on MacPro to the iTunes library on my iMac (which is also on the same network and has a static IP address), any thoughts on whether or not a single applescript launched from the MacPro ~ which will be the one initiating the chronosync synchronization ~ would be able to quit iTunes running on both machines, then reopen iTunes on both machines after the sync is complete?

To clarify... the script you helped me out with will do the trick for quitting the itunes library of the mac pro, but if itunes us running on my iMac at the time the sync occurs, then I'm still likely to run into issues with the sync procedure.

Any suggestions would be greatly appreciated... Thanks!

robbieduncan
May 23, 2011, 01:43 PM
Not easily. If you open System Preferences and look in the Sharing preferences you'll see something called "Remote Apple Events". I suspect this could be used to bridge AppleScript across machines but I've never used this.

The easier option would be to find something that can do this already and has been built for you…

Edit: OK. This actually looks possible!

Take a look at http://dougscripts.com/itunes/itinfo/remcontrol.php

If we change the first script to:

set remMachine to "eppc://username:userpass@remote_ip"

using terms from application "iTunes"
try
tell application "iTunes" of machine remMachine
quit
end tell

on error errM number errN
display dialog (errM & space & errN)
end try
end using terms from

tell application "iTunes"
quit
end tell


Then this will connect to IP remote_ip as username with password userpass and tell iTunes on that machine to quit. The it'll tell the local iTunes to quit. Obviously you replace username, userpadd and remote_ip with the correct values. And turn on Remote Apple Events on both machines (or maybe only the remote one?)

m021478
May 23, 2011, 02:18 PM
the script you suggested worked perfectly to quit iTunes on both machines, but when I tried it in reverse to re-open itunes on both machines (the post-synchronization script) it failed to launch itunes on the remote machine and it gave me the following error message:
http://dl.dropbox.com/u/54787/Screenshots/img_20110523_031323.png

Doug from Doug's iTunes scripts does say that following at the beginning of the page you referenced:
"The iTunes application on the remote machine must already be activated in order for an AppleScript on the local machine to control it. I couldn't get it to activate via a remote script."

It would be great if someone could help me figure this one out because at this point I'm so close to pulling off the impossible that it'd be tragic to fail now :confused:

Thanks for your help! Look forward to seeing if anyone can help figure this one out.

robbieduncan
May 23, 2011, 02:36 PM
Try this to launch iTunes on the remote machine (found in 2 seconds using Google)

tell application "Finder" of machine "eppc://username:password@powermac.local" to open ":Applications:iTunes"

m021478
May 23, 2011, 02:45 PM
I'm having a difficult time authenticating when using the script to remotely quit itunes. It says I can use my username (which is my first and last name with a space in between), or I can use my Rendezvous name (somename.local)... I am randomly getting authentication failed messages when I try running the applescript. A couple times after running the script, it gave me a popup dialog asking me to enter my username and apassword and log and it gave me the option to save the login info to my keychain which I did, but a subsequent attempt at running the script has failed...

Not really sure what I am supposed to do here to get it to connect to the remote computer consistently.

Any suggestions would be greatly appreciated... Thanks!

m021478
May 23, 2011, 02:50 PM
assuming my username is "FirstName LastName", I've tried configuring the script as follows:

set remMachine to "eppc://FirstName LastName:password@10.0.1.??"

using terms from application "iTunes"
try
tell application "iTunes" of machine remMachine
quit
end tell

on error errM number errN
display dialog (errM & space & errN)
end try
end using terms from

tell application "iTunes"
quit
end tell


assuming my Rendezvous name is "Office (Computer)" which in rendezvous speak gets translated into "Office-Computer.local", I've also tried configuring the script as follows:

set remMachine to "eppc://Office-Computer.local:password@10.0.1.??"

using terms from application "iTunes"
try
tell application "iTunes" of machine remMachine
quit
end tell

on error errM number errN
display dialog (errM & space & errN)
end try
end using terms from

tell application "iTunes"
quit
end tell

Neither approach works… what am I doing wrong here? Thanks!

EDIT: Ignore the happy face in the code above... that's meant to be a ':' followed by a 'P' which got inadvertently translated into an emoticon :)

robbieduncan
May 23, 2011, 02:53 PM
Usernames with spaces are probably an issue. Try using your shortname. For example my username is Robert Duncan but my shortname is robbie. Or try using %20 instead of a space which might work.

Also the Rendezvous name of the computer is nothing to do with "you": it's the machine name, not a username. It's not even remotely similar. It might work in place of the IP address but you can't log in using that as a username.

Edit: instead of using quote blocks use code blocks:

:p :) :D

m021478
May 23, 2011, 02:58 PM
Try this to launch iTunes on the remote machine (found in 2 seconds using Google)

tell application "Finder" of machine "eppc://username:password@powermac.local" to open ":Applications:iTunes"

I tried this and it gives me the following error:

http://dl.dropbox.com/u/54787/Screenshots/img_20110523_035744.png

robbieduncan
May 23, 2011, 03:54 PM
I tried this and it gives me the following error:

Image (http://dl.dropbox.com/u/54787/Screenshots/img_20110523_035744.png)

Try

do shell script "open /Applications/iTunes.app/Contents/MacOS/iTunes"


This works on my local machine. It leaves a terminal window hanging around but I can't avoid that.

kainjow
May 23, 2011, 03:54 PM
Maybe try tell application "Finder" of machine "eppc://username:password@powermac.local" to open POSIX file "/Applications/iTunes.app"

m021478
May 23, 2011, 04:04 PM
that worked! You pulled off the impossible! The cycle is complete. Now ChronoSync can quit iTunes on both machines, mirror the library from the Mac Pro to the iMac, then relaunch the libraries on both machines once the process is complete. PERFECT!

You rock! Thanks so much for your help!

Just for the record for anyone who stumbles onto this thread in the future -

...here is the script that closes iTunes on both machines:

set remMachine to "eppc://username:password@powermac.local"

using terms from application "iTunes"
try
tell application "iTunes" of machine remMachine
quit
end tell

on error errM number errN
display dialog (errM & space & errN)
end try
end using terms from

tell application "iTunes"
quit
end tell

... and here is the code that opens iTunes back up again on both machines:

tell application "Finder" of machine "eppc://username:password@powermac.local" to open POSIX file "/Applications/iTunes.app"

tell application "iTunes"
activate
end tell

Be sure to enable 'Remote Apple Events' in the 'Sharing' pane of the System Preferences

Thanks again for all your help with this one!

bonimac
Oct 24, 2013, 12:58 AM
Amazing guy's, it worked for me from the first time!
thank you so much (from the future) :-)

never expected how to find out to do this from my iMac to Mac mini media server.
i'm so happy, thanks thanks thanks!
:D