Applescript If exists trouble, please help

Discussion in 'Mac Programming' started by cjaneway, Apr 24, 2014.

  1. cjaneway macrumors newbie

    Joined:
    Apr 24, 2014
    #1
    I am have a problem with the set var editButton

    Getting this error-- System Events got an error: Can’t get table 1 of group 6 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1 of process "Safari". Invalid index.

    I know the elements in the table are available sometime and not others. The purpose of the editButton var is to check and see if that element exists before it tries to click the button. I believe what this error is telling me is that that element {n} does not exist. Any insight as to how to rectify this would be greatly appreciated.

    -Blake

    Code:
    
    set mainURL to "https://your site/" as text
    set mainLogin to "your username" as text
    set mainPword to "your password" as text
    
    
    --display dialog mainURL
    tell application "Safari"
    	activate
    	open location mainURL
    end tell
    
    delay 3
    -- Count number of rows in the table
    to countRows()
    	tell application "System Events"
    		tell process "Safari"
    			count (rows of table 1 of group 6 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1)
    		end tell
    	end tell
    end countRows
    
    set rowCount to countRows()
    
    --Number of rows minus the header
    set x to rowCount - 1 as integer
    
    --Repeat function
    repeat with n from 2 to x
    	
    	tell application "Safari"
    		tell application "System Events"
    			
    			--If this exists then execute process
    			tell process "Safari" to set editButton to get value of UI element 2 of row 1 of table 1 of UI element 2 of row {n} of table 1 of group 6 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
    			
    			if (exists editButton) then --If link exists 
    				
    				--then click link and click previous page
    				delay 3
    				tell process "Safari" to click button 1 of UI element 2 of row 1 of table 1 of UI element 2 of row {n} of table 1 of group 6 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
    				delay 2
    				tell process "Safari" to click UI element 2 of group 1 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
    			end if
    		end tell
    	end tell
    	
    end repeat --End Repeat
    
    tell application "Safari"
    	tell application "System Events"
    		delay 3
    		--Logout of website
    		tell process "Safari" to click static text 1 of UI element 2 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
    	end tell
    	quit --Close App
    end tell
    
    
     
  2. cjaneway thread starter macrumors newbie

    Joined:
    Apr 24, 2014
    #2
    I figured it out using TRY instead of IF, if anyone wanted to see the working script.

    Code:
    --display dialog mainURL
    tell application "Safari"
    	activate
    	open location mainURL
    end tell
    
    tell application "System Events"
    	tell application process "Safari"
    		
    		delay 5
    		
    		keystroke mainLogin --type username
    		keystroke (ASCII character 9) -- tab character
    		keystroke mainPword --type password
    		keystroke (ASCII character 13) -- this equals return
    		
    	end tell
    end tell
    
    delay 3
    -- Count number of rows in the table
    to countRows()
    	tell application "System Events"
    		tell process "Safari"
    			count (rows of table 1 of group 6 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1)
    		end tell
    	end tell
    end countRows
    
    set rowCount to countRows()
    
    --Number of rows minus the header
    set x to rowCount - 1 as integer
    
    --Repeat function
    repeat with n from 2 to x
    	
    	tell application "Safari"
    		tell application "System Events"
    			
    			--Try if this exists then execute process
    			try
    				--Click link and click previous page
    				delay 3
    				tell process "Safari" to click button 1 of UI element 2 of row 1 of table 1 of UI element 2 of row {n} of table 1 of group 6 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
    				delay 3
    				tell process "Safari" to click UI element 2 of group 1 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
    			end try
    		end tell
    	end tell
    	
    end repeat --End Repeat
    
    tell application "Safari"
    	tell application "System Events"
    		delay 2
    		--Logout of website
    		tell process "Safari" to click static text 1 of UI element 2 of group 1 of UI element 1 of scroll area 1 of group 1 of group 1 of group 2 of window 1
    	end tell
    	quit --Close App
    end tell
    
     

Share This Page