I need to play a sound at random times throughout the day.
How can I create a random delay function, that then plays the file through the system audio?
delay x takes an integer for an argument.
random number from a to b generates a random integer between values a and b
try this
on run {}
my doit()
end run
on doit()
display dialog "hello world"
end doit
on idle {}
set ran to random number from 0 to 10
doit()
return ran
end idle
be sure to save it as stay open application
Related
I am working on an application where there are read only screens.
To test whether the data is being fetched on screen load, i want to set some wait time till the screen is ready.
I am using python to record the actions. Is there a way to check the static text on the screen and set the time ?
You can simply use
snooze(time in s).
Example:
snooze(5)
If you want to wait for a certain object, use
waitForObject(":symbolic_name")
Example:
type(waitForObject(":Welcome.Button"), )
The problem is more complicated if your objects are created dynamically. As my app does. In this case, you should create a while function that waits until the object exists. Here, maybe this code helps you:
def whileObjectIsFalse(objectID):
# objectID = be the symbolic name of your object.
counter = 300
objectState = object.exists(objectID)
while objectState == False:
objectState = object.exists(objectID)
snooze(0.1)
counter -= 1
if counter == 0:
return False
snooze(0.2)
In my case, even if I use snooze(), doesn't work all the time, because in some cases i need to wait 5 seconds, in other 8 or just 2. So, presume that your object is not created and tests this for 30 seconds.
If your object is not created until then, then the code exits with False, and you can tests this to stop script execution.
If you're using python, you can use time.sleep() as well
I want to know how to execute a function after a certain amount of time has passed. The user will enter a duration, say 30 minutes, and after 30 minutes they will be given a message, along with other code being done. I am new to Ruby, and can't figure out the best way to do it.
If you don't want to block IO you can use threads:
time = gets.to_i # time in seconds
Thread.new do
sleep time
# your code here
end
Or just:
time = gets.to_i # time in seconds
sleep time
# your code here
You could look into gems like DelayedJob or Resque.
I am getting into ruby and have been using threads for a little while now with out fully understanding them. I notice that when adding a thread to an array and if I add a sleep() command as the first command the thread does not run until I do a join which is mostly what I want. So I have 2 questions.
1.Is that suppose to happen?
2.Is there a better way to do that other then the way I'm doing it. Here is a sample code that I have to show what I'm talking about.
job = Array.new
10.times do |n|
job << Thread.new do
sleep 0.001
puts "done #{n}"
end
end
#job.each do |t|
#t.join
#end
puts "End of script"
Output is
End of script
If I remove the comments output is
done 1
done 0
done 7
done 6
done 5
done 4
done 3
done 2
done 9
done 8
End of script
So I use this now but I don't understand why it does that. Sometimes I notice even doing something like `echo hi` instead of sleep does the trick.
Thanks in advance.
Timing of threads isn't a defined behavior. Once you put them to sleep, they will be put in a queue to be run later. You can't ever expect it to run one way or another.
Your main program doesn't take very long to run, so it is likely to happen to finish before your other threads get picked back up to run again. Really, when you think about it, 0.001 seconds is quite a long time to computer, so spinning off 10 threads in that time is likely to happen -- but even if it takes longer, there is no guarantee the thread will resume immediately after .001 seconds. Often there's really no guarantee it won't start before .001 seconds, either, but sleep calls usually don't end early.
When you add the join calls, you are introducing additional time into your main thread which allows the other threads time to run, so this behavior is expected.
I've started to play with Ruby on Rails to make some plugins for Siri Proxy Server.
I am inexperienced with Ruby but have manage the basics.
what I have done:
################ Commands
listen_for (/show a demo to (.*)/i) do |name|
show_demo
request_completed
end
################ Actions
def show_demo(name)
say "Hi #{name}, let me do a quick demo for You."
say "For example if You tell me 'Turn on sidelight' I will turn the sidelights in Living room like now..."
system "/usr/local/bin/tdtool --on 2"
say "That was the sidelights, and now if like I can turn on the gallery for You, just tell me 'turn on gallery' like so... "
system "/usr/local/bin/tdtool --on 3"
say "This only part of things I can do after mod."
say "Now I will turn all devices off..."
system "/usr/local/bin/tdtool --off 3"
system "/usr/local/bin/tdtool --off 2"
say " Thank You #{name}, and goodbye."
end
The problem is when I'll start the demo all the actionssystem "..." are executed before Siri start to say anything .
How can I delay above action to put them in right place in time to execute them right after words I want?
Thank You in advance.
The problem is that say won't wait for Siri to actually say the words, it just sends a packet over to your iDevice and then goes on. The simplest approach i can think of would be to wait a few seconds, depending on how long the text is. So first we need a method that gives us the duration to wait (in seconds). I tried with the OSX built-in say command and got the following results:
$ time say "For example if You tell me 'Turn on sidelight' I will turn the sidelights in Living room like now..."
say 0,17s user 0,05s system 3% cpu 6,290 total
$ time say "That was the sidelights, and now if like I can turn on the gallery for You, just tell me 'turn on gallery' like so... "
say 0,17s user 0,06s system 2% cpu 8,055 total
$ time say "This only part of things I can do after mod."
say 0,13s user 0,04s system 5% cpu 2,996 total
So this means we have the following data:
# Characters w/o whitespace | Seconds to execute
------------------------------+---------------------
77 | 6.290
87 | 8.055
34 | 2.996
This leaves us with an average of about 0.0875 seconds per character. You may need to evaluate the average time for your scenario yourself and with more samples. This function will wrap say and then wait until the text was spoken out by Siri:
def say_and_wait text, seconds_per_char=0.0875
say text
num_speakable_chars = text.gsub(/[^\w]/,'').size
sleep num_speakable_chars * seconds_per_char
end
where gsub(/[^\w]/,'') will remove any non-word characters from the string. Now you can use this to simply say something and wait for it to be spoken out:
say_and_wait "This is a test, just checking if 0.875 seconds per character are a good fit."
Or you can also override the duration in special cases:
say_and_wait "I will wait for ten seconds here...", 10
Let me know if it works for you.
I'd like to run an applescript for a determined amount of time.
When the counter reaches 0 the script should stop working.
I've found online a timer code like this one:
set input to text returned of (display dialog "Enter length of timer" default answer "")
delay input
beep
But instead of waiting for that amount of time I'd like to perform all the cycles and actions of my script. Is there any easy way to achieve that?
set start to current date
repeat
say "a"
if (current date) - start ≥ 3 then exit repeat
delay 1
end repeat