Quit idle applications

OK, this is a tough one :wink:
My dad always forgets to quit apps after using them, so i want to make a script that will use the on idle handler to run every 15 minutes and quit idle apps (or ask user if he wants to quit them)

soā€¦

  1. is there even a way for applescript to get a list of running GUI apps?

  2. if there is, is there a way to check if the app has any window open?

thanks

Back in the OS9 days when process threading was lame, having a bunch of open apps chewed up CPU, but in OSX, itā€™s no problem. Plus the virtual memory page swapping is smart. So donā€™t worry about it! I donā€™t know what your dadā€™s using or how much RAM is installed, but I usually launch a whole bunch of apps at startup and leave them running. Like over a dozen of them. For weeks on end between restarts. No problem!

Howeverā€¦ you can get a list of open processes. Itā€™s in the dictionary for System Events. Then you can address each one to get a count of windows and quit if there are none. But only if the app is scriptable.

or you can use a unix shell script to kill the app. but that would kill background processes as well, which, in the end would force you to restart.

Hi LobsterMan,

Yes and yes.

Take a look at this script:

-- App Quitter
-- Application to check for applications with no open windows
-- Checks every 15 mins
-- Save as 'Stay Open' app
-- John Maisey 17 July 2005

on idle
	-- List of "safe" apps including name of this script
	set safeAppList to {"Finder", "App Quitter"}
	-- Get list of current non background apps
	tell application "System Events" to set theApps to name of processes whose background only is false
	
	-- Loop through list of current apps
	repeat with myApp in theApps
		-- Check to see of app is in "safe" list
		if myApp is not in safeAppList then
			-- Count open windows of the app
			try
				tell application myApp to set winCount to count (windows whose visible is true)
			on error
				set winCount to 0
			end try
			-- If there are no open windows
			if winCount is equal to 0 then
				display dialog "Would you like to quit \"" & myApp & "\"?" buttons {"Yes", "No"} default button 2
				-- Quit this app
				if the button returned of the result is "Yes" then tell application myApp to quit
			end if
		end if
	end repeat
	-- Every 15 mins in seconds
	return 900
end idle
--

Save it as a ā€˜Stay Openā€™ application called ā€œApp Quitterā€.

Best wishes

John M

On my 10.3.9 box, this line fails:

with ā€œonlyā€ highlighted.

Sorry, this may have been added in 10.4

Iā€™m running 10.4.2

John M

Browser: Safari 412
Operating System: Mac OS X (10.4)

thanks! :smiley: iā€™ll try it as soon as i get a chance (a bit busy nowā€¦)

it seems to mess up with some apps, like Graphic converter, i get an error message that the variable winCount isnā€™t defined, oh well, maybe iā€™ll just put it in a try blockā€¦

Hi Lobster or whoever started this thread,

If you want to run womething periodically, then itā€™s better to use the crontab. If you run a script with an idlw handler, then itā€™s always running. On the other hand, if you run a script with crontab it will only run at a certain time.

gl,

Fair enough. Is there code that works in both 10.3 and 10.4 that does the same, or nearly the same, check? All I can see in the Panther S-E scripting dictionary that vaguely resembles ā€œbackground onlyā€ is ā€œvisible,ā€ but no doubt something like cupsd could be called ā€œnot visibleā€ and thus be vulnerable to an unwanted ā€œkillā€ from the Quitter script.

Silversleeves

As far as finding the open apps, these are equivalent on my machine in 10.4.8 (producing identical lists of 10 apps), and the ā€œvisible is trueā€ version ought to work in 10.3.


tell application "System Events"
	set A1 to name of processes whose visible is true and name is not "Finder" and name is not "SomeThingElse"
	set A2 to name of processes whose background only is false and name is not "Finder" and name is not "SomeThingElse"
end tell

A1 works in both 10.3 and 10.4, A2 doesnā€™t.

I guess Iā€™d prefer something a little friendlier:


tell application "System Events" to set A1 to name of processes whose visible is true and name is not "Finder" and name is not "anythingElseToExclude"

set Killed to {}

set K to choose from list A1 OK button name "Leave Running" with prompt "Select applications you'd like to leave running and quit all others)" with title "Clean up Applications" with multiple selections allowed
if K is not false then -- not cancel button
	repeat with p in A1
		if contents of p is not in K then tell application p to quit with saving
	end repeat
end if