Getting url variables without a browser
Getting url variables without a browser
- Subject: Getting url variables without a browser
- From: Lee Noble <email@hidden>
- Date: Thu, 3 Feb 2005 09:41:44 +0000
I'm not at all familiar with AppleScript's URL handling ability and
I've only used curl to download files from fixed locations. I have a
file which I need to download on a periodic basis because the content
of it is dynamic. The method I currently use employs Firefox and a
certain amount of UIScripting to acheive its aim.
There has to be a better way. Incidentally the site I am accessing is
very windows centric and uses a stupid popup window. Even if I allow
Safari to open the popup the site download does not work with Safari at
all.
Here's my script:
tell application "Finder"
-- this block of script is just a temporary measure which allows me to
stop the download before it starts if I like
try
with timeout of 10 seconds
set action to button returned of (display dialog "Do you wish to let
the WWW XML update run?" buttons {"No", "Yes"} default button 1)
end timeout
on error
set action to "No"
end try
if action is "Yes" then
tell do_xml_update to run
end if
end tell
on fetch_url_variable_from_firefox(varToFind)
-- this handler fetches the session variable from the url once logged
in
tell application "System Events"
set theOldClipboard to the clipboard
tell process "Firefox"
tell window 1
-- select the address bar
keystroke "l" using command down
-- copy url to the clipboard
keystroke "c" using command down
delay 3
end tell
end tell
set theVariable to the clipboard as string
set oldDelims to AppleScript's text item delimiters
set variableList to {}
set AppleScript's text item delimiters to "?"
try
set theRequest to text item 2 of theVariable
on error
stop
end try
set AppleScript's text item delimiters to "&"
repeat with i from 1 to number of text items in theRequest
set variableList to variableList & {(text item i of theRequest)}
end repeat
set foundValue to false
repeat with j from 1 to number of items in variableList
set thisItem to item j of variableList
if thisItem begins with (varToFind & "=") then
set AppleScript's text item delimiters to "="
set theValue to text item 2 of thisItem
set foundValue to true
-- display dialog thisItem as string
end if
end repeat
set AppleScript's text item delimiters to oldDelims
if foundValue is false then
set theValue to "NA"
end if
end tell
return theValue
end fetch_url_variable_from_firefox
script do_xml_update
set theDateString to (do shell script "date +%d%b%Y" as string)
tell application "Firefox"
-- this request just makes sure that a session isn't still hanging
over from last time by forcing a logout
Get URL "http://the.download.url.com/index.html?FORM_ACTION=LOGOUT"
activate
delay 15 -- clumsy, I know
-- the login form is completed by keychain so no typing necessary
Get URL "javascript:window.document.forms[0].submit();"
delay 20
end tell
delay 20
-- now we are logged in I need to extract the session identifier value
which is in the new url, key PROFILE
set sessionID to fetch_url_variable_from_firefox("PROFILE")
if sessionID is "NA" or sessionID is false then
-- not necessary, just logs events using my own logger script
-- log_event("SESSION ID could not be found while running XML
download") of commonScript
stop
else
tell application "Firefox"
-- OK, here's the biggy. Since it proved impossible to manually set
form element values using javascript calls, I tried
-- using the request as a GET. Despite the form being POST, it
worked so I guess they don't care what method is used.
Get URL ("http://the.download.url.com/xml_export_file.html?PROFILE="
& sessionID &
"&form_fill=1&form_redirector=&return_form=&retained_distribution_contro
l=26&label_distribution_control=Distribution+Control&distribution_contro
l=26&retained_cde_product_category_id=4&label_cde_product_category_id=Ca
tegory&cde_product_category_id=4&retained_cde_product_sub_category_id[
]=&label_cde_product_sub_category_id[]=Subcategory&cde_product_sub
_category_id[]=1366&cde_product_sub_category_id[]=1349&cde_produ
ct_sub_category_id[]=1350&cde_product_sub_category_id[]=1351&cde
_product_sub_category_id[]=1352&cde_product_sub_category_id[]=13
53&cde_product_sub_category_id[]=1307&cde_product_sub_category_id[
]=1138&cde_product_sub_category_id[]=1356&cde_product_sub_category
_id[]=1135&cde_product_sub_category_id[]=1358&cde_product_sub_ca
tegory_id[]=1359&retained_grading_scheme[]=&label_grading_scheme
[]=Grading+Scheme&retained_grading_designator[]=&label_grading_d
esignator[]=Grading+Designator&retained_region_type=M&label_region_t
ype=Region+Type®ion_type=M&retained_region[]=&label_region[]=
Region®ion[]=31®ion[]=32®ion[]=33®ion[]=34&s
earch_button=Search")
delay 60
(*
*)
end tell
end if
tell application "System Events"
-- again, just for logging purposes
set windowName to ("Opening " & theDateString & "_CDE.xml")
-- log_event("Window Name" & windowName) of commonScript
tell process "Mozilla Firefox"
keystroke return -- dismisses a javascript dialog box
delay 5
keystroke "w" using command down -- closes download window
delay 5
keystroke return
delay 5
keystroke "w" using command down -- closes main browser window
end tell
end tell
tell application "Firefox"
quit
end tell
end script
I don't like having to use all that flaky UIScript so if there is a way
to avoid it can someone let me know.
Cheers
Lee
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden