automatic web order processing

Hi,

I’m working on an applescript to automate processing web orders on our website, The idea is that the script launches Safari, logs in, checks if there are any orders (and if not, gives up) and if there are some, opens each in turn, changes it’s status from pending to shipped and then prints it out, returning to the orders list page and selecting the next one, or, if there are none, stopping (perhaps with a dialog to say there are no more orders).

I can’t get the repeat loop working correctly, and I’m not sure about the if/else clause either.

Here’s my script:


tell application “Safari”
activate
open location “http://www.my_website”
delay 5
tell application “System Events”
tell process “Safari”
keystroke tab
keystroke “username”
delay 5
keystroke tab
keystroke “password”
delay 5
keystroke return
delay 5
end tell
tell application “Safari”
set counter to 0
– the number of links in the front document
set the_link_number to do JavaScript “document.links.length” in document 1
repeat the_link_number times
– getting the target URL of the first link of the front document
set counter to counter + 1
do JavaScript “document.links[” & counter & “].href” in document 1
end repeat
end tell
tell application “Safari”
set thelink to do JavaScript “document.links[1].href” in document 1
delay 5
set the URL of document 1 to thelink
delay 5
set thelink to do JavaScript “document.links[1].href” in document 1
end tell

	-- this is the point I want the main repeat loop to come back to --
	
	repeat
		if the_link_number = 12 then
			display dialog "there aren't any orders so I'm gonna just give up"
			delay 5
			stop
		else
			-- open first pending order --
			delay 5
			tell application "Safari"
				set counter to 0
				-- the number of links in the front document
				set the_link_number to do JavaScript "document.links.length" in document 1
				repeat the_link_number times
					-- getting the target URL of the first link of the front document
					set counter to counter + 11
					do JavaScript "document.links[" & counter & "].href" in document 1
				end repeat
			end tell
			
			-- change status from pending to shipped --
			delay 5
			tell application "Safari"
				set thelink to do JavaScript "document.links[11].href" in document 1
				delay 5
				set the URL of document 1 to thelink
				delay 5
				set thelink to do JavaScript "document.links[1].href" in document 1
			end tell
			keystroke tab
			delay 3
			key code 125
			delay 3
			key code 125
			delay 3
			keystroke return
			
			-- select print this invoice --
			delay 5
			tell application "Safari"
				set counter to 0
				-- the number of links in the front document
				set the_link_number to do JavaScript "document.links.length" in document 1
				repeat the_link_number times
					-- getting the target URL of the first link of the front document
					set counter to counter + 12
					do JavaScript "document.links[" & counter & "].href" in document 1
				end repeat
			end tell
			delay 5
			tell application "Safari"
				set thelink to do JavaScript "document.links[12].href" in document 1
				delay 5
				set the URL of document 1 to thelink
				delay 5
				set thelink to do JavaScript "document.links[12].href" in document 1
			end tell
			
			-- bring up print dialog and print invoice --
			delay 5
			tell application "System Events"
				tell process "Safari"
					keystroke "p" using (command down)
					delay 5
					keystroke return
				end tell
			end tell
		end if
	end repeat
end tell

end tell


Any ideas would be gratefully accepted (as ever).

Cheers,

Kev.

OK, I just realised a possible problem; I wasn’t sending Safari back to the Pending Orders page of the site to then do the ‘if there are orders process them, else give up’ bit. I’ve added the following (the bit after the print dialog bit in the original script):

		-- bring up print dialog and print invoice --
			delay 5
			tell application "System Events"
				tell process "Safari"
					keystroke "p" using (command down)
					delay 5
					keystroke return
				end tell
			end tell
			
			-- tell Safari to go back to the pending orders page and the if/repeat bit --
			delay 5
			
			tell application "Safari"
				open location "https://my_website/shop/admin/admin/order.php?"
				delay 5
			end tell
		end if
	end repeat
end tell

end tell

What’s happening is that the script opens the prnding orders page, then a blank safari page, which it then proceeds to print. There appears to be some confusion in the repeat loops.