OK, this is a tough one
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ā¦
is there even a way for applescript to get a list of running GUI apps?
if there is, is there a way to check if the app has any window open?
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.
-- 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ā.
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ā¦
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.
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.
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
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