2007-04-20

AppleScript: Check to see if an app is running

AppleScript

As I was writing an AppleScript to control Skype and Adium yesterday, I wanted to make sure that I didn't start those applications if they weren't already running.

There seem to be a number of ways in AppleScript to check if an application is already running, and they all center around using the "

System Events
" method.

Here's the one that I chose:
tell
application
"System Events"
if
(
count
(
every
process
whose
name
is
"Adium")) > 0
then

tell
application
"Adium"
set
my status type
to
away
set
my status message
to
"I'm on the phone..."
end
tell

end
if

if
(
count
(
every
process
whose
name
is
"Skype")) > 0
then

tell
application
"Skype"
send
command
"SET PROFILE MOOD_TEXT I'm on the phone..."

script name
"IM On Phone"

--

--
Uncomment the following if you'd like to take Skype off-line
--

--
send
command
"SET USERSTATUS OFFLINE"
script name
"IM On Phone"

end
tell

end
if

end
tell

Read my previous blog entry for a description of the scripts that I wrote.
They are also available for download.

Thanks to sparrow from Walled Networks for pointing me in the right direction.

0 comments: