I’m trying to write a simple script that returns true when the mail application is running else it returns false.
I really have no clue how can this be done…
Can anyone help? Your help would be greatly appreciated since this is an urgent matter.
I love this “unix minimalism” but unfortunatedly I don’t know enough to use it myself
But I think plain AS is easy enough
tell application "System Events"
set runningProcesses to name of every process
if "Mail" is in runningProcesses then
-- do something
else
-- do something else
end if
end tell
sorry for the unix stuff - but Applescript just frustrates me sometimes, I spend an hour trying to figure out something that takes 2 minutes in the shell…
I did at least put it into a “do shell script” and properly escape the quotes for Applescriptability though… so that counts right? haha
agreed. here is an AppleScript that leverages your code:
set myVar to "Remote Desktop is not running"
try
set myVar to (do shell script "ps -awwx | grep -v grep | grep \"Remote Desktop.app\"")
end try
if myVar is "Remote Desktop is not running" then
display dialog myVar
else
display dialog "Remote Desktop is running"
end if
a lot of times i do something like the above. use AppleScript as a wrapper–it makes for a good looking, practical program that i can send out to a user.
set myVar to "iCal is not running"
try
set myVar to (do shell script "ps -awwx | grep -v grep | grep \"/iCal.app\"")
end try
if myVar is "iCal is not running" then
display dialog myVar
else
display dialog "iCal is running"
end if
i hadn’t thought about apps that have a similar name and a space in them. this is kind of a kludge, but i think it will work (can’t test, i don’t have that app).
set myVar to "iCal is not running"
try
set myVar to (do shell script "ps -awwx | grep -v grep | grep \"/iCal \"")
end try
if myVar is "iCal is not running" then
display dialog myVar
else
display dialog "iCal is running"
end if
capJ,
how about this for iTunes:
set myVar to "iTunes is not running"
try
set myVar to (do shell script "ps -awwx | grep -v grep | grep -w \"iTunes \"")
end try
if myVar is "iTunes is not running" then
display dialog myVar
else
display dialog "iTunes is running"
end if
love it. how do you show all the processes from System Events? is there anywhere to read up on System Events? i see people post about them from time to time, but as you can probably tell, i’m coming at this from a different angle.
Hi waltr. While you won’t get some of the more obscure processes from System Events that you might from ps, you’ll get the ones that we’re usually interested in. Try this:
tell application "System Events" to name of processes
In that case, j, here’s another little morsel to digest.
With System Events (or Finder), you can also perform some neat filtering tricks to isolate processes with (or without) certain properties. Here are just a few examples:
tell application "System Events" to tell processes to {|not type "APPL"|:name whose file type is not "APPL", |is scriptable|:name whose has scripting terminology is true, |name starts with "s"|:name where name starts with "s", |frontmost|:name where it is frontmost, |background only|:name where it is background only, |visible|:name where it is visible, |not visible and contains "gin"|:name where it is not visible and name contains "gin"}
Ciao Kai,
this sort of “piping” is very interesting. I’ve looked around the web to find some more information on it but without success. Is it documented somewhere?
My apologies, waltr. I neglected to answer this part of of your question.
To be perfectly honest, I don’t personally know of any detailed reference source about scripting System Events. (However, I should confess that I’m not very big on books or manuals, anyway.) The only on-line references, of which I’m aware, mention a little about GUI Scripting and, to an even lesser degree, Folder Actions. (Even then, such references are made only in passing.) All the same, if anyone knows of any general material that might have escaped me in this area, please feel free to chip in…
Of the knowledge (such as it is) that I’ve managed to glean, most came initially from perusing System Events’ AppleScript dictionary - and has since been supplemented by a fair amount of evaluation, based almost entirely upon trial and error. (As you’re no doubt aware, extracting viable code from an AS dictionary usually requires a combination of both study and experimentation, much like by dredging a Unix Man page to derive a workable solution - although the latter is often more likely to contain at least one or two syntax examples… ;))
Ciao, chebfarid.
By coincidence, this very subject cropped up recently in the (long) discussion about how to sort OSX recent items by date (check out messages #7 & #11, particularly). There’s also a very brief mention in the venerable AppleScript Language Guide, under the subject heading Identifiers.