Applescript app to shut down Mathematica at a specific time of day

Solution 1:

Here's why it doesn't work.

HIDIdleTime is the number of seconds of inactivity, so unless the user do absolutely nothing during fifteen minutes, your script doesn't work.

Here's how to do this in AppleScript.

set timeToQuit to 50700 -- time in seconds =14h05
set currDate to current date

-- do nothing on Saturday and Sunday or this script is launched after 14h05
if weekday of currDate is in {Saturday, Sunday} or (time of currDate) > timeToQuit then return

delay (timeToQuit - (time of currDate)) -- wait 
tell application "System Events" to exists process "Mathematica"
if the result then quit application "Mathematica"

Solution 2:

Ok, well, here's how you would make launchd run at 2:05 p.m.

Copy this using a text editor and save it as ~/Library/LaunchAgents/com.tjluoma.quit-at-time.plist

<?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>Label</key>
    <string>com.tjluoma.quit-at-time</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/killMathematica.sh</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>14</integer>
        <key>Minute</key>
        <integer>5</integer>
    </dict>
</dict>
</plist>

Note that you'll need to logout/login or use launchctl load ~/Library/LaunchAgents/com.tjluoma.quit-at-time.plist

Now, the /usr/local/bin/killMathematica.sh line could point to whatever it is you want to run: an app, an AppleScript, or a shell script.

The easiest solution is a shell script:

#!/bin/zsh

APP='Mathematica'

PID=$(ps cx | fgrep "$APP" | awk '{print $1}')

    # if $APP isn't running, then just exit already
[[ "$PID" == "" ]] && exit 0

    # if we get here, then $APP is running
    # tell it to quit using AppleScript
    # if this exits properly, the script will exit too
osascript -e "tell application \"$APP\" to quit" && exit 0

    # if we get here, AppleScript didn't work, so let's try
    # sending the app a 'kill' message
kill -QUIT "$PID" && exit 0

exit 1

# EOF

save that as '/usr/local/bin/killMathematica.sh' (or wherever) and make it chmod 755

Test it out and see if it works.

Now that I've given you the answer to the question you asked

Here's a better solution: use Keyboard Maestro. It's much easier.

How easy?

http://images.luo.ma/Keyboard_Maestro_Quit_at_Specific_Time-20120717-195546.jpg

Boom. Done.

No launchd. No cron. No AppleScript.

(I had to use VLC instead of Mathematica because I don't have Mathematica installed.)

Solution 3:

The tl;dr Version

  1. Execute crontab -e in Terminal.
  2. Paste in 5 14 * * 1-5 osascript -e 'tell application "Mathematica" to quit' on a new line.
  3. Save.

The Detailed Version

I'm far from an Applescript expert, so I can't offer much feedback on why your script isn't working, but I can offer an alternate (and simpler) option.

Pretty much any UNIX-like system, including OS X has the cron daemon, which can run commands on a set schedule. OS X also has launchd, but that's a bit trickier to use, so we'll stick with cron.

The crontab format

Cron is just a plain text file that executes commands based on a simple structure.

  • Each line in the crontab is a separate job, in the form minute hour day-of-the-month month day-of-the-week command, with each field separated by a space.
    • * means any time, and you can put comma-separate multiple entries (1,3,5) or use a hyphen for a range (1-5).
    • Hours are on a 24-hour clock.
    • Note the distinction between day of the month and day of the week. Be careful setting both of these, you probably only want to use one or the other; i.e. if day of the month is 13 and day of the week is 5, your job will only run on Friday the 13th, not every Friday and every 13th of the month).
    • Days of the week go 0-7, with both 0 and 7 meaning Sunday.

Examples

0 15 * * 1-5 /usr/bin/blah would execute /usr/bin/blah at 3 PM every Monday to Friday on any calendar date of any month.

15 * 1 1-6 * /usr/bin/blah runs the same command at 15 minutes of every hour on the first day of January through June, regardless of what day of the week it is.

Your specific cron entry

Based on the schedule in your question, to quit Mathematica at 2:05 PM every weekday, the applicable line would be 5 14 * * 1-5 osascript -e 'tell application "Mathematica" to quit'

  • osascript runs Applescript from the command line, either from a file, or a one-liner you specify (as we're doing here).

Adding a cron job

  1. Open Terminal
    • Optional: if you're not comfortable with the vim editor, change the shell editor to one you're comfortable with (I suggest nano if you don't have a preference) by running the following command: export EDITOR=nano (change nano to your preferred editor). Note that you should probably stick to shell-based editors, GUI editors may not work with cron's peculiarities in saving the files.
  2. Type crontab -e and hit enter to open the crontab in the editor.
    • Each user has their own crontab, and the commands are executed with their permissions, so you should do this as whatever user is running Mathematica in the first place.
  3. On a new line (or the first line if there are no other entries), enter the command we determined earlier: 5 14 * * 1-5 osascript -e 'tell application "Mathematica" to quit'.
  4. Save the file (control-O in nano) and quit your editor.

That should do it. Obviously you'll want to test it out to be sure there aren't any formatting errors in your crontab, but that should be all you'll need to quit on a schedule.