getting time an application has been running

I was trying to create a script to shut down Safari if it had been open for longer than a certain length of time, and got up to:

(if Safari has been running more than 12 hours)

set variable1 to display dialog “Safari must quit!” buttons {“yes”, “don’t”} default button 2
if button returned of variable1 is “yes” then tell application “Safari” to quit

I’m just not sure how to translate the bit in brackets – the important bit – from English to AppleScript. Also, the script would have to run in the background, wouldn’t it, to monitor how long Safari had been running; so can I do that without it showing in the Dock?

I am not sure the exact bounds of your question but to add a couple of comments.

Often the solution to getting odd data about something is to use the unix command line by issuing a unix command from AppleScript. The command output normally displayed in the terminal is returned to AppleScript as the result. For example, the line

set x to do shell script “ls”

sets x to a string of the output of the ls command from your home folder,

the line

set x to the second word of (do shell script “ps -xco command,pid | grep Dock”)

sets x to a string representing the PID of the dock

I noticed that the ps command could return the time a process started so unless AppleScript has some other way of getting that information (it might) the ps command is a good option. You would have to programatically do some conversions and comparisons to the current time.

Making an Application run in the background without a menu bar or Dock icon is easy. Apple provides a switch. Save the script in Script Editor 2 as a Run Only Application Bundle (the Run Only thing is because you already know that as a monitoring app you are going to want to use an idle handler, it is not actually important to the background status). Right click on the app and select Show Package Contents. Go into Contents and edit the Info.plist file (using Property LIst Editor from the dev tools is easy) and add a new enty

LSBackgroundOnly Boolean Yes.

Thats it.

Note, in this state AppleScript apps can be hard to control. The terminal command

killall applet

will kill it in a pinch.

Hope this helps and is not already obvious to you.

[/list]

Here’s a script to get how long an app has been running and to check every 10 minutes adding your code to display the dialog to quit it if it has been running longer than the allowed time:

Save this as a stay open script and to make it a faceless background script, see Drop Script Backgrounder

Jon


[This script was automatically tagged for color coded syntax by Convert Script to Markup Code]

Thanks, both for the method of editing the plist directly and the script, not all of which I understand, but which works.