Go Back   The macosxhints Forums > Working with OS X > OS X Developer



Reply
 
Thread Tools Rate Thread Display Modes
Old 08-05-2009, 09:58 AM   #1
NovaScotian
Hall of Famer
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 4,948
Apps Running

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?
__________________
17" MBP, OS X 10.8.3; 27" iMac, OS X 10.8.3
NovaScotian is offline   Reply With Quote
Old 08-05-2009, 11:05 AM   #2
biovizier
All Star
 
Join Date: May 2004
Location: london on ca
Posts: 930
I don't know if it will get them all, but how about something like this for starters...
Code:
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...
Code:
tell application "System Events" to set theList to name of processes whose background only is false

Last edited by biovizier; 08-05-2009 at 11:28 AM.
biovizier is offline   Reply With Quote
Old 08-05-2009, 11:55 AM   #3
NovaScotian
Hall of Famer
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 4,948
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"** 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 "......"
__________________
17" MBP, OS X 10.8.3; 27" iMac, OS X 10.8.3
NovaScotian is offline   Reply With Quote
Old 08-05-2009, 05:11 PM   #4
biovizier
All Star
 
Join Date: May 2004
Location: london on ca
Posts: 930
Quote:
Originally Posted by NovaScotian
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...
biovizier is offline   Reply With Quote
Old 08-05-2009, 06:51 PM   #5
NovaScotian
Hall of Famer
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 4,948
I eventually came up with this, Biovizier:

set JS to "javascriptfunction(){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.
__________________
17" MBP, OS X 10.8.3; 27" iMac, OS X 10.8.3
NovaScotian is offline   Reply With Quote
Old 08-05-2009, 11:52 PM   #6
tw
Hall of Famer
 
Join Date: Apr 2007
Posts: 4,262
just as a matter of sanity, you can write Nova's script as follows:
Code:
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.
__________________
Philosophy is a battle against the bewitchment of our intelligence by means of language. -LW-
tw is offline   Reply With Quote
Old 08-06-2009, 08:40 AM   #7
NovaScotian
Hall of Famer
 
Join Date: Oct 2002
Location: Halifax, Canada
Posts: 4,948
Perfectly acceptable to NetNewsWire (as I suspected). I've made the change in my script to enhance its readability :-)

Thanks
__________________
17" MBP, OS X 10.8.3; 27" iMac, OS X 10.8.3

Last edited by NovaScotian; 08-06-2009 at 08:42 AM.
NovaScotian is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 09:39 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.