Apple Script Help- Clicking somewhere in Safari

Discussion in 'Mac Programming' started by ApolloProg, Apr 14, 2009.

  1. macrumors newbie

    Joined:
    Apr 14, 2009
    #1
    Hey.
    So I have an apple script question,
    How do I get Apple Script to click somewhere in safari via source code of a page?
    I'm trying to get Apple script to tell safari to click a button.
    This is pretty much what I'm trying to do:

    Code:
    tell application "Safari"
    	activate
    	
    	do JavaScript "window.open('http://www.WebPageHere.com')" in document 1
    end tell
    
    tell application "System Events"
    	tell process "Safari"
    		--Whatever Goes Here To Make Safari Click On The Button
    	end tell
    end tell
    Any ideas?
     
  2. macrumors member

    Joined:
    Aug 25, 2008
    Location:
    NE Florida
    #2
    Try this, perhaps:

    tell application "Safari"
    activate
    do JavaScript "window.open('http://www.WebPageHere.com')" in document 1
    delay 8 -- need to allow enough time for the web page to load; adjust as necessary
    end tell
    tell application "System Events"
    tell process "Safari"
    repeat x times -- where x = the number of times it takes to reach the button using the tab key
    keystroke tab
    end repeat
    keystroke return -- performs the press action
    end tell
    end tell

    Good luck.
     
  3. macrumors 6502a

    LtRammstein

    Joined:
    Jun 20, 2006
    Location:
    Denver, CO
    #3
    I cleaned it up so people could read it:

    Code:
    tell application "Safari"
    	activate
    	do JavaScript "window.open('http://www.WebPageHere.com')" in document 1
    	delay 8 -- need to allow enough time for the web page to load; adjust as necessary 
    end tell
    
    tell application "System Events"
    	tell process "Safari"
    		repeat x times -- where x = the number of times it takes to reach the button using the tab key
    			keystroke tab
    		end repeat
    		keystroke return -- performs the press action
    	end tell
    end tell
    
     
  4. thread starter macrumors newbie

    Joined:
    Apr 14, 2009
    #4
    I need it to click a button though. Not enter text.
     
  5. macrumors member

    Joined:
    Aug 25, 2008
    Location:
    NE Florida
    #5
    The script is designed to click a button.

    If you find that pressing the Tab key repeatedly doesn't bring focus to the desired button, make sure that under Safari Preferences > Advanced > "Press Tab to highlight each item on a webpage..." is checked, and that under System Preferences > Keyboard & Mouse > Keyboard Shortcuts > Full keyboard access, "All Controls" is selected.

    Good luck.
     

Share This Page