Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

eds1986

macrumors newbie
Original poster
Mar 8, 2012
1
0
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
 
Last edited:

Mark FX

macrumors regular
Nov 18, 2011
159
17
West Sussex, UK
You can use the Applescript delay command to wait a specific amount of time,
like this.

Code:
--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.

Code:
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

Code:
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

macrumors newbie
Jul 25, 2011
7
0
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 :
Code:
tell application "(your-application-name)"
	activate
end
OR
Code:
tell application "System Events"
	set frontmost of process "(your-application-name)" to true
end tell

Is that what you mean?

m.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.