Applescript run script every 5 seconds - applescript

I am trying to run the following code as a coroutine. Is there any way to accomplish this?
repeat
delay 5
log theScript
end repeat

Use the on idle handler and save the script as application
on idle
log theScript
return 5
end idle
In case of an AppleScriptObjC application you can't use the idle handler, an alternative is NSTimer of the Foundation framework.
property timer : missing value
on applicationWillFinishLaunching:aNotification
set timer to current application's NSTimer's scheduledTimerWithTimeInterval:5.0 target:me selector:"timerFired:" userInfo:(missing value) repeats:true
end applicationWillFinishLaunching_
on timerFired:aTimer
log "timerFired"
end timerFired

Related

How to set a default wait for Alert with Capyabara?

In my code I don't want to use sleep. How do I use wait_until.
########### my code ###############33
btn_logout.click
sleep 3
page.driver.browser.switch_to.alert.accept
This should work
accept_alert do
btn_logout.click
end
It'll wait for the modal as long as the max wait time that is set for Capybara.

vbs automatic script

Is it possible to create a vbs script that call itself when a certain pc action is activated,lets say opening up a browser?A replica would be someone opens up a browser,then the vbs listens to this activity and runs itself or calls another vbs script?
Jimmy, if you want the code to be executed when a browser window is opened, consider this code :
Set obj0 = createobject("wscript.shell")
Dim Count
Count = 0
Do while count = 0
If obj0.appactivate("browserwindowtitle") then
-------do something----
----
----
Count = 1
Else
Wscript.sleep(10000)
Count = 0
End if
Loop
Set obj0 = nothing
The above code will check if the window having the title "browserwindowtitle" is open or not. If it is open, it will execute the desired action. If the window is not open, script will wait for 10 seconds and try again.
After writing the script go to control panel-> scheduled tasks and add the script as scheduled on startup. When you do this, the script will start executing when your PC turns on and will keep on checking if the browser window is open or not.
There will be easier ways to do this, others might be able to help.
You may want to give System Scheduler a try.
http://www.splinterware.com/products/wincron.htm

Run a script for a determied amount of time

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

Set Messages (iChat) status to song currently playing in Rdio

I have the following script that successfully retrieves the current track and updates my Messages (iChat) status, but for this to work autonomously I guess I need to run it on a loop? Recommendations for that?
tell application "Rdio"
set theTrack to current track
set theArtist to artist of theTrack
set theName to name of theTrack
end tell
tell application "Messages"
if status is available then
set status message to ("♫ Playing in Rdio: " & (theArtist as string) & " - " & (theName as string))
end if
end tell
Unless Rdio has the ability to trigger scripts on certain condition (which you would have to check for yourself, as I am not a Rdio user myself – the rather sparse Rdio AppleScript docs on site do not indicate anything about that), your best chance to achieve this is to store your script as a Stay-Open AppleScript Application and put the script proper in the on idle handler. The AppleScript Language Guide has the nitty-gritty on this, if you want to look it up, but the basic procedure is:
wrap your script above in an on idle handler, i.e.:
on idle
tell application "Rdio"
set theTrack to current track
set theArtist to artist of theTrack
set theName to name of theTrack
end tell
tell application "Messages"
if status is available then
set status message to ("♫ Playing in Rdio: " & (theArtist as string) & " - " & (theName as string))
end if
end tell
return 0 -- change this to stray from the default 30 sec. interval
end idle
save the script as an AppleScript Application, making sure you check Stay open in the saving sheet.
Launch your newly created AppleScript app, and you are good to go – it will keep running, executing the idle handler periodically (every 30 seconds by default – you can change that value by returning an integer value from the idle handler, which will be evaluated as the number of seconds until the next check. If you want to be fancy, and the Rdio AS interface supports it, you could use the remaining playing time of your song, say…)

Speech Recognition Server Does Not Stay Open

I am trying to create a simple program that loops for user speech input using com.apple.speech.recognitionserver. My code thus far is as follows:
set user_response to "start"
repeat while user_response is not equal to "Exit"
tell application id "com.apple.speech.recognitionserver"
set user_response to listen for {"Time", "Weather", "Exit"} with prompt
"Good Morning"
end tell
if user_response = "Time" then
set curr_time to time string of (the current date)
set curr_day to weekday of (the current date)
say "It is"
say curr_time
say "on"
say curr_day
say "day"
else if user_response = "Weather" then
say "It is hot outside. What do you expect?"
end if
end repeat
say "Have a good day"
If the above is run on my system it says good morning and it then pops up with the speech input system and waits for either Time, Weather, or Exit. They all do what they say they are going to do, but instead of looping if I say Time and Weather and asking again until I say exit the speechserver times out and never pops up again. Is there a way of either keeping that application open until the program ends or is applescript not capable of looping for user speech input?
If you don't find a way to keep speech recognition open, try adding a delay before you call it again. I recall (long ago) finding that events can be just lost if you try to send an event to an application that's already in the middle of quitting (it doesn't reopen the application).
Before your end Repeat add
tell application "SpeechRecognitionServer"
quit
end tell
After about 35 seconds it will repeat, it is slow as honey on a cold day but it works. give it a try.
Here is a Simple Example:
repeat
tell application "SpeechRecognitionServer"
set theResponse to listen for {"yes", "no"} with prompt "open a finder?"
set voice to (theResponse as text)
end tell
if voice contains "yes" then
tell application "Finder"
activate
end tell
else
say "not understood"
end if
tell application "SpeechRecognitionServer"
quit
end tell
end repeat

Resources