• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is app running?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is app running?


  • Subject: Re: Is app running?
  • From: kai <email@hidden>
  • Date: Fri, 6 May 2005 17:12:23 +0100


On Friday, May 6, 2005, at 12:44 am, Brennan wrote:

Should be simple. I need to find out whether a specific app (in this case
QuickTime player) is running. At the moment I am using:


tell application "Finder"
  (application processes whose name contains "QuickTime Player") is {}
end tell

This works, but is there a way to do this without using the Finder? (And
preferably without launching any other apps).


Maybe it's an old OS9 habit dying hard (where calls to the Finder had
significant performance overheads).

I am part of the way there with a shell script. I have some small working
chunks, but am a bit stuck piecing them together, for example:


do shell script "top -l1 | grep 'QuickTime*' | cut -f2 -d' '"

This returns pid if Quicktime Player is running, or "" if not, but may
return false positives. (i.e. Other users' copies of the app, or other
apps that start with the word 'QuickTime').

I may be barking up the wrong tree.

Is there another solution that I have overwoofed?

Whichever method you use, Brennan, an external call of some sort is required - be it to Finder, System Events or Shell.


Your reservations about using the Finder for this job are probably valid, since its dictionary shows process, etc. in its legacy suite. These items are now available via the processes suite of System Events, clearly the preferred route for the future. In fact, Finder appears to take up to 33% longer to execute equivalent statements here (and it wouldn't particularly surprise me if they were passed on to System Events anyway).

Given this, the most obvious (but by no means fastest) way to determine the presence of a given process via System Events is probably something like <process "xyz" exists>. (I generally use either this - or, where speed is more critical, the <"xyz" is in name of processes> form.)

Turning to the shell, I wouldn't have expected your 'top' approach to be the fastest or the most reliable shell call, since it also gathers a whole bunch of system usage statistics - rather than mainly process names. The construct also truncates process names longer than 10 characters. In addition, each time the target application is launched here, your script returns a different value (eg: "2422", "2481", "2506") - indicating the current Unix process id (PID). So, as it currently stands, you'd need to do something like count the resulting text to make it in any way useful. Emmanuel's suggested use of 'ps' is more direct - not to mention way faster (by about 8-10 times here).

To give you a better idea of relative performance, I've compared several approaches, starting with your Shell and Finder methods - each adjusted where necessary to return the same boolean value:

-------------
1: shell/top:
-------------

(count (do shell script "top -l1 | grep 'QuickTime*' | cut -f2 -d' '")) > 0

--------------------
2: fndr/compareList:
--------------------

tell application "Finder"
(application processes whose name contains "QuickTime Player") is not {}
end tell


----------------
3: sysEv/exists:
----------------

tell application "System Events"
    application process "QuickTime Player" exists
end tell

------------
4: shell/ps:
------------

"QuickTime Player" is in (do shell script "ps -xwwc")

------------------
5: sysEv/isInList:
------------------

tell application "System Events"
    "QuickTime Player" is in name of application processes
end tell

------------------

Each script was run 1,000 (10 x 100) times and the averaged results expressed as an index: 100 = fastest overall:

test: index - method
--------------------

1: 968 - shell/top

2: 212 - fndr/compareList

3: 165 - sysEv/exists

4: 113 - shell/ps

5: 100 - sysEv/isInList

--------------------

---
kai

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Is app running? (From: Brennan <email@hidden>)

  • Prev by Date: Re: Script as internet helper protocol in tiger
  • Next by Date: Re: move all files also with no extension via shell command
  • Previous by thread: Re: Is app running?
  • Next by thread: Script as internet helper protocol in tiger
  • Index(es):
    • Date
    • Thread