urgeently need a simple mail script

Hello guys,

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.

Regards,
Psycho-ed

Hi Ed,
you should take a look at the Processes Suite in the System Events dictionary.

Good scripting
Farid

I don’t know about you guys - but getting an active app using System Events has allways been confusing to me… I use

do shell script "ps -awwx | grep -v grep | grep  \"Remote Desktop.app\""

Just seems simpler to me…

I love this “unix minimalism” but unfortunatedly I don’t know enough to use it myself :smiley:
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

Ciao
Farid

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…:wink: 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.

Amen to that, most of the work I do utilizes do shell script.

I’ve read the man page, but I couldn’t work this out.

How do you get an exact match?

set myVar to (do shell script "ps -awwx | grep -v  grep | grep   \"iCal\"")

I have MenuCalendarClock iCal.app, so waltr’s script returns “iCal is Running” - but it isn’t. The same goes for iTunes and iTunes Helper.

Thanks,

j

hi J,

does this work for you?

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).

Personally, I’d opt for this (faster) approach:

tell application "System Events" to process "Mail" exists

hi jaques,

how about this:

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

You might try this variation, j (although I’ll still stick with System Events, thanks very much ;)):

"iCal" & return is in (do shell script "ps -xco command") & return

hi kai,

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

Thanks for all the replies everybody.

I just finished dinner, and now I have a bunch of posts to read.

j

In my case, it doesn’t - nothing scheduled. I get

[b]“The command exited with a non-zero status.”

[/b]but iTunesHelper is returned if I use

grep \"/iTunes.app\""

j

In that case, j, here’s another little morsel to digest. :wink:

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"}

Hi kai.

Very cool. I have to learn to use records.

I think every suggestion for solving the problem of name similatity worked. I’ve learned several useful things today - thanks everybody

j

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?

Farid

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. :slight_smile: