Run AppleScript as login item but only if it meets specific time criterion

Solution 1:

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 .