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

waitForTimeBetween

How would you write out this function? I'm experimenting as we speak and so far no luck. I would greatly appreciate the help! I have no choice but to use TE

1

u/cynicalmoose Feb 04 '22

You can use the code you had previously and just replace the specified times with the names of the function arguments - e.g. (although I haven't tested this!):

to waitForTimeBetween(startString, endString)  
	(\*   
	NB For illustration only - this is not a recommended method!  
	\*)  
	set gIsThisTime1 to false  
	repeat until gIsThisTime1 is true  
		delay 1  
		set myTime1 to   
		if myTime1 > startString and myTime1 < endString then  
			set gIsThisTime1 to true  
		end if  
	end repeat  
end waitForTimeBetween  

But it's still not ideal. I don't know if you can use Objective-C methods in TextExpander, but if you can the following should work:

use AppleScript version "2.4" -- Yosemite (10.10) or later  
use framework "Foundation"  
use scripting additions  
property NSDate : a reference to current application's NSDate  
property NSDateFormatter : a reference to current application's NSDateFormatter  
to getSecondsUntil(timeString)  
	set dateFormatter to NSDateFormatter's new()  
	set dateFormatter's dateFormat to "h:mm:ss a" -- e.g. "11:29:00 am"  
	set dateFormatter's defaultDate to NSDate's new()  
	set targetDate to dateFormatter's dateFromString:timeString  
	return targetDate's timeIntervalSinceNow as integer  
end getSecondsUntil  
to scheduleReminder(reminderText, forTimeString)  
	delay getSecondsUntil(forTimeString) -- delay with a negative number is a no-op so it doesn't matter if forTimeString is in the past  
	say reminderText  
	loadSafariAndWait()  
	typeCredentials()  
end scheduleReminder  
scheduleReminder("Clock out for lunch", "11:29:00 am")

Now you could also try to re-write that in pure applescript, parsing the time string and converting it to seconds to give you the number of seconds to delay. Unfortunately AppleScript's date/time-handling functions leave a lot to be desired.