1

I have this AppleScript as a "login item" (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 "System Events" to keystroke " " using {option down}    
delay 1    
tell application "System Events"        
    keystroke "start"       
end tell    
tell application "System Events" to key code 36 
3
  • No, leaving aside whatever it does, what you need to do is have it look at the clock and not do whatever it is, when you don't want it doing whatever it is. May 16 at 3:11
  • Also, rather that popping up Spotlight/Alfred/Quicksilver/Butler/whatever and using it to launch whatever this is, why don't you, well, just launch whatever this is? May 16 at 19:28
  • Hi Marc, I know what you mean... To be honest I'm not a programmer I kind of understand how applescript works and as you can see my script is as empiric as it looks, with this code I get the computer to run an Alfred Workflow, I think I would be able to launch it if it was an app but I couldn't come with any idea on how to make the script to laun an alfred workflow, If you know I will appreciate your help on this matter. May 17 at 1:35
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 .

2
  • Thank you! This seems just what I need, I will give a try! I Appreciate your help, I'm sorry I'm not a programmer... I would also like the script to display a notification if the "if" condition is not met... Lets say "This is not the time to run this script" do you know how can I acchieve that? May 17 at 1:38
  • @Notrace Notracking, RE: "I would also like the script to display a notification if the "if" condition is not met... Lets say "This is not the time to run this script" do you know how can I acchieve that?" -- Yes, add an else clause to the if statement block with a display notification command. Have a look at if Statements under Control Statements Reference and display notification under Commands Reference in the AppleScript Language Guide linked in my answer. May 17 at 1:58

You must log in to answer this question.

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