New to Applescript

Discussion in 'Mac Programming' started by eds1986, Mar 8, 2012.

  1. eds1986, Mar 8, 2012
    Last edited: Mar 9, 2012

    macrumors newbie

    Joined:
    Mar 8, 2012
    #1
    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
     
  2. macrumors member

    Mark FX

    Joined:
    Nov 18, 2011
    #2
    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
     
  3. macrumors newbie

    MacGrunt

    Joined:
    Jul 25, 2011
    #3
    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.
     

Share This Page