How to find out if an application is running
|
|
Thread rating:  |
Ted Zeng - 12 Oct 2005 22:37 GMT Hi,
I would like to find out if an application like Illustrator is running On OS X or not from a perl script. How can I do it?
"ps" doesn't list the processes like Illustrator.
Ted Zeng Adobe Systems
Jeff Lowrey - 12 Oct 2005 22:38 GMT I'd think with your email address, you should know which process Illustrator runs as, or at least be able to find out... ;-)
Use AppleScript, from osascript or from Mac::Glue or etc.
You won't find a single, cross platform method for answering this question. Ergo, if you need the "same" piece of code to run on multiple OSes, you will need to code to detect which platform it's running on, and then DTRT on that platform.
-Jeff Lowrey
>Hi, > [quoted text clipped - 5 lines] >Ted Zeng >Adobe Systems John Delacour - 12 Oct 2005 23:09 GMT >I would like to find out if an application like Illustrator is running >On OS X or not from a perl script. How can I do it? #!/usr/bin/perl @processes = split /, /, `osascript -e ' tell app "system events" to name of processes whose visible is true'`; print join $/, @processes
...if you don't mind waiting a little.
JD
Sherm Pendley - 12 Oct 2005 23:25 GMT > I would like to find out if an application like Illustrator is running > On OS X or not from a perl script. How can I do it? > > "ps" doesn't list the processes like Illustrator. "px x" does. ;-)
#!/usr/bin/perl
use strict; use warnings;
my $ps = (grep /Adobe Illustrator/, split("\n", `ps x`))[0]; my ($pid, $tt, $status, $time, $cmd) = split(" ", $ps, 5);
print <<EOF; PID = $pid TT = $tt STATUS = $status TIME = $time CMD = $cmd EOF
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net Hire me! My resume: http://www.dot-app.org
Chris Nandor - 13 Oct 2005 00:01 GMT > "ps" doesn't list the processes like Illustrator. Yeah, like Sherm said.
These are the two most common methods that I've used, along with a sort-of port of JD's AppleScript to Mac::Glue.
#!perl -wl # simple print 1 if grep 'Photoshop', `ps auxw`;
# heh use Mac::Glue ':all'; my $syse = new Mac::Glue 'System Events'; print 1 if $syse->obj(processes => whose( creator_type => contains => '8BIM' # can't see way to do bundle ID? ))->get;
# my favorite use Mac::Apps::Launch; print IsRunning('com.adobe.photoshop');
The latter is almost surely your best solution. No calls to external apps, is included with Tiger, and doesn't rely on possibly conflicting paths and app names.
It uses Mac::Processes to loop over running applications, and is probably as efficient in that regard as the other methods, since they all basically have to do the same thing too. But that it doesn't have to call out to another app makes it that much more efficient.
It can also accept a four-char code, in this case, IsRunning('8BIM'), like the Mac::Glue code does.
For the Mac::Apps::Launch code to work, you need version 1.90; 1.91 is included in Tiger, so you should be fine if you're using that. Older versions of the module can still handle the four-char code syntax.
 Signature Chris Nandor pudge@pobox.com http://pudge.net/ Open Source Technology Group pudge@ostg.com http://ostg.com/
Chris Nandor - 13 Oct 2005 00:24 GMT Oops, typo. This:
> print 1 if grep 'Photoshop', `ps auxw`; should be:
> print 1 if grep /Photoshop/, `ps auxw`; And I forgot to mention -- just because it may be useful -- you can also convert between PID and PSN. Also in Mac::Processes, there are GetProcessPID() and GetProcessForPID(). The former converts a PSN to a PID, and the latter a PID to a PSN.
Much of the Mac:: API can accomodate either if necessary (Mac::Glue can target by PSN or PID, and a "process" object from System Events can give you a PSN or PID), but if you need to convert from one or the other, you can use this API.
One example I like:
use Mac::Processes; while (my($psn, $psi) = each %Process) { kill 15, GetProcessPID($psn) if $psi->processName =~ /Photoshop/i; }
 Signature Chris Nandor pudge@pobox.com http://pudge.net/ Open Source Technology Group pudge@ostg.com http://ostg.com/
Ted Zeng - 13 Oct 2005 00:47 GMT Chris, I don't receive your earlier post. I will check out your solution. Thanks.
Thanks for everyone who has responded. Both Solutions (use AppleScript or just "ps x") work Well.
BTW, Apple just reported yesterday that Mac units shipped increased By 48% last quarter. Sound like Mac is gaining market share.
Ted zeng
> Oops, typo. This: > [quoted text clipped - 20 lines] > kill 15, GetProcessPID($psn) if $psi->processName =~ /Photoshop/i; > } Sherm Pendley - 13 Oct 2005 01:06 GMT > BTW, Apple just reported yesterday that Mac units shipped increased > By 48% last quarter. Sound like Mac is gaining market share. Yeah - and AAPL immediately dropped 10%. I'll *never* understand Wall Street.
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net Hire me! My resume: http://www.dot-app.org
Joel Rees - 13 Oct 2005 02:26 GMT >> BTW, Apple just reported yesterday that Mac units shipped increased >> By 48% last quarter. Sound like Mac is gaining market share. > > Yeah - and AAPL immediately dropped 10%. I'll *never* understand > Wall Street. People who make money out of money have strange superstitions. Good news is bad news. (Or, if you want to play with conspiracy theories, ... )
I suspect what's happening is that the expected drop before the shift to intel is not happening. Rather than seeing the steady sales as a confirmation of Mac OS X, they are worried that Steve might decide to keep both CPU lines, messing up their misguided plans to capitalize on the next big monopoly.
(My delusions, of course, do not necessarily reflect the views of my employer.)
Joel Rees <rees@ddcom.co.jp> digitcom, inc. Kobe, Japan +81-78-672-8800 ** <http://www.ddcom.co.jp> **
Dominic Dunlop - 13 Oct 2005 07:51 GMT > I would like to find out if an application like Illustrator is running > On OS X or not from a perl script. How can I do it? > > "ps" doesn't list the processes like Illustrator. A late arrival which hasn't been mentioned so far:
$ killall -0 Illustrator 2>/dev/null && echo "Illustrator is running"
Broadly, killall sends a signal to any process with a name matching its final command-line parameter. (See the man page for chapter and verse.) Here we're sending signal zero instead of the default SIGKILL. Signal zero isn't really a signal at all: it acts as a "could I send a signal if I wanted to?" query. If a signal could be sent -- that is, if Illustrator is running -- killall exits with status zero (success), so the next command in the pipeline gets run; otherwise with one (failure), terminating the pipeline. The 2>/dev/ null junks killall's diagnostic in the event that Illustrator isn't running.
 Signature Dominic Dunlop
David Cantrell - 13 Oct 2005 11:40 GMT > A late arrival which hasn't been mentioned so far: > $ killall -0 Illustrator 2>/dev/null && echo "Illustrator is running" killall is a Really Bad Idea. While it does indeed do what you intend on OS X, on other commercial Unices like Solaris it really does kill all. That is, it sends your chosen signal to all processes. Not good. So don't get in to the habit of using it.
 Signature David Cantrell | Benevolent Dictator Of The World
Eye have a spelling chequer / It came with my pea sea It planely marques four my revue / Miss Steaks eye kin knot sea. Eye strike a quay and type a word / And weight for it to say Weather eye am wrong oar write / It shows me strait a weigh.
Dominic Dunlop - 13 Oct 2005 15:55 GMT >> A late arrival which hasn't been mentioned so far: >> $ killall -0 Illustrator 2>/dev/null && echo "Illustrator is running" [quoted text clipped - 4 lines] > good. > So don't get in to the habit of using it. Aw, cut me some slack. This was specifically a Mac OS X query (Illustrator not being available on Solaris); the Solaris command only does its dangerous stuff for root (according to the man page -- I don't have access to a Sun box); and Sun puts it in a place that shouldn't be on a normal user's PATH. Plus killall, BSD-style, is a neat command. Works on Linux too. But anyway. Here's Yet Another Way To Do It:
#!/usr/bin/perl -wl
use Proc::ProcessTable; my $t = new Proc::ProcessTable; for (@{$t->table}) { if ($_->cmndline =~ m%/Mail.app/%) { # *** Your app name here *** print $_->pid; last; } }
Proc::ProcessTable is available from CPAN, and builds and runs without problem on Mac OS X. It also works in a lot of other environments (including Windows) (and Solaris).
Sadly, this approach no good for use from an optionally installed package, as you can't rely on the module being present. (Unless you were to include it as part of the bundle and tell perl how to find it, which would probably be more trouble than it's worth.)
 Signature Dominic Dunlop
Ted Zeng - 13 Oct 2005 18:36 GMT As it turns out, only Sherm's version works For all the situations.
Here is what I did: I clicked the menu "File:New" and Illustrator presents An "New document" dialog.
Now, I go to execute the commands to kill it.
The AppleScript version could not kill it. "kill" does kill it.(Sherm's code)
Killall doesn't seem to do anything even in the normal sitution (I use "Adobe Illustrator" instead of Illustrator, otherwise it will complaint there is no matching process)
I haven't tried Chris's version yet.
Ted zeng
>>> A late arrival which hasn't been mentioned so far: >>> $ killall -0 Illustrator 2>/dev/null && echo "Illustrator is running" [quoted text clipped - 32 lines] > were to include it as part of the bundle and tell perl how to find > it, which would probably be more trouble than it's worth.) Jeff Lowrey - 14 Oct 2005 00:05 GMT >As it turns out, only Sherm's version works >For all the situations. Actually, merely for the situation that you didn't tell us you were aiming for.
You asked "How to find out if an application is running".
You didn't ask "How do I find out a PID so I can kill a process".
-jeff
Ted Zeng - 14 Oct 2005 04:02 GMT You are right.
After I got the info. On how to detect the process, I added the codes to kill them. Then I thought they all work. Didn't think what I had asked.
Here is the fun part: I made a killapp.pl script and Call it to kill Bridge with `killapp.pl Bridge `
And it kills itself.
As it turned out, the script finds itself from the list because the Command line includes Bridge(or "Adobe Illustrator" for Illustrator).
Now, I put the app. Name in two part, `killapp.pl Brid ge` And merge them in the killapp.pl script.
It works. But somebody probably laughs at it.
Ted zeng
>> As it turns out, only Sherm's version works >> For all the situations. [quoted text clipped - 7 lines] > > -jeff Peter N Lewis - 14 Oct 2005 06:59 GMT >Here is the fun part: I made a killapp.pl script and >Call it to kill Bridge with [quoted text clipped - 4 lines] >As it turned out, the script finds itself from the list because the >Command line includes Bridge(or "Adobe Illustrator" for Illustrator). The standard trick for this, if the search is a regular expression, is to surround one of the letters with [], ie:
killapp.pl '[B]ridge'
>It works. But somebody probably laughs at it. It's hardly the first time, indeed the general "find this application" of
ps auxw | grep xxxx
inevitably finds the search command unless you use a trick like the above, or:
ps auxw | grep xxxx | grep -v grep
Enjoy, Peter.
 Signature <http://www.stairways.com/> <http://download.stairways.com/>
Jeremy Mates - 14 Oct 2005 17:06 GMT * Peter N Lewis <peter@stairways.com.au>
> ps auxw | grep xxxx > > inevitably finds the search command unless you use a trick like the above, > or: > > ps auxw | grep xxxx | grep -v grep Or, utterly off topic, the clever regex method, which may run afoul of quoting rules in certain shells, but is quicker to type:
ps auwwx | grep xxx[x]
I recommend ww on OS X, due to the long command names. More shell tips:
http://sial.org/howto/shell/
Leering back towards Perl, I often use psgrep.
Ted Zeng - 14 Oct 2005 17:35 GMT I like these solution.
Thanks.
ted
>> Here is the fun part: I made a killapp.pl script and >> Call it to kill Bridge with [quoted text clipped - 22 lines] > Enjoy, > Peter. Chris Nandor - 14 Oct 2005 21:22 GMT I just uploaded Mac::Apps::Launch. Now IsRunning() returns the PSN, instead of simple true/false (1/0).
Here's a fun, simple, and efficient script to kill the Dock (which should relaunch immediately):
use Mac::Apps::Launch 1.92; use Mac::Processes; use POSIX 'SIGTERM';
my $psn = IsRunning('com.apple.dock'); kill SIGTERM, GetProcessPID($psn);
 Signature Chris Nandor pudge@pobox.com http://pudge.net/ Open Source Technology Group pudge@ostg.com http://ostg.com/
James Reynolds - 26 Oct 2005 00:13 GMT >I just uploaded Mac::Apps::Launch. Now IsRunning() returns the PSN, instead >of simple true/false (1/0). [quoted text clipped - 8 lines] > my $psn = IsRunning('com.apple.dock'); > kill SIGTERM, GetProcessPID($psn); Don't try that with Launch 1.91!
--
Thanks,
James Reynolds University of Utah Student Computing Labs james@scl.utah.edu 801-585-9811
Joseph Alotta - 26 Oct 2005 00:16 GMT why not?
>> I just uploaded Mac::Apps::Launch. Now IsRunning() returns the >> PSN, instead [quoted text clipped - 22 lines] > james@scl.utah.edu > 801-585-9811 James Reynolds - 26 Oct 2005 00:24 GMT Sorry...
It killed everything running as me. I was dumb enough to try it on my main computer.
James
>why not? > [quoted text clipped - 22 lines] >>james@scl.utah.edu >>801-585-9811 Sherm Pendley - 14 Oct 2005 01:07 GMT > Here is what I did: > I clicked the menu "File:New" and Illustrator presents [quoted text clipped - 3 lines] > > The AppleScript version could not kill it. Makes sense. The AppleScript would presumably (I don't have it in front of me) work by sending an Apple Event. Having a modal dialog open probably jams up the processing of such events.
sherm--
Cocoa programming in Perl: http://camelbones.sourceforge.net Hire me! My resume: http://www.dot-app.org
Joseph Alotta - 14 Oct 2005 01:28 GMT Greetings,
I know this is off topic, but does anyone know how to set the default user-agent in Safari to MSIE 6.0 permanently? I have the debug menu but that doesn't work in websites that open a new window.
Joe.
Dominic Dunlop - 14 Oct 2005 07:46 GMT > As it turns out, only Sherm's version works > For all the situations. Great! Go for it.
The tough bit about there being more than one way to do it is to resist the temptation to try them all, even after you've found one that works. I can't always resist the temptation...
 Signature Dominic Dunlop
|
|
|