Why aren't weekday scheduled applescripts working?

  Collisio-Adolebitque 14:48 13 Jul 2018

In an effort to improve my productivity and reduce distractions (after listening to 'Deep Work' by Cal Newport on Audible), I decided to automatically launch and close email and IM apps on my Mac at certain times of day, allowing me to focus on productive work the rest of the time. I cobbled together 2 apple-scripts that work fine from the Terminal:

Script 1: /Users/$USER/Documents/dev/applescripts/opencomms_apps.scpt

# Open messaging & comms apps.
tell application "Microsoft Outlook.app"
    if not (exists window 1) then activate
end tell
tell application "Slack.app"
    if not (exists window 2) then launch
end tell
tell application "Skype.app"
    launch
end tell

Script 2: /Users/$USER/Documents/dev/applescripts/closecomms_apps.scpt

# Close messaging & comms apps.
tell application "Microsoft Outlook.app"
    if (exists window 1) then quit
end tell
tell application "Slack.app"
    quit
end tell
tell application "Skype.app"
    quit
end tell

NB: I had already assigned Outlook, Slack and Skype to their own desktops.

What I wanted to do was open them 3 times a day on weekdays only, so I created the following plist files for launchd.

Scheduled task 1: /Users/$USER/Library/LaunchAgents/com.open.comms.apps.plist

    Disabled

    Label
    com.open.comms.apps
    KeepAlive

    RunAtLoad

    Program
    /usr/bin/osascript
    ProgramArguments

        osascript
        /Users/$USER/Documents/dev/apple_scripts/open_comms_apps.scpt

    StartCalendarInterval


            Weekday
            1
            Hour
            9
            Minute
            0


            Weekday
            1
            Hour
            13
            Minute
            0


            Weekday
            1
            Hour
            16
            Minute
            30


            Weekday
            2
            Hour
            9
            Minute
            0


            Weekday
            2
            Hour
            13
            Minute
            0


            Weekday
            2
            Hour
            16
            Minute
            30


            Weekday
            3
            Hour
            9
            Minute
            0


            Weekday
            3
            Hour
            13
            Minute
            0


            Weekday
            3
            Hour
            16
            Minute
            30


            Weekday
            4
            Hour
            9
            Minute
            0


            Weekday
            4
            Hour
            13
  Collisio-Adolebitque 15:00 13 Jul 2018

Forum appears to cut off most of the thread and removed the plist tags. In a nutshell, the scheduled tasks are't working.

    Disabled

    Label
    com.open.comms.apps
    KeepAlive

    RunAtLoad

    Program
    /usr/bin/osascript
    ProgramArguments

        osascript
        /Users/$USER/Documents/dev/apple_scripts/open_comms_apps.scpt

    StartCalendarInterval


            Weekday
            1
            Hour
            9
            Minute
            0


            Weekday
            1
            Hour
            13
            Minute
            0


            Weekday
            1
            Hour
            16
            Minute
            30


            Weekday
            2
            Hour
            9
            Minute
            0


            Weekday
            2
            Hour
            13
            Minute
            0


            Weekday
            2
            Hour
            16
            Minute
            30


            Weekday
            3
            Hour
            9
            Minute
            0


            Weekday
            3
            Hour
            13
            Minute
            0


            Weekday
            3
            Hour
            16
            Minute
            30


            Weekday
            4
            Hour
            9
            Minute
            0


            Weekday
            4
            Hour
            13
            Minute
            0


            Weekday
            4
            Hour
            16
            Minute
            30


            Weekday
            5
            Hour
            9
            Minute
            0


            Weekday
            5
            Hour
            13
            Minute
            0


            Weekday
            5
            Hour
            16
            Minute
            30

Scheduled task 2: /Users/$USER/Library/LaunchAgents/com.close.comms.apps.plist

  Collisio-Adolebitque 15:00 13 Jul 2018

NB: I tried this with hard-coded username in the file paths and the $USER system variable.

I amended the permissions on the scripts and loaded the plist files via terminal as follows:

chmod 755 /Users/$USER/Documents/dev/apple_scripts/open_comms_apps.scpt
chmod 755 /Users/$USER/Documents/dev/apple_scripts/close_comms_apps.scpt
launchctl load -w /Users/$USER/Library/LaunchAgents/com.open.comms.apps.plist
launchctl load -w /Users/$USER/Library/LaunchAgents/com.close.comms.apps.plist

As many might expect, this hasn't worked and when I try to start the scheduled tasks manually:

launchctl start /Users/$USER/Library/LaunchAgents/com.open.comms.apps.plist
launchctl start /Users/$USER/Library/LaunchAgents/com.close.comms.apps.plist

Nothing happens.

Can anyone out there (with more than my <1 days experience with apple-script and launchd), please advise how to get this to work?

What is Markdown?

Markdown lets you add more formatting to your post. Simply type in your post and it will display as written.

If you wish to add bold or italic characters, add a hyperlink to another website, a heading or a horizontal line, simply use the relevent icons above the text input field.

A preview of your post will appear in the grey box below. If you make a change and you're not happy, simply press the back arrow icon to undo.

Post a Reply

4500

Elsewhere on IDG sites

Google Home Hub review

50 Cent is Iron Man in these comic book cover reissues of hip hop classics

How to watch Apple’s October 2018 iPad launch

Mac : comment forcer la fermeture d’une application ?