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.