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-13-2009 03:10 AM

AppleScript to quit application if idle
 
Hello All,

I am very new to AppleScript and hope to get some advice. I want to write a AppleScript to quit an application if leaving it idle for 15 minutes. Please also note that the computer may not be in idle. Is it possible only to get idle time for application?

In live situation, it works something like this: I am working on my mac and I have a few applications open. I want the script to quit/force quit the application that I leave it for 15 minutes. Similarly I want to do the same with mounted network drive. I want to disconnect network drive if leaving them idle for 15 minutes.

Please help.

Thanks & Regards,
Jeno

benwiggy 05-13-2009 09:44 AM

Can I ask why you want to quit idle apps? What's your objective?

jeno 05-14-2009 12:09 AM

Over the time, I had try to educate the user at my company to always save their work close the application that they do not need to use anymore. Users always complaining that their workstation are slow even if they are using the best and fully loaded Mac Pro.

There are times where I am getting very frustrated and even want to slap them on their face. Now that it is proven that "nice education" doesn't work. I am going to write a script to force quit/disconnect any application and server. I have done a search on this forum and understand that this is not a good/healthy way, but user need to get educated, I doesn't really care if user lost their work and it doesn't do much damaged to the application itself. I need a way to forcing user to adopt a good habit of always saving job and close down the apps.

Thanks & Regards,
Jeno

fazstp 05-14-2009 12:22 AM

Sounds like a bad idea. If they lose work because you force-quit an application surely that is worse than a workstation being a bit slow? If the slowness of the workstations is due to poor work habits then I would say step up the education but force-quitting apps is going to create some bad workplace relations.

Sorry, I know you asked for a script not an opinion. Just my 2c.

hayne 05-14-2009 12:24 AM

Quote:

Originally Posted by jeno (Post 532954)
Users always complaining that their workstation are slow even if they are using the best and fully loaded Mac Pro.

And what evidence do you have that the slowness (real or apparent) is due to having extra apps running that aren't being used? (This doesn't seem like a likely reason for slowness.)

Quote:

user need to get educated, I doesn't really care if user lost their work and it doesn't do much damaged to the application itself. I need a way to forcing user to adopt a good habit of always saving job and close down the apps.
So in order to "educate" your users, you are going to make them lose some data (things they have worked on but not yet saved) ?!?

Not a good idea.
What exactly is your higher level goal? Are you trying to help the users? Or just stop them from complaining? Or just exert control because you think you should?

tw 05-14-2009 03:03 AM

there are much better options here than forcing users to quit their apps. if these are multi-user (public) workstations, redesign your system so that users have to log in and out (maybe with a timer that pops up after 10 minutes of system inactivity and logs them out if they don't respond). that will prevent users from leaving files open on the machine. If these are workstations for individuals, let them do what they want. write an FAQ about using computer resources efficiently, and point anyone who complains to that FAQ.

benwiggy 05-14-2009 03:03 AM

Quote:

Originally Posted by jeno (Post 532954)
Over the time, I had try to educate the user at my company to always save their work close the application that they do not need to use anymore. Users always complaining that their workstation are slow even if they are using the best and fully loaded Mac Pro.

If the Macs are fully loaded with RAM, then having idle apps is probably not the cause of the slowness.
Might it be something else? Network accounts?

jeno 05-14-2009 04:31 AM

I totally understand but apparently the bad user habit has causes the OS or application to crashed most of the time and it happen regularly. This is where I get idea of writing a script to "simulating" the "crash".

benwiggy 05-14-2009 04:53 AM

What is the bad habit that the users have? Leaving apps open is not a bad habit.
Even so, why would you want to simulate a crash: to help your users learn not to cause crashes....?:confused:

What do the crash logs indicate might be the cause of the crashes, both for the apps and the OS?

jeno 05-14-2009 05:14 AM

Education is definitely not going to work. Can anyone please advice me a better way to get this done?

Cheers,

jeno 05-14-2009 05:26 AM

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

tw 05-14-2009 09:02 AM

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

That's confusing. software licenses are for the installation, not the use: if you have 12 machines and 3 licenses that means you can install the app on 3 machines. installing it on all 12 is illegal, even if no one ever uses it.

at any rate, my advice would be the same: pick 'x' machines (one for each license that you own); install the app on those and remove it from the others, and then make it so people have to log in and log out to use those machines.

hayne 05-14-2009 09:41 AM

Quote:

Originally Posted by tw (Post 533010)
software licenses are for the installation, not the use

No - it is quite common to have "floating licences" where you can install the software on as many machines as you want, but only a specified number can use the software at a given time - usually controlled by a licence server on the network. It would seem that this is the situation jeno has.

hayne 05-14-2009 09:45 AM

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.

Indeed it would have been a good idea to have explained the real problem from the beginning. Now it is clear.

Since the problem seems to be related to one particular application, it seems that you could just monitor that one app (e.g. with a background script that looks at the CPU time used by that app) and pop up a dialog asking the user to quit the app if it hasn't been used for a while.

You can get info on CPU usage via the 'ps' command.

NovaScotian 05-14-2009 10:40 AM

Quote:

Originally Posted by hayne (Post 533018)
No - it is quite common to have "floating licences" where you can install the software on as many machines as you want, but only a specified number can use the software at a given time - usually controlled by a licence server on the network. It would seem that this is the situation jeno has.

This is precisely the way in which limited licenses for expensive image editing and CAD software were controlled at the university from which I'm retired. A network server controlled that and it did indeed monitor usage because students would often run a limited app just to save their access to it later. The server had a "Use It Or Lose It" script, too, that started with a 10-minute warning and ended with a shutdown of the software.

tw 05-14-2009 12:33 PM

Quote:

Originally Posted by hayne (Post 533018)
No - it is quite common to have "floating licences" where you can install the software on as many machines as you want, but only a specified number can use the software at a given time - usually controlled by a licence server on the network. It would seem that this is the situation jeno has.

well, what an odd way of doing things. if that's the way it works, though, then there's a couple of approaches to the problem:
  • have each machine run a script that checks to see how long an app has been idle and quit it if it's over a certain limit
  • have a central machine run a script over the network to see how many versions are running and force quit idle apps if there are more than the allotted share (and maybe disable machines from launching new instances if the limit is reached)
There's also the question of what the best way to check for idle processes is, particularly since the 'nix conception of idling is computer-centric - 20 seconds - while the human conception of idling is probably closer to 15 minutes. how would you get ps (or top, or come other process) to calculate how much action an app has gotten in the last 15 minutes? I'm not sure CPU use will cut it (microsoft Word, for instance, consistently uses 8-12% of my CPU, foreground or background). is there a way to check to see if the app is (or has been) the active app in the last X minutes?

NovaScotian 05-14-2009 01:56 PM

Back in the days I was talking about (now 7 years ago), Novell had server software for managing limited licenses in exactly the way you're describing. I don't know how the software measured "idle" time, but we didn't get complaints so it worked. Bear in mind, however, that the limited license software was not entirely on the user's machine, it was netware. I don't know the details, unfortunately (I was the Dean of the Faculty, not the IT guy).

jeno 05-14-2009 11:34 PM

Hello,

Any luck helping me getting this done?

NovaScotian 05-15-2009 09:20 AM

I can't believe there isn't software available to do it.

tw 05-15-2009 09:43 AM

Quote:

Originally Posted by jeno (Post 533143)
Any luck helping me getting this done?

Jeno, read my last post and answer the questions. and at least tell us what application you're talking about. is it illustrator itself you want to quit, or a particular plugin?

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.