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

Click a button in Safari using Applescript

Hi,


I've been dabbling in Applescript for the last few weeks and while I can make it perform some pretty complex workflows, I still have virtually no knowledge of how to make it interact with JavaScript, and it's an essencial part of a new, larger applescript I'm working on to streamline a mundane task I'll need to perform a few hundred times.


In particular, with the page "https://www.google.com/doubleclick/studio/swiffy/" already opened in Safari, I'd like AppleScript to:


1) Click the "Browse" button.


2) Browse to find the file in question (FilePath)


3) Check the Checkbox.


4) Then click the "Upload and convert" button to start the conversion.


Could anyone tell me how to interact with these buttons? I'd appreciate specific solutions, since i can make neither head nor tails of JavaScript.


Cheers!

MacBook Pro, Mac OS X (10.7.4)

Posted on Sep 21, 2012 3:21 PM

Reply
4 replies

Sep 22, 2012 3:32 PM in response to twtwtw

Hi twtwtw


thx for the reply, but could you be more specific as to which command I should execute. How to get the id of the element? If I inspect the element with Firefox's WebInspector I get:


input.gwt-FileUpload

input#gwt-uid-13

button.gwt-SubmitButton


are these the IDs you were talking about? If yes, how do I construct the syntax of the javascript comand? Also, I have no idea what DOM means. Sorry, I have no experience of JavaScript.

Sep 30, 2012 5:07 AM in response to Queerly

Hi again,


since I still haven't found the solution to the problem, I've tried to find some answers on my own. I've tried to use .getElementById function, but it only works for the little checkbox on the page, since I can't get the IDs for the other elements. This is how much working code I've come up with so far:


tell application "Safari"

activate

set URL of document 1 to "https://www.google.com/doubleclick/studio/swiffy/"

delay 4

do JavaScript "document.getElementById('gwt-uid-13').click()" in document 1

delay 1

end tell


The problem is, I don't think the other elements have IDs. So, after some research I found that you should be able to manipulate them with other functions such as, getElementsByTagName, but it didn't work for me. I added the following line to the script:


do JavaScript "document.getElementById('gwt-uid-13').click()" in document 1


but it does nothing.


Below you can find the two elements as they embedded in the page:


First Element to open the file finder info to upload the file:

<form target=​"FormPanel_SwiffyFrontend_1" enctype=​"multipart/​form-data" method=​"post" action=​"/​doubleclick/​studio/​swiffy/​upload">​<table cellspacing=​"0" cellpadding=​"0">​<tbody>​<tr>​<td align=​"left" style=​"vertical-align:​ top;​ ">​<input type=​"file" class=​"gwt-FileUpload" name=​"swfFile">​


Second Element to submit the file:

<td align=​"left" style=​"vertical-align:​ top;​ ">​<table cellspacing=​"0" cellpadding=​"0">​<tbody>​<tr>​<td align=​"left" style=​"vertical-align:​ top;​ ">​<button type=​"submit" class=​"gwt-SubmitButton">​Upload and convert​</button>​


I'm really desperate for some help here. I just don't understand what I'm supposed to do. More specific and less general solutions would be very much appreciated.


Cheers!

Oct 12, 2012 2:47 AM in response to Queerly

I found the sollution on my own:


putting [0] before the .click() action helped. I'm not sure, not a JavaScript expert here, but the way I understand it the document.getElementsByClassName Method returns a whole array of elements and you have to tell it which on it is supposed to be. In the end, I used the following syntax.


do JavaScript "document.getElementsByClassName('gwt-SubmitButton')[0].click()" in document 1



Maybe this will help some other helpless JavaScript Dummies like myself out there.

Click a button in Safari using Applescript

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