Run AppleScript as login item but only if it meets specific time criterion I have this AppleScript as a &quot;login item&quot; (it runs every time I login), but I really just need it to run the code if I login between 5:00 AM and 11:00 pm.
I would appreciate your help to achieve this result.
delay 60    
tell application &quot;System Events&quot; to keystroke &quot; &quot; using {option down}    
delay 1    
tell application &quot;System Events&quot;        
    keystroke &quot;start&quot;       
end tell    
tell application &quot;System Events&quot; to key code 36
You can wrap your code in an if statement block that checks the seconds since midnight:
Example AppleScript code:
set secondsSinceMidnight to time of (current date)

if secondsSinceMidnight ≥ 18000 and secondsSinceMidnight ≤ 82800 then
    
    --  # Your code goes here.
    
    
end if


Notes:
I assume the code you show in your question is either just part of it, as keystroke with System Events is sent to the front most window,  or you are manually bring the target window front most before the code is run.
If you place your code within the example if statement block, as coded, only the code within it will be run if it's between 5:00 AM and 11:00 PM.
Have a look at current date and the date class in the AppleScript Language Guide .