Welcome, Daring Fireballers!
What I find interesting is that we can even have multiple ways to accomplish things like this. Not sure how the Leading Brand affords it users this kind of power . . . .
Daring Fireball: How to Determine if a Certain App Is Running Using AppleScript and Perl:
But so how do you test if an application is currently running?
There’s More Than One Way to Do It: Gruber suggests this method.
white:~ paul$ time osascript -e ‘tell app “System Events” to count processes whose name is “Finder”’
1real 0m5.296s
user 0m0.524s
sys 0m0.380s
But it takes anywhere from 1 to 5 seconds: spinning up AppleScript to call an application is expensive.
This method takes a little less time:
white:~ paul$ time ps auxwww | grep -v grep | grep -c Finder
1real 0m0.217s
user 0m0.022s
sys 0m0.075s
And it could be even more concise if you didn’t care about the count: Gruber’s initial question is “is an application running?” but he is actually doing more than that by returning a count. That could be a limitation/feature of AppleScript.
If you were married to AppleScript or somehow need this to be a double-clickable item, you could make a script file that wraps the icky terminal commands:
on run
set theCommand to “ps auxwww | grep -v grep | grep -c Finder”
do shell script theCommand
end run
It’s a nice compromise: slower than the command line but faster than a pure AppleScript solution.
white:~ paul$ time osascript test.scpt
1real 0m0.717s
user 0m0.310s
sys 0m0.226s