The macosxhints Forums

The macosxhints Forums (http://hintsforums.macworld.com/index.php)
-   Applications (http://hintsforums.macworld.com/forumdisplay.php?f=5)
-   -   AppleScript to quit application if idle (http://hintsforums.macworld.com/showthread.php?t=101536)

jeno 05-16-2009 03:15 AM

The application is Adobe Illustrator CS2 & CS3. The plugin is loaded once the application is launch.

tw 05-16-2009 04:25 AM

well, first draft. take this script, paste it into the script editor and save it as a stay-open application bundle. (you'll also want to play with the info.plist file in the package to give it a non-generic name and make it background only, but that can wait until after it's been debugged). give it permissions so that only admins can run or execute it, put it in an admin folder somewhere so no one messes with it, and run it at startup. it should sit in the background and check once a minute to see if illustrator is the active app, and if it's not the active app for ten minutes it should throw up an alert and quit Illustrator 30 seconds later.

I don't have illustrator to test it on, of course, so you'll have to play with it yourself.

code:
Code:

property ticsSinceActive : 0
property idleTime : 60

on idle
        tell application "System Events"
                if exists (some process whose name contains "Illustrator") then
                        set theIllustratorApp to some process whose name contains "Illustrator"
                else -- illustrator is not running
                        return
                end if
                if frontmost of theIllustratorApp is true then
                        set ticsSinceActive to 0
                else
                        set ticsSinceActive to ticsSinceActive + 1
                end if
                if ticsSinceActive is greater than 10 then
                        tell theIllustratorApp
                                activate
                                display alert "Shutting down Illustrator" message "Illustrator has been inactive for 10 minutes.  Please verify that you are still using this application, or the system will shut it down." as critical buttons {"Close it", "Keep it running"} default button 1 giving up after 30
                                if button returned of the result is "Close it" or gave up of the result is true then
                                        repeat 60 times
                                                if exists document 1 then
                                                        close document 1 saving no
                                                else
                                                        exit repeat
                                                end if
                                        end repeat
                                        quit
                                else
                                        set ticsSinceActive to 0
                                end if
                        end tell
                end if
        end tell
        return idleTime
end idle


tw 05-16-2009 03:21 PM

improvements. this version checks to see if it's been active in the last ten minutes, and also records the maximum and average cpu for the last ten minutes, and closes the app if either is less than 3%. you can play with the conditions and the %s to suit your tastes.

Code:

property ticsSinceActive : 0
property idleTime : 60 -- time to idle, in seconds
property timeLimit : 10 -- memory range, in minutes
property cpuUsage : {}

on idle
        set maxTicCount to timeLimit * 60 / idleTime
        tell application "System Events"
                if exists (some process whose name contains "Illustrator") then
                        -- get reference to the app
                        set theIllustratorApp to some process whose name contains "Illustrator"
                else
                        -- illustrator is not running
                        set ticsSinceActive to 0
                        set cpuUsage to {}
                        return
                end if
                set illustratorPID to unix id of theIllustratorApp
                -- check if illustrator is inactive
                if frontmost of theIllustratorApp is true then
                        set ticsSinceActive to 0
                else
                        set ticsSinceActive to ticsSinceActive + 1
                end if
                -- take a running snapshot of cpu usage
                set shellCmd to "ps -c -o %cpu='' -p " & illustratorPID
                set end of cpuUsage to (do shell script shellCmd) as number
                if (count of cpuUsage) is greater than maxTicCount then
                        -- trim off items more than 10 minutes old
                        set cpuUsage to items ((count of cpuUsage) - maxTicCount + 1) thru -1 of cpuUsage
                end if
                set maxCpu to 0
                set aveCpu to 0
                repeat with thisBit in cpuUsage
                        if thisBit > maxCpu then setmaxCpu to thisBit
                        set aveCpu to aveCpu + thisBit
                end repeat
                set aveCpu to aveCpu / (count of cpuUsage)
                if ticsSinceActive > maxTicCount or maxCpu < 3 or aveCpu < 3 then
                        tell theIllustratorApp
                                activate
                                display alert "Shutting down Illustrator" message "Illustrator has been inactive for 10 minutes.  Please verify that you are still using this application, or the system will shut it down." as critical buttons {"Close it", "Keep it running"} default button 1 giving up after 30
                                if button returned of the result is "Close it" or gave up of the result is true then
                                        repeat 60 times
                                                if exists document 1 then
                                                        close document 1 saving no
                                                else
                                                        exit repeat
                                                end if
                                        end repeat
                                        quit
                                else
                                        set ticsSinceActive to 0
                                end if
                        end tell
                end if
        end tell
        return idleTime
end idle


jeno 05-17-2009 10:43 PM

Thanks tw,

I'll have a try and see how it goes.

Cheers,
Jeno

tlarkin 05-17-2009 11:04 PM

Quote:

Originally Posted by jeno (Post 532996)
Maybe I explained it wrong from thee beginning. The main issue is that I am limited to a certain # of license software plug-in Apps. for Illustrator. Every computer has the software plug-in app installed, but I only have a finite # of licenses. When a user logs into the plug-in, does the task at hand, the plug-in remains active. That causes myself and my IT staff to walk around to all of the computers and see who is idle but still connected to thee plug-in. This is what I am looking for and to see if it is possible to write a script that sits in the background, instead of looking at each computer through Apple Remote desktop and see that way. Thanks

Are you running a license server? It sounds to me that is what you need. If you have a finite number of licensee and a larger number of users have the client machines use a license server. That way if too many licenses are in use, you can send network wide messages to have users quit those programs so others may use them.


All times are GMT -5. The time now is 02:06 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, 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.