2

I coded a simple AppleScript to remind me to stand up once an hour. I've saved the code as an application and run it, but when I try and shut down my machine it won't turn off. I have to force quit.

This is my first time building an AppleScript, so I'm a little lost about how to debug it.

repeat  
    set myTime to time string of (current date)
    
    if "00:00" is in myTime then
        display notification "Time to standup" with title "Standup" sound name "Blow"
    end if
end repeat

1 Answer 1

3

I think using on idle rather than repeat should stop it preventing sleep.
You can check this in Activity Monitor - set the View Menu to All Processes, then from the CPU or Memory tabs, right click the header & add Preventing Sleep, then sort by this.

enter image description here

The return value is to idle what delay is to a regular repeat. Your existing script is hammering round as fast as it can go, which it doesn't really need to do.
If it doesn't find 00:00 the first time round then the else sets it to check every minute until it does, then the return goes out to a full hour. I'm assuming you don't need millisecond precision for this ;)

on idle
    if the minutes of the (current date) is 0 then
        display notification "Time to standup" with title "Standup" sound name "Blow"
        return 3600
    else
        return 60
    end if
end idle

Save as an application & check 'Stay open after run handler'.

5
  • That works perfectly! Thank you for the detailed explanation. I did edit line 4 to :00:, I thought there could be an edge case if you started the application at 9:00:05, it would never trigger on 10:00:00 exactly, only 9:01:05 etc. Thank you again Commented Jan 7, 2022 at 13:09
  • Fair point! tbh, you don't really need all of that anyway. You could replace the whole of the myTime routine with if the minutes of the (current date) is 0 I've added the streamlined version above
    – Tetsujin
    Commented Jan 7, 2022 at 13:17
  • Should it not be 'is 00'? or is there some syntax contained with the minute's command? Commented Jan 7, 2022 at 14:31
  • If you write 00 it will compile as 0. It works. I watched it pass the hour mark.
    – Tetsujin
    Commented Jan 7, 2022 at 16:32
  • I'd return "3585" and "15" rather than "3600" and "60", so that minor delays won't cause you to miss an hour.
    – Mark
    Commented Jan 7, 2022 at 22:45

You must log in to answer this question.

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