Whenever I try running this code it does not log the text.

 repeat
        set myTime to (time of (current date))
        set precisetime1 to 82800
        set precisetime2 to 84600
        set precisetime3 to 85500
        set precisetime4 to 86100
        set wantTime to "0"
        if myTime ≥ precisetime1 then
            repeat
                set myTime to (time of (current date))
                if myTime ≥ precisetime2 then
                    repeat
                        set myTime to (time of (current date))
                        if myTime ≥ precisetime3 then
                            repeat
                                set myTime to (time of (current date))
                                if myTime ≥ precisetime4 then
                                    repeat
                                        set myTime to (time of (current date))
                                        if myTime ≤ wantTime then
                                            tell application "Messages"
                                                activate
                                            end tell
                                            tell application "System Events"
                                                keystroke "Good Morning"
                                                keystroke return
                                            end tell
                                            delay (0.5)
                                            exit repeat
                                        end if
                                        delay (5)
                                    end repeat
                                end if
                                delay (300)
                            end repeat
                        end if
                        delay (900)
                    end repeat
                end if
                delay (1800)
            end repeat
        end if

        log {"checked at % seconds since midnight", myTime}
        delay (3600)
    end repeat

this code is supposed to wait until midnight, then send a message saying "good morning" I added so many if statements and repeat statements because the code was designed to run constantly and checking every 5 seconds used too much resources.


Solution 1:

you can use this tutorial to use launchd:

https://www.maketecheasier.com/use-launchd-run-scripts-on-schedule-macos/

<key>StartCalendarInterval</key> 
<dict> 
    <key>Hour</key> 
    <integer>9</integer> 
    <key>Minute</key> 
    <integer>0</integer> 
<string>/Users/user/Scripts/restart.sh</string>
</dict>

I am also new to AppleScript

I hope this works

Solution 2:

Turns out, My answer was simple

I added a delay function

set myTime to (time of (current date))
set waitTime to 86400 - myTime
delay (waitTime)
tell application "Messages"
    activate
end tell
tell application "System Events"
    keystroke "Good Morning"
    keystroke return
    log {"checked at % seconds since midnight", myTime}
end tell
delay (3600)