I need to make a script to runs on random days in a week. It will need to run every day but might not finish running every day as it is not a random day that the script selects.
I know how to start creating a random number, but get stuck next in getting to compare the day of the week to the random number and either exit or execute the rest of the script.
set theNumb1 to random number from 1 to 7
set theNumb to theNumb1 as string
delay theNumb
Anybody out there who knows what to do next or how to compile this?
Browser: Safari 534.50
Operating System: Mac OS X (10.6)
Does the script have to run at least once a week?
Must it only run once a week?
Does the day(s) have to be randomly chosen every week or once in advance?
etc.
It must run at least once a week. it could run more often if that is what happens, as a matter of fact that would be nicer. See I want to use this script to send random reminders for tasks that need to be done.
[quote=Nigel
Does the day(s) have to be randomly chosen every week or once in advance?
etc.
[/quote]
I imagined it like this, the scripts gets triggered daily (needs to be as the MM that runs it shuts down daily) by lets say iCall. It than generates a random day and if it is that day it would run. However your question makes me realise that it might not run in weeks done that way. so maybe we need to pick 2 or 3 of the weekdays and if it is one of these i runs. this would makes the change of it not running in a week very small.
Two approaches that come to mind are:
- A “shall I act today or not?” method like your original idea, but with increased odds of getting a hit. So instead of the script choosing a random day when it runs and acting if today’s that day, it chooses a random number between, say, 1 and 3, or 1 and 4, and acts if the result’s, say, 1.
if ((random number from 1 to 4) is 1) then
-- Perform.
end if
- A “when shall I act next?” method whereby, when the script does act, it chooses a random weekday from the following seven and stores it in a property. Next time it runs, it compares the current day with the stored one and acts if there’s a match. The illustration here uses the weekday number (Sunday start) rather than the weekday itself.
property nextRunDay : missing value
set todaysNumber to (current date)'s weekday as integer
if (nextRunDay is missing value) or (nextRunDay is todaysNumber) then
-- Perform.
-- Then:
set nextRunDay to (todaysNumber + (random number 6)) mod 7 + 1
end if
This could return the same weekday number twice in succession, which is OK if the script’s only run once a day: it’ll just act again next on the same day the following week.
Possibly a better implementation of this approach would be to set an actual date for the next performance:
property nextRunDate : missing value
set today to (current date)
set today's time to 0
if (nextRunDate is missing value) or (today ≥ nextRunDate) then
-- Perform.
-- Then:
set nextRunDate to today + (random number from 1 to 7) * days
end if
Nigel thanks a million, my approach was very different and I would have never come up with this (skills to primitive) I am going to test these and see what works best.
Nigel I have run tests (all work like a dream) and I am going to use your 3rd script. Again thanks for helping me out here!