PDA

View Full Version : New to Applescript




eds1986
Mar 8, 2012, 04:12 PM
Hello all, I'm new here but thought it the best place to ask this.

Iv just started using Applescript Editor and I need some help. I have written a string of commands (activating applications followed by keystrokes ect.) and I wanted to know if there was a way to make sure the script waits for each command to finish before it starts the next?

i.e. if it takes a long time to load an app the script would wait until it is loaded before it hits the enter key.

If anyone can help that would be great.

Thanks, Ed

Also it would be helpful to know how to Focus a particular app from the app selector (command tab). At the moment I'm using a series of key strokes to select the one I wont and it's not really working for what I need.

Thanks Again



Mark FX
Mar 9, 2012, 03:10 PM
You can use the Applescript delay command to wait a specific amount of time,
like this.


--Long process code

delay 3 --wait 3 seconds

--Another not so long process

delay 0.5 -- wait half second


Also you could check that an application is active by using a repeat loop,
like this.


set runningApps to {} as list
tell application "System Events"
repeat until runningApps contains "TextEdit"
set runningApps to name of every application process whose visible is equal to true
delay 1
end repeat
end tell


But be aware, this repeat loop will go forever until the TextEdit app is started.

You could also adapt this to run the repeat a certain number of times,
like this


set runningApps to {} as list
tell application "System Events"
repeat 30 times
set runningApps to name of every application process whose visible is equal to true
if runningApps contains "TextEdit" then
exit repeat
end if
delay 1
end repeat
end tell


This version runs the repeat loop 30 times, or about 30 seconds, before it
stops checking for the TextEdit App, but if the App starts before the 30 seconds
is up, then the script will exit.

Hope this is of some help.

Regards Mark

MacGrunt
Mar 31, 2012, 11:36 PM
Also it would be helpful to know how to Focus a particular app from the app selector (command tab). At the moment I'm using a series of key strokes to select the one I wont and it's not really working for what I need.

G'day,

If you mean you want to make a particular application active on screen you can just use this :
tell application "(your-application-name)"
activate
end
OR
tell application "System Events"
set frontmost of process "(your-application-name)" to true
end tell

Is that what you mean?

m.