Jump to content
Macro Express Forums
jack777

Program macro at random interval

Recommended Posts

Hi,

I'm wondering if it's possible to program a macro that would run at random intervals (every 1-2 hours on average).

Thanks for your feedback !

Have a nice day !

Share this post


Link to post
Share on other sites

Hi,

Interesting question, although I'll resist the temptation to put my current project aside to explore it thoroughly! Some initial thoughts:

1. Your  macro 'Random' uses Activate > Schedule, with the option 'When a Specified Time has Elapsed'. Macro 'Timer' runs every M minutes, where M equals a reasonable minimum for your time range, say 5 minutes. It opens (not activates) 'Random' in the Script Editor, operating on the Activate tab to change the 'elapsed time' entry to an appropriately random value and then saving 'Random'. Hopefully (!) that will then run randomly, doing whatever you have scripted it to do, then close - awaiting further modification.

2. Explore the 'Run Macro in Variable' command.

3. Maybe we could suggest simplified methods if you can spell out the details of what the macro will do? As an obviously extreme example, if it was merely to remind you of something at random intervals ("RSI ALERT! Leave your PC keyboard for a while,") then it would be trivial.

Share this post


Link to post
Share on other sites

Schedule your macro (with Macro Express) to run every 1 minute.  Durin each run of the macro:

If "timer file" does not exist, generate a random number between 60 and 120 (because you want between 1 and 2 hours).  Store the number in a text file ("timer file").

If "timer file" DOES exist, read it, decrement the number by 1, and check whether the number has reached zero.  If not, store the new (decremented) number in "timer file" and exit from the macro without doing anything else.  If time HAS reached zero, delete "timer file" and do whatever other stuff the macro is intended for.  Because you have deleted "timer file", next time the macro runs 1 minute later the whole scheduling process will begin again.

If you have Macro Express run your macro every 5 minutes, instead of every 1 minutes, then the decrement described above would be 5, not 1.  And so on -- since you have a fairly broad time range for your random runs, it is not really necessary to check "timer file" as often as every minute.   

Somebody will probably embarrass me with a much simpler method, but that's all I have come up with for now. :huh:

Share this post


Link to post
Share on other sites

I would create a macro that waits a random interval. When it starts, it generates the long interval. Then have there be a loop with a short interval. Say 1-10 seconds. The number of iterations would be the long divided by the short. Each time it loops, you can update the text box with a countdown with the remaining long interval. IE a countdown to the next iteration.

Share this post


Link to post
Share on other sites
5 hours ago, rberq said:

Schedule your macro (with Macro Express) to run every 1 minute.  Durin each run of the macro:

If "timer file" does not exist, generate a random number between 60 and 120 (because you want between 1 and 2 hours).  Store the number in a text file ("timer file").

If "timer file" DOES exist, read it, decrement the number by 1, and check whether the number has reached zero.  If not, store the new (decremented) number in "timer file" and exit from the macro without doing anything else.  If time HAS reached zero, delete "timer file" and do whatever other stuff the macro is intended for.  Because you have deleted "timer file", next time the macro runs 1 minute later the whole scheduling process will begin again.

If you have Macro Express run your macro every 5 minutes, instead of every 1 minutes, then the decrement described above would be 5, not 1.  And so on -- since you have a fairly broad time range for your random runs, it is not really necessary to check "timer file" as often as every minute.   

Somebody will probably embarrass me with a much simpler method, but that's all I have come up with for now. :huh:

Looks like a pretty nice solution ! Thank you very very much !!!!

Share this post


Link to post
Share on other sites

Take a look at Cory's suggestion, though -- his is simpler to program.  Execute the macro only once, so it does its thing then idles for the random time before doing its principle function again, generating a new random interval, and so on.  You don't even have to intervene to start it running, just schedule it to run when Macro Express is started.  

My thinking is kind of stuck at the Macro Express 3 level which doesn't allow for more than one macro to run at a time -- therefore I missed the option of having your macro run only once for a duration of many hours or even days.  With ME3 if the macro runs continuously, then no other macros can run.  With ME Pro a long-running macro doesn't block other macros from running. 

Share this post


Link to post
Share on other sites
4 hours ago, rberq said:

Take a look at Cory's suggestion, though -- his is simpler to program.  Execute the macro only once, so it does its thing then idles for the random time before doing its principle function again, generating a new random interval, and so on.  You don't even have to intervene to start it running, just schedule it to run when Macro Express is started.  

My thinking is kind of stuck at the Macro Express 3 level which doesn't allow for more than one macro to run at a time -- therefore I missed the option of having your macro run only once for a duration of many hours or even days.  With ME3 if the macro runs continuously, then no other macros can run.  With ME Pro a long-running macro doesn't block other macros from running. 

I'm running Macro Express pro so more macros is not a problem for me. But I don't understand Cory's suggestion. Maybe because my English is limited...

Share this post


Link to post
Share on other sites

This is what I think Cory is saying, simplified just a little:

:BEGIN (generate a random number between 60 and 120)
Repeat until RAND > 60
   Variable Set Integer (random) RAND (max 120)
Repeat end

Repeat until RAND = 0
   Wait for time to elapse (1 minute)
   Subtract 1 from RAND
Repeat end

Do whatever stuff the macro is intended for

GOTO :BEGIN  

This will ALWAYS wait between one and two hours, though you said you want it USUALLY to wait that long.  You can expand the random number range if desired to get some times less than an hour or more than two hours.  
    

 

Share this post


Link to post
Share on other sites

Two loops (repeats), one inside the other. In the outside loop generate a random number between 3600 and 7200. This is the number of seconds in one to two hours. Create an inner loop that loops that random number of times. In the inner loop, insert a command to decrement the counter by 1, a 1 second delay, and a message box update command. In the message box you can display the random number as it decrements. This will serve as a launch countdown. When it exits the inner loop, but still within the outer loop, close the message box and execute your macro commands.

You might also consider using the Macro Run and have your main code exist in it and be called by this macro. 

You could also get fancy and use time variables instead of integer to display a more friendly countdown. EG: Hours, minutes, seconds.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...