1

I tried different way to run a script on a specific schedule / time, I found Lingon quite handy, I also try using calendar but the result was very bad.

I wrote this :

repeat
    set myDate to date string of (current date)


    set myTime to time string of (current date)

    set myDateTime to myDate & " - " & myTime

    if myTime > "11:30:00" and myTime < "11:30:10" then
        display notification "demo - This is the time"
    end if
end repeat

which seems to work fine but obviously, generate a few notification. How can I run a script only once when this condition is found ?

I thought I can play with some delay but then I'm worried I would miss the time condition.

  • 1
    Why not just use Lingon? – user3439894 May 26 '18 at 11:07
  • I want to be able to schedule or stop the script quickly and a bit randomly, I also don't want to run it if I'm not here that while I prefer use a simple Applescript but that seems to work fine anyway at the end – Kevin May 26 '18 at 11:50
3

This is just a slightly more efficient re-hash of the existing answer...

set gIsThisTime to false

repeat until gIsThisTime is true
    delay 60
    set myTime to time string of (current date)
    if myTime > "1:15:00 pm" and myTime < "1:20:00 pm" then
        set gIsThisTime to true
    end if
end repeat

set myDate to date string of (current date)
set myDateTime to myDate & " - " & myTime
display notification "time = " & myDateTime

Notes:
leaving out am/pm means it will trigger on either am or pm, whichever it hits first
it doesn't work on 24-hour clock, so 13:15:00 will not trigger

0

REVISED SOLUTION:

The AppleScript code I used in my first solution used a repeat until theTime is greater than or equal to requested_time loop with a delay 10 command to keep checking the current time versus the requested time, every 10 seconds. Although running that code as a script or an application, did not use a huge amount of the System Resources... Saving the new following AppleScript code as a stay open application with an on run and on idle handler, in my opinion, had significantly less of an impact on the System Resources. Save this following AppleScript code as a stay open application.

This app, when run, will allow you to input your desired time, then choose a file or app to launch at that given time.

This AppleScript code works for me using the latest version of macOS Mojave.

global chosenApp
property theTime : missing value
property requested_time : missing value

on run
    set theTime to time string of (current date)
    activate
    set requested_time to text returned of (display dialog ¬
        "Please Enter Your Start Time With The Following Format: Hour:Minutes:Seconds" default answer theTime ¬
        buttons {"CANCEL", "OK"} default button "OK" cancel button "CANCEL" with title ¬
        "Choose App Launch Time")
    activate
    set chosenApp to choose file with prompt ¬
        "Choose The App Or File You Would Like To Run At Your Specified Time" without multiple selections allowed, invisibles and showing package contents
end run

on idle
    runAtSpecificTime()
    return 30 -- in seconds
end idle

on runAtSpecificTime()
    set theTime to time string of (current date)
    if theTime is greater than or equal to requested_time then
        tell application "Finder" to open chosenApp
        quit me
    end if
end runAtSpecificTime

on quit
    continue quit -- allows the script to quit
end quit
-2

Ok I think I found it :

set isThisTime to false
repeat
    set myDate to date string of (current date)


    set myTime to time string of (current date)

    set myDateTime to myDate & " - " & myTime

    if myTime > "11:30:00" and myTime < "11:47:26" then
        set isThisTime to true
        exit repeat
    end if
end repeat

if isThisTime is true then
    display notification "in the time"
end if
  • 2
    I'm no coder, but to me that looks like it's going to be hammering round that cycle at full clock speed all the time it's running, until TRUE is triggered. If there's no better way - cron or something above my pay-grade - then unless you need millisecond accuracy, I'd put a big delay timer in the while loop. You're also repeatedly calling the date, which really only seems to be needed once, at init. – Tetsujin May 26 '18 at 11:07
  • it's just I don't want to run this script all the time and I want to be able to stop it very quickly that while I don't use cron,but yes I tihnk I gonna come back to my first idea to add some delay even-though its seems to work fine so far – Kevin May 26 '18 at 11:48
  • Yup - I'd also remove all the pointless repetition. if it's checking the time in a loop, it doesn't need to be doing anything else in there. Pull everything else out to globals, or declare when you need them rather than create/destroy local variables every time round. It's not much 'work' but it's 'work' it doesn't need to be doing. – Tetsujin May 26 '18 at 11:55

You must log in to answer this question.

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