AppleScript: complex script for WIFI login, getting time and launch screensaver

applescriptscreensaverscriptwifi

Experts!

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:

–SCRIPT 1

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

–SCRIPT 2

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

try

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

end try

Best Answer

WIP


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
    else
        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

3.

tell application "Safari"
    activate
    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)

1.

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

launchd

  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">
<dict>
    <key>Label</key>
    <string>com.mycompany.daemon</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/osascript</string>
        <string>/path/to/YourAppleScript.scpt</string>
    </array>
    <key>StartCalendarInterval</key>
    <dict>
        <key>Hour</key>
        <integer>9</integer>
        <key>Minute</key>
        <integer>0</integer>
    </dict>
</dict>
</plist>
  1. Press ^O (Control + O)

  2. Press Enter

5.

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