PDA

View Full Version : Apps Running


NovaScotian
08-05-2009, 09:58 AM
Command-Tab presents a view of the GUI apps that are currently running
Is there an way to get a list of the apps that appear there in an AppleScript or shell script?

biovizier
08-05-2009, 11:05 AM
I don't know if it will get them all, but how about something like this for starters...tell application "System Events"
-- visible apps
set theList to name of processes whose visible is true

-- normally visible but "hidden" apps that still appear in Cmd-Tab
set notvisibles to name of processes whose visible is false

repeat with currentProcess in notvisibles
set itsproperties to properties of process currentProcess
if (background only) of itsproperties is false then
set theList to theList & currentProcess
end if
end repeat
end tell

set result to theList

Edit: This may end up giving the same result...
tell application "System Events" to set theList to name of processes whose background only is false

NovaScotian
08-05-2009, 11:55 AM
Thanks for the response Biovizier. After playing with it for a few minutes, I realized that I had not asked the question intelligently (completely off the mark, in fact).

I have a JavaScript called "Readability (http://lab.arc90.com/experiments/readability/)"** that produces a configurable large page of the main event on a web page with surprising accuracy. I like to use it reading newspaper articles and NetNewsWire feeds, and it works with either. Because I'm a keyboard command fan I often run scripts from Quicksilver and I'd like the script to target either Safari or NetNewswire, whichever has the frontmost window (the one I want to read). I haven't found a clean way to do that.
_________
** Readability comes as a link to be dragged to the Bookmarks Bar of a browser, but having done that, you can copy the JavaScript from the bookmark for use in an AppleScript like this (with either Safari or NetNewsWire in the tell): tell application "NetNewsWire" to tell document 1 to do JavaScript "......"

biovizier
08-05-2009, 05:11 PM
I'd like the script to target either Safari or NetNewswire, whichever has the frontmost window (the one I want to read).

Hmm, that sounds like a tough one... I see other properties that can be tested for processes, such as "frontmost" or "focused" (this one seems to be "missing value" for everything) that might work for the active app, but unless one or the other is actually the active application, I can't think of a way to determine which one is closer to the front (i.e. whose icon is closer to the left in Cmd-Tab) either...

NovaScotian
08-05-2009, 06:51 PM
I eventually came up with this, Biovizier:

set JS to "javascript:(function(){readStyle='style-novel';readSize='size-large';readMargin='margin-wide';_readability_script=document.createElement('SCRIPT');_readability_script.type='text/javascript';_readability_script.src='http://lab.arc90.com/experiments/readability/js/readability.js?x='+(Math.random());document.getElementsByTagName('head')[0].appendChild(_readability_script);_readability_css=document.createElement('LINK');_readability_css.rel='stylesheet';_rea dability_css.href='http://lab.arc90.com/experiments/readability/css/readability.css';_readability_css.type='text/css';_readability_css.media='screen';document.getElementsByTagName('head')[0].appendChild(_readability_css);_readability_print_css=document.createElement('LINK');_readability_print_css.rel='stylesh eet';_readability_print_css.href='http://lab.arc90.com/experiments/readability/css/readability-print.css';_readability_print_css.media='print';_readability_print_css.type='text/css';document.getElementsByTagName('head')[0].appendChild(_readability_print_css);})();"

tell application "System Events" to set last_app to item 1 of (get name of processes whose frontmost is true)
if last_app is "Safari" then
tell document 1 of application "Safari" to do JavaScript JS
else if last_app is "NetNewsWire" then
tell document 1 of application "NetNewsWire" to do JavaScript JS
else
beep 3
end if

"tell document 1 of application last_app to do JavaScript ..." wouldn't compile for some reason.

tw
08-05-2009, 11:52 PM
just as a matter of sanity, you can write Nova's script as follows:
set JS to "readStyle='style-novel';
readSize='size-large';
readMargin='margin-wide';
_readability_script=document.createElement('SCRIPT');
_readability_script.type='text/javascript';
_readability_script.src='http://lab.arc90.com/experiments/readability/js/readability.js?x='+(Math.random());
document.getElementsByTagName('head')[0].appendChild(_readability_script);
_readability_css=document.createElement('LINK');
_readability_css.rel='stylesheet';
_readability_css.href='http://lab.arc90.com/experiments/readability/css/readability.css';
_readability_css.type='text/css';
_readability_css.media='screen';
document.getElementsByTagName('head')[0].appendChild(_readability_css);
_readability_print_css=document.createElement('LINK');
_readability_print_css.rel='stylesh eet';
_readability_print_css.href='http://lab.arc90.com/experiments/readability/css/readability-print.css';
_readability_print_css.media='print';
_readability_print_css.type='text/css';
document.getElementsByTagName('head')[0].appendChild(_readability_print_css);"

tell application "System Events" to set last_app to item 1 of (get name of processes whose frontmost is true)

if last_app is "Safari" then

tell document 1 of application "Safari" to do JavaScript JS

else if last_app is "NetNewsWire" then

tell document 1 of application "NetNewsWire" to do JavaScript JS

else

beep 3

end if
setting it inside an anonymous function is not necessary, and Safari will parse the line breaks perfectly well. NetNewsWire I can't speak to, however - might need the anonymous function there.

NovaScotian
08-06-2009, 08:40 AM
Perfectly acceptable to NetNewsWire (as I suspected). I've made the change in my script to enhance its readability :-)

Thanks