Automate Proxy Logon with Applescript
Wednesday Apr 23, 2008.
I download journal articles from home pretty often, which requires me to go through the library’s proxy server, a web authentication process that takes me through 3 pages to input a username and a password. Eventually my utility for future laziness trumped my utility for current laziness, so I wrote an applescript to automate the input process. This is my first try with applescript, and so far it’s been very easy and powerful.
In case I get berated by the librarians for having a potential security hole, I used the Mac’s keychains to manage the username and password. So now you can input the password once instantaneously rather than wait, input, wait again, input again, wait yet again, click...., you get the point.
--get username and password
tell application "Keychain Scripting"
set myKey to first key of current keychain whose name is "FILL IN KEYCHAIN NAME HERE”
set user to account of myKey
set pass to password of myKey
end tell
--set constants
set proxy to "PROXY URL"
set success to "Proxy Server login successful"
set prompt to "TITLE OF WEBPAGE PROMPTING FOR PASSWORD"
tell application "Safari"
activate
--get title and reference to current document
set title to (name of window 1) as string
set login to (document of window 1)
--redirect to proxy if not already there
if title is not equal to prompt then
set cururl to (URL of document of window 1) as string
set newurl to proxy & cururl
set (URL of document of window 1) to newurl
delay 2
--get title and reference to current document
set title to (name of window 1) as string
set login to (document of window 1)
end if
if title is equal to success then -- click to continue
set thelink to do JavaScript "document.links[0].href" in login
set (URL of login) to thelinkq
else if title is equal to prompt then --input userid & password
do JavaScript "document.userid.UserID.value = '" & user & "';" in login
do JavaScript "document.userid.submit();" in login
delay 3
do JavaScript "document.password.Password.value = '" & pass & "';" in login
do JavaScript "document.password.submit();" in login
--click to continue
delay 2
set login to (document of window 1)
set thelink to do JavaScript "document.links[0].href" in login
set (URL of login) to thelink
else
display dialog "Error: Perhaps Internet connection is bad?"
end if
end tell