Jump to content

Start workflow once a day randomly


Recommended Posts

Posted

Tricky question.

 

I would like to launch a my personal workflow once a day , randomly, between two times (example random from 9:PM to 20:00 every day).

 

I read that probably I have to use launchd for Mac . Is there something like this already did?

 

Thank you

Posted
19 minutes ago, Daniele said:

Is there something like this already did?

 

Not specifically, no. Your random time requirement is unusual, and if you want to use launchd, you're going to have to figure out the random part yourself.

Posted
2 hours ago, Daniele said:

random from 9:PM to 20:00 every day

 

Do you mean between 9:00 and 20:00? 9PM is 21:00.

 

2 hours ago, Daniele said:

I read that probably I have to use launchd for Mac

 

launchd is indeed your best bet, but it has a peculiarity you may not agree with: if the machine is asleep at the time of the trigger, it’ll fire as soon as it wakes up. Multiple events accumulate and fire at once. If that’s not something you want, you may wish to have the script / Workflow itself check if it’s trying to fire at the right time, and skip if it isn’t.


To get the random time, you’ll need to have the firing script edit (or overwrite) the launchd job itself with the new hour and minute. You may also need to unload and load it back again, to make the system recognise the update (unsure if that’s necessary; would have to test).

Posted (edited)
57 minutes ago, vitor said:

To get the random time, you’ll need to have the firing script edit (or overwrite) the launchd job itself with the new hour and minute.

 

This is pretty tricky to do (it's how my script to change Alfred's theme at sunrise/sunset works). You have to fork a child process and wait for the parent (i.e. the process launchd is running) to exit, as launchd doesn't let you update a Launch Agent while it's running.

 

A simpler way might be to run the script at 9 a.m. (assuming that's what @Daniele actually means), and have it first sleep for a random duration between 0 and 660 minutes.

Edited by deanishe
Posted
16 minutes ago, deanishe said:

This is pretty tricky to do (it's how my script to change Alfred's theme at sunrise/sunset works). You have to fork a child process and wait for the parent to exit, as launchd does not like you updating a Launch Agent while it's running.

 

In this case I was thinking the agent wouldn’t do the updating, the Workflow would, called via an External Trigger (hence returning before the action is performed).

 

If updating the agent itself would still be problematic, a way to go around it would be to make a new agent every time (append some random string), but that then leads to workarounds on how to delete the previous one.

 

38 minutes ago, deanishe said:

A simpler way might be to run the script at 9 a.m. (assuming that's what @Daniele actually means), and have it first sleep for a random duration between 0 and 660 minutes.

 

That can be problematic. If the machine is often off at 9:00, it may not fire often (more than what’s desired). If it’s asleep, the timer will also start later in the day. But even if it starts on time, the machine may be under heavy load or asleep several times during this interval, delaying the timer even further. Also, a crash may occur or a reboot might be necessary, which would kill the job. All of this contributes to the firing time being heavily skewed for the latter portion of the day, possibly even firing after 20:00 or not at all if the machine is turned off or asleep before then. 

Posted
1 hour ago, vitor said:

If the machine is often off at 9:00, it may not fire often (more than what’s desired). If it’s asleep, the timer will also start later in the day. But even if it starts on time, the machine may be under heavy load or asleep several times during this interval, delaying the timer even further.

 

I was thinking it’d be a bit smarter than that, and check what the current time is before sleeping. You could also have the script only sleep for 10 minutes at a time, and then pick a wake-up at random to do its thing.


Multiple Launch Agents would also work, provided you set each one to fire once, not every day. A fair deal more complicated to code than sleep, though, as it’d have to know how to create Launch Agents and manage them via launchctl.

 

1 hour ago, vitor said:

All of this contributes to the firing time being heavily skewed for the latter portion of the day, possibly even firing after 20:00 or not at all if the machine is turned off or asleep before then.

 

I don't think it would be significantly skewed if it were a bit smarter than just sleeping for several hours, and you'll have the "late firing" problem with both because of the way launchd works. To avoid that, in either case, you'd have to make the script exit instead of doing whatever if it fires after the cutoff.

Posted (edited)

Hello mates, thank you for your support.

 

I solved in a simple way. 

 

 

1) created a external trigger that do something

2) Created a script with AS that call launch the triggger

2) created file .plist to load it into launchd that simple run every 6 hours (21600 seconds)

 

I highly recommend to install LaunchControl app from Brew Cask, that helps you a lot configuring launchd.

 

 

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>

    <key>Label</key>
    <string>workflow.plist</string>
    
    
    <key>RunAtLoad</key>
    <true/>
    
    <key>StartInterval</key>
    <integer>21600</integer> <!-- //every 6 hours -->
    
    <key>ProgramArguments</key>
    <array>
      <string>/usr/bin/osascript</string> <!-- use osascript to launch the script -->
      <string>/Applications/Alfred.alfredpreferences/workflows/user.workflow.FF774A33-6E03-4D11-9DFB-F88B03455C77/script.scpt</string> <!-- the argument -->
    </array>

  </dict>
</plist>

 

Thank you 🙂

 

 

Edited by Daniele
Posted
2 hours ago, Daniele said:

Created a script with AS that call launch the triggger

 

You can call the trigger directly from the Launch Agent by passing the script to osascript -e 'your script goes here'

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...