I'm new to apple script and struggling with its complexity...But maybe someone can help. The thing I want to do my iMac:

The iMac is used to work as a HighEnd slideshow for an artist work. Its extremely important that the iMac gets the correct time for displaying the slideshow.

So what the iMac needs to do:

  • iMac wakes up every morning at 8 AM and gets to sleep at 11 PM (done by Energysaver)

  • iMac toggles Wifi on and off (see script1 below)

  • Starting Safari (script 2)
  • iMac finds the correct Wifi and logs in (missing)
  • Login Page of Wifi needs a "click in checkbox" to accept terms of use (SCRIPT 3 I tried to adapt a script found on the net, but doesn't really work)
  • iMac waits until a dedicated time (9 AM) and launches screensaver. (SCRIPT 4 is launching, but waiting until specific time is missing)

Any helps & suggestions would be very much appreciated!! Andreas

What I have:


set status to do shell script "networksetup -getairportpower en1"

if status ends with "On" then

    do shell script "networksetup -setairportpower en1 off; sleep 2"

end if

set status to do shell script "networksetup -getairportpower en1"

if status ends with "Off" then

    do shell script "networksetup -setairportpower en1 on"

end if


tell application "Safari" activate

end tell

--SCRIPT 3 clicking the box to accept terms of use

to clickID(theId) 

    tell application "Safari"

        do JavaScript "document.getElementById('" & theId & "').click();" in document 1 

    end tell 

end clickID

--SCRIPT 4 is needed to get the time, wait and launch screensaver

set ntpdPID to do shell script "pgrep ntpd; exit 0"

if ntpdPID is not "" then

    do shell script "systemsetup -setusingnetworktime On" with administrator privileges

end if


    tell application id "com.apple.Screensaver.engine" to launch

end try
  • We can not help with script 3 unless we have the source code of the page. Please edit your question to include relevant sections of the html.
    – JBis
    Aug 7 '18 at 14:09
  • 1
    From a personal perspective, any digital signage project I did, thing I found most useful was not connecting the machine to the same network that everyone else was using - especially not a (free) public network. This did two things: 1) ensured that the machine had connectivity and 2) prevented unauthorized access. This is something you should investigate further; specifically, look at using VLANs and (where applicable) different SSIDs
    – Allan
    Aug 7 '18 at 15:17
  • THANKS TO ALL FOR YOUR VALUABLE HELP!!! I am really impressed for getting so much help from you guys! I will now check and post the code of the website - will take a few days because I will be on a business trip....
    – Andreas
    Aug 8 '18 at 11:37
  • @Allan +1 Very good point. I would probably suggest using a WPA2 secured network for the machine. It would be easier to script. While allowing the captive portal one for guests.
    – JBis
    Aug 9 '18 at 11:12
  • @Allen & JBis Thanks for this hint! I will check if I can install a secured network for the machine. Additionally it seems that the public network I wanted to use is not reliable enough to ensure proper connection...
    – Andreas
    Aug 11 '18 at 11:25


Script 1

repeat 2 times
    set status to do shell script "networksetup -getairportpower en0"
    if status ends with "On" then
        do shell script "networksetup -setairportpower en1 off"
        delay 2
        do shell script "networksetup -setairportpower en1 on"
    end if
end repeat

Script 2

tell application "Safari" to activate

Script 2.5

Use @CJK code to connect. Then use Script 3 to accept terms and conditions.

Script 3

  1. Safari > Preferences > Advanced > Show Develop menu in menu bar

  2. Develop > Allow JavaScript from Apple Events


tell application "Safari"
    make new document with properties {URL: "http://captive.apple.com"}
    if not (exists document 1) then reopen
    tell current tab of window 1 to set URL to "https://stackoverflow.com/questions/24500011/how-to-wait-for-webpage-to-fully-load-before-proceeding-script"
    set the_state to missing value
    repeat until the_state is "complete"
        set the_state to (do JavaScript "document.readyState" in document 1)
        delay 0.2
    end repeat
    set theId to "WillBeAddedOnceCodeIsAddedInQuestion"
    do JavaScript "document.getElementById('" & theId & "').click();" in document 1
end tell

Script 4

If the script is working then do the following:

Cron Method (Depreciated)


export VISUAL=nano; crontab -e

2. Copy and paste the following

0 9 * * * osascript /path/to/YourAppleScript.scpt

3. Press ^O (Control + O)

  1. Press Enter

Note: Cron will not execute if the computer is asleep


  1. sudo nano /Library/LaunchDaemons/com.mycompany.plist

  2. Copy and paste the following:

<?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">
  1. Press ^O (Control + O)

  2. Press Enter


 sudo launchctl load -w /Library/LaunchDaemons/com.mycompany.plist

  • 1
    As CJK points out, launchd is probably preferable over cron, the latter of which is considered deprecated on macOS. StartCalendarInterval would probably be the key to use here.
    – aaplmath
    Aug 7 '18 at 14:08
  • Although deprecated, it does work and is a bit more simple especially for simple things like this. If it was anything more complicated I would use launchd. Anyway I will edit question to include that option.
    – JBis
    Aug 7 '18 at 14:11
  • Lots of Users have reported issues with getting Allow JavaScript from Apple Events on the Develop menu of Safari to work. For me at first in macOS High Sierra, it just wouldn't toggle no matter what I tried as suggested in countless posts. Then it toggled on, and a bit later I toggled it off and it would not work further. Weeks later I was able to get the check mark to show, but not going to turn it off for now, as I need it. BTW It will read much better if you put some space around > when you use them, e.g. Safari > Preferences > Advanced> Show Develop menu in menu bar, etc. Aug 7 '18 at 15:12
  • open location is not a Safari command and shouldn't be used inside a Safari tell block (or, rather, it's superfluous doing so). open location opens a URL in the system's default browser, which may be Chrome. To open a URL in Safari, do tell app "Safari" to make new document with properties {URL: "http://example.com"}.
    – CJK
    Aug 9 '18 at 2:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .