Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Need AppleScript (and probably JavaScript) help

How do I write an AppleScript that tells safari to load a page, then looks for a hyperlink and clicks it?


I am trying to set up a large number of accounts in our domain on Google Apps for Education. I have created the accounts, but I need to log into each one, accept the ToS, then open the newest email in the inbox, and click a link in the email.


I know the CSV part is working because I am recycling code from the EnterpriseiOS site. I just need to know how to get the script to click the links I described above.


The links are not necessarily buttons. Sometimes they are simple a hrefs. Other times they are parts of a form. I am looking for a way to match the string (or part of it) of the link. Example: if you log off of gmail, and get back in, there is a link at the bottom that says "Sign in with a different account" - I'd like to be able to have the script click that link. Using Chrome's inspector, I see that it is an a id="account-chooser-link" href="some url" - How do I get the script to click that link?


Thanks in advance - AppleScript is not that hard to figure out, but JavaScript makes my brain hurt.

iMac, OS X Mavericks (10.9.4)

Posted on Sep 15, 2014 7:01 PM

Reply
11 replies

Oct 15, 2014 8:59 AM in response to MacDude72

I think this will do what you want. (Assumes you have loaded the signed out page as you describe in the frontmost document):


tellapplication "Safari"

setlinkCountto (do JavaScript "document.links.length" indocument 1)

repeatwithlinkNofrom 0 to (linkCount - 1)

settheLinkto (do JavaScript "document.links[" & linkNo & "].href" indocument 1)

iftheLinkcontains "AccountChooser" then

setURLofdocument 1 totheLink

exitrepeat

endif

endrepeat

endtell

Apr 13, 2015 8:08 AM in response to etresoft

Well, I attempted to post a Python script here that finds the account-chooser-link class, and then prints the adjacent href link contents. Using the advanced editor, and two different (plain, Python) syntax highlighting templates resulted in the red banner blocked from posting the code. Thought police prevail.


The business end of this script was nine lines of code, though 36 lines in total to make it auto-adapt from Python 2.7.5 through 3.4.3, and perform error handling.


User uploaded file

Apr 11, 2015 8:03 PM in response to etresoft

I am using standard Python libraries in my example that can be used to retrieve info from websites, or transmit data back to CGI backends. Wanted to keep it as simple as possible without using add on modules.


There are Python web frameworks (Django, Flask, Bottle, Pyramid, etc.) that you may be thinking about. It is possible to combine Django with Asynchronous JavaScript for a client-server web solution. More on Python web frameworks.

Apr 13, 2015 11:22 AM in response to MacDude72

I trimmed the Python code down to its essentials and below is how you would use it in an AppleScript. You would replace the search url in the examplewith the path to the website you want to scrape for account-chooser-link. The AppleScript function containing the Python code will return either one or more instances of the href link associated with the provided site, or the ‘None’ string because the search string was not found, or there were errors.


set myURL to "http://localhost/~user" as text


set hrefList to gethrefbyid(myURL)

if hrefList does not contain "None" then log (hrefList as text)

return


on gethrefbyid(aURL)


return do shell script "python <<'EOF' - " & aURL & "


from urllib2 import Request, urlopen, URLError, HTTPError

import re

import sys


this_url = \"\".join(sys.argv[1:]).decode('utf-8')


def main():

try:

req = Request(this_url)

website = urlopen(req)

page = website.read().decode('utf-8')

found = re.findall('account-chooser-link.+?href=\"(.+?)\"', page)

for href in found:

print(''.join(href))

except (URLError, HTTPError) as e:

print('None')


if __name__ == '__main__':

sys.exit(main())

'EOF'"


end gethrefbyid

Need AppleScript (and probably JavaScript) help

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.