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.

I want to edit a part of an Apple Script to quit various applications at once:

tell application "TweetDeck"
    quit
end tell
tell application "Google Chrome"
    quit
end tell

Over all there are seven entries just like above.

Is there any way to write this more compact?

share|improve this question

3 Answers 3

up vote 10 down vote accepted

You can use a list of apps and a loop. Just add new apps to that first list and they'll be quit automatically.

set apps to {"Google Chrome", "Tweetbot", "ForkLift"}
repeat with thisApp in apps
    tell application thisApp to quit
end repeat
share|improve this answer
    
Yay, this is very much what I wanted to have! –  gentmatt Oct 24 '12 at 15:30

Unfortunately you cannot pass multiple applications to the 'tell application' call.

Rather, you could use a unix utility that can terminate multiple applications at once - killall.

You can invoke this utility from within an AppleScript:

do shell script "killall firefox Mail" - This would terminate FireFox and Mail

'killall' is case sensitive, so you must first determine the process names of the applications that you wish to kill.

  • Launch the applications that you will want to be terminating with the script
  • Use the following command (in a Terminal window) to find their full and correct names. (In this example, we are looking to find out tweetdeck's correct process name).

ps x | grep -i tweetdeck | grep -v grep

With TweetDeck running, this will give output similar to the following:

59127 ?? S 0:01.23 /Applications/TweetDeck.app/Contents/MacOS/TweetDeck -psn_0_21423213

The last part of the path is the process name as it should be passed to 'killall'. In this case TweetDeck (...Contents/MacOS/TweetDeck).

So, we go back to our AppleScript and add TweetDeck to the string of applications that we are terminating. In addition to my previous example, I'd do:

do shell script "killall firefox Mail TweetDeck"

Hope this helps!

share|improve this answer
    
Thanks for this detailed answer! I guess this trick could work just fine. Then I could also use a stronger command to make sure that applications are quit. –  gentmatt Oct 24 '12 at 14:13
1  
killall is such a command. So far in my testing it terminates and "takes no prisoners". You could also look into using kill -9 instead of killall. –  macaco Oct 24 '12 at 14:28
tell application "TweetDeck" to quit
tell application "Google Chrome" to quit

tell-end-tell blocks with only one command can be written in one line.

share|improve this answer

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.