Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

My wife and I are set up on a single laptop and I have our iTunes libraries stored on an external HD. I'm writing an applescript to auto eject that drive when we want to take the laptop elsewhere (we normally use iPads around the house), but I'm having trouble getting the script set up to also quit iTunes for both of our accounts when run.

Here's the basic script right now which is obviously missing the magic to quit iTunes for all logged in users:

tell the application "iTunes" to quit

tell the application "Finder"
    if disk Media exists then
        eject disk Media
    end if
end tell

Any suggestions?

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted

The problem is that your Applescript runs in the context of the current user and can't access the iTunes running in other accounts. One way to close/terminate all running iTunes processes would be (in Terminal)

sudo killall -sm iTunes

but this may leave the iTunes meta data in an inconsistant state (which means longer startup time the next time you start iTunes and potential data loss).

share|improve this answer
    
What could cause the metadata issues? Is that something that could happen if iTunes is sitting open but otherwise idle? –  pettifoggist Jun 12 '12 at 12:42
    
In theory yes, in practice I haven't seen this for quite some time now (and I tend to kill iTunes about once a week). –  patrix Jun 12 '12 at 12:43
add comment

Use the do shell script directive in AppleScript:

do shell script "sudo killall iTunes"

tell the application "Finder"
    if disk Media exists then
        eject disk Media
    end if
end tell

You'll need to make sure that you can execute sudo killall with no password.

share|improve this answer
    
sudo -u other-user -e osascript doesn't work because the Apple Event to quit iTunes is still send to user currently logged in. –  patrix Jun 12 '12 at 6:21
    
Ah, that's right. Forgot about that. Fixed. –  bahamat Jun 12 '12 at 16:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.