2

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.

  • 1
    If you want something to happen at a specific time, maybe it would be better to consider using launchd and a Launch Agent instead of such rigmarole coding. – user3439894 Apr 4 '20 at 18:39
  • 3
    Can we start with … "What are you actually trying to achieve?" rather than try to decode the spaghetti – Tetsujin Apr 4 '20 at 18:44
  • 1
    Locking up the script with those repeat loops and delays is a bad idea even in a Cocoa app, and may not be giving an editor time to do much. Other options would be to use NSTimer, or set up a Calendar alarm. – red_menace Apr 4 '20 at 19:12
  • @Tetsujin I want to send a message at midnight. sending after a length of time would not work launchd might work, I will have to try it Is there a way to write something like find time until X, then wait Y seconds? – FBI Surveillance Van Apr 4 '20 at 19:52
  • 6
    "I want to send a message at midnight. " launchd is the tool for this. Having an AppleScript running continually in the background is just a bad idea. – Allan Apr 5 '20 at 2:53
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

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)
  • If anyone has a better answer than this post the code as an answer and I will upvote/mark yours as the answer – FBI Surveillance Van Apr 5 '20 at 19:38

You must log in to answer this question.

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