How to bring application to front every 15 minutes on Mac OS X Lion?

Solution 1:

~/Library/LaunchAgents/com.stackoverflow.open.desktop.task.timer.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.stackoverflow.open.desktop.task.timer</string>
    <key>ProgramArguments</key>
    <array>
        <string>open</string>
        <string>-a</string>
        <string>Desktop Task Timer LE</string>
    </array>
    <key>StartInterval</key>
    <integer>900</integer> <!-- every 900 seconds -->
    <key>RunAtLoad</key> <!--run on the 0th second-->
    <true/>
</dict>
</plist>

launchctl load ~/Library/LaunchAgents/com.stackoverflow.open.desktop.task.timer.plist or log out and back in.

crontab -e

*/15 * * * * open -a "Desktop Task Timer LE"
  • You can use a different editor like EDITOR=nano crontab -e
  • crontab -l lists currently scheduled tasks
  • crontab /path/to/file updates the table from an external file

Solution 2:

I see Lri's answer and you have chosen it as your correct answer. I wanted to show you a different approach that would also handle the second "feature" you requested. This method uses launchd too but that runs an applescript. Here's the applescript. Notice that I checked it using TextEdit so just change that to your application name.

property lastRunDate : missing value

set currentDate to current date
if lastRunDate is missing value then set lastRunDate to currentDate
set idleTime to (do shell script "ioreg -c IOHIDSystem | awk '/HIDIdleTime/ {print $NF/1000000000; exit}'") as number

-- check if the computer has been idle for 5 minutes or if 15 minutes has passed
if idleTime is greater than or equal to (5 * 60) or (currentDate - lastRunDate) is greater than or equal to (15 * 60) then
    tell application "TextEdit" to activate
    set lastRunDate to currentDate
end if

Here's the launchd plist. Notice this runs every 5 minutes and the applescript does the checking for your 5 minute criteria or your 15 minute criteria. You can see from this that I called the applescript "FiveMinuteRunner.scpt" and placed it on my desktop. Good luck :)

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>StartInterval</key>
    <integer>300</integer>
    <key>ProgramArguments</key>
    <array>
        <string>osascript</string>
        <string>/Users/hmcshane/Desktop/FiveMinuteRunner.scpt</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>Label</key>
    <string>com.HAMSoftEnginnering.FiveMinuteRunner</string>
</dict>
</plist>