r/applescript • • Jan 22 '22

AppleScript for clocking in?

Hey all! I have a current apple script that I run at the start of my shift thats set to verbally remind me to clock in to work at X intervals which works very well IF I dont have a random system restart / crash, but the issue is when I do and I re-run the script, it doesnt recognize the later "clock" time.

Current script:

display dialog "Starting shift reminders!"

set gIsThisTime1 to false

repeat until gIsThisTime1 is true

delay 1

set myTime1 to time string of (current date)

if myTime1 > "11:29:00 am" and myTime1 < "11:35:00 am" then

set gIsThisTime1 to true

end if

end repeat

set myDate1 to date string of (current date)

set myDateTime1 to myDate1 & " - " & myTime1

say "Clock in for shift"

tell application "Safari"

make new document with properties {URL:"https://google.ca"}

set the bounds of the front window to {769,25,3199,1740}

set keyword to "User Name"

repeat

set myWindow to current tab of first window

activate

do JavaScript "window.location.reload()" in myWindow

repeat while (do JavaScript "document.readyState" in document 1) is not "complete"

delay 0.5

end repeat

set pageContent to do JavaScript ("window.document.documentElement.outerHTML") in myWindow

if pageContent contains keyword then

exit repeat

end if

end repeat

end tell

tell application "System Events"

set textToType to "sull024"

keystroke textToType

key code 48

end tell

tell application "System Events"

set textToType to "password123"

keystroke textToType

key code 76

end tell

set gIsThisTime1 to false

repeat until gIsThisTime1 is true

delay 1

set myTime1 to time string of (current date)

if myTime1 > "4:30:00 pm" and myTime1 < "4:31:00 pm" then

set gIsThisTime1 to true

end if

end repeat

set myDate1 to date string of (current date)

set myDateTime1 to myDate1 & " - " & myTime1

say "Clock out for lunch"

tell application "Safari"

make new document with properties {URL:""https://google.ca"}

set the bounds of the front window to {769,25,3199,1740}

set keyword to "User Name"

repeat

set myWindow to current tab of first window

activate

do JavaScript "window.location.reload()" in myWindow

repeat while (do JavaScript "document.readyState" in document 1) is not "complete"

delay 0.5

end repeat

set pageContent to do JavaScript ("window.document.documentElement.outerHTML") in myWindow

if pageContent contains keyword then

exit repeat

end if

end repeat

end tell

tell application "System Events"

set textToType to "sull024"

keystroke textToType

key code 48

end tell

tell application "System Events"

set textToType to "password123"

keystroke textToType

key code 76

end tell

set gIsThisTime2 to false

repeat until gIsThisTime2 is true

delay 1

set myTime2 to time string of (current date)

if myTime2 > "5:00:00 pm" and myTime2 < "5:01:00 pm" then

set gIsThisTime2 to true

end if

end repeat

set myDate2 to date string of (current date)

set myDateTime2 to myDate2 & " - " & myTime2

say "Clock in for lunch"

tell application "Safari"

make new document with properties {URL:""https://google.ca"}

set the bounds of the front window to {769,25,3199,1740}

set keyword to "User Name"

repeat

set myWindow to current tab of first window

activate

do JavaScript "window.location.reload()" in myWindow

repeat while (do JavaScript "document.readyState" in document 1) is not "complete"

delay 0.5

end repeat

set pageContent to do JavaScript ("window.document.documentElement.outerHTML") in myWindow

if pageContent contains keyword then

exit repeat

end if

end repeat

end tell

tell application "System Events"

set textToType to "sull024"

keystroke textToType

key code 48

end tell

tell application "System Events"

set textToType to "password123"

keystroke textToType

key code 76

end tell

set gIsThisTime3 to false

repeat until gIsThisTime3 is true

delay 1

set myTime3 to time string of (current date)

if myTime3 > "10:00:00 pm" and myTime3 < "10:01:00 pm" then

set gIsThisTime3 to true

end if

end repeat

set myDate3 to date string of (current date)

set myDateTime3 to myDate3 & " - " & myTime3

say "Clock out for the night"

tell application "Safari"

make new document with properties {URL:""https://google.ca"}

set the bounds of the front window to {769,25,3199,1740}

set keyword to "User Name"

repeat

set myWindow to current tab of first window

activate

do JavaScript "window.location.reload()" in myWindow

repeat while (do JavaScript "document.readyState" in document 1) is not "complete"

delay 0.5

end repeat

set pageContent to do JavaScript ("window.document.documentElement.outerHTML") in myWindow

if pageContent contains keyword then

exit repeat

end if

end repeat

end tell

tell application "System Events"

set textToType to "sull024"

keystroke textToType

key code 48

end tell

tell application "System Events"

set textToType to "password123"

keystroke textToType

key code 76

end tell

The goal is to be able to run a script at any time of the day based on crashes that recognizes the existing time and bypasses any previous times and alerts me on the next "clock" time. This script only works if I start it at the start of my shift and dont have any issues but if I have to run it later on, it does nothing. Please help :D

2 Upvotes

4 comments sorted by

View all comments

Show parent comment

1

u/mvfsullivan Feb 03 '22 edited Feb 03 '22

Hey! This is an amazing reply, thank you so much. I do not have access to Automator, terminal, Script Editor etc, literally just TextExpander which has AppleScript capability. It kills me to have to do it like this but its all I have lol.

Also, your incredibly clean code puts mine to shame and truly shows exactly how much I know (hint, its just google :D)