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

applescript

I have the following workflow that I need to execute on a daily basis for a period of days:

automation workflow

The computers start at a specified time, Mathematica and a specific Mathematica notebook launch and evaluate. Then I need to quit Mathematica programmatically and let the relevant schedules shut down the computers. I've worked out everything here except the highlighted step to quit Mathematica with an Applescript application.

To start with I created a simple application using the
Applescript Editor:

tell application "Mathematica" to quit

This will shut down Mathematica if the open notebook has been saved which the notebook does itself. Now I need to add a timer to the Applescript application.

An earlier question Automator workflow or AppleScript to close Safari after a period of time does something similar that I hoped to adapt to my needs. It shows the following code:

global quit_after, check_every

set quit_after to 2700
set check_every to 10
set minute to quit_after / 60

display dialog "Check is performed every " & check_every & " seconds. Things will be quit after " & minute & " minutesof system inactivity."

on reopen
    display dialog "Check is performed every " & check_every & " seconds. Things will be quit after " & minute & " minutes of system inactivity."
end reopen

on idle
    set idletime to do shell script "echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))"
    if (idletime as integer) > quit_after then
        tell application "System Events"
            if ((name of processes) contains "Safari") then
                tell application "Safari" to quit
            end if
        end tell
    end if
    return check_every
end idle

I don't need the dialog boxes and I want it to close Mathematica after say 15 minutes, so I tried this as a next step towards a solution.

global quit_after, check_every

set quit_after to 900
set check_every to 10
set minute to quit_after / 60

on idle
    set idletime to do shell script "echo $((`ioreg -c IOHIDSystem | sed -e '/HIDIdleTime/ !{ d' -e 't' -e '}' -e 's/.* = //g' -e 'q'` / 1000000000))"
    if (idletime as integer) > quit_after then
        tell application "System Events"
            if ((name of processes) contains "Mathematica") then
                tell application "Mathematica" to quit
            end if
        end tell
    end if
    return check_every
end idle

It doesn't work.

So I have two questions, can anyone suggest:

  • How to get my modified code to work?
  • How to alter it so it will shut down Mathematica at a specified time on weekdays only?

The latter would work better for me as I think I'd have fewer conflicts if I shut down Mathematica at a specific time rather than after a certain amount of time has passed.

Best Answer

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"