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

Uploading a File with AppleScript via JavaScript

Hi everybody,


being new to AppleScripting and JavaScripting I'm faced with the following problem:


Is it possible to make AppleScript to input the path for a file to be uploaded via JavaScript? If yes, what should be the correct Path Format and how to wrap it up the JavaScript action into AppleScript? I've tried a few comands but to no success.



On the webpage in question, there's a "Choose" button, that is defined as follows:


<input type=​"file" class=​"gwt-FileUpload" name=​"swfFile">​


I've been trying to use the following syntax:


do JavaScript "document.getElementsByName('swfFile')[0].value('Macintosh HD:Users:myaccount:Desktop:Scrapbook:!动画.swf')" in document 1


as well as this one, where the Path is definitelly understood by shellscript:


do JavaScript "document.getElementsByName('swfFile')[0].value('/Users/myaccount/Desktop/Scrap book/!动画.swf')" in document 1


The name of the file is (and must remain) in chinese, but for testing purposes I have also tried files with english names, without success.


Currently I'm using a solution with JavaScript comand:

do JavaScript "document.getElementsByName('swfFile')[0].click()" in document 1

to click the finder window to browse for the file, and manipulating keystrokes by using System Events to find the right file, but it's not infallible with a number of files and it's hardly an ellegant sollution.


Any idea what I'm doing wrong?


Cheers!

MacBook Pro, Mac OS X (10.7.4)

Posted on Oct 12, 2012 12:08 PM

Reply
7 replies

Oct 12, 2012 12:55 PM in response to Queerly

I can't really test this without uploading a junk file to the server, but I think you set the file button value first, then submit the form element like in your other thread:


tell application "Safari"

do JavaScript "document.forms[0].elements['swfFile'].value=" & "'Macintosh HD:Users:myaccount:Desktop:Scrapbook:!动画.swf'" in document 1

do JavaScript "document.forms[0].submit()"

end tell


I made it a concatenation because I assume you'll want a variable to go there eventually. If that doesn't work, try using document.forms[0].elements['swfFile'].click() rather than the submit() line.

Oct 12, 2012 2:36 PM in response to twtwtw

@twtwtw


the do JavaScript "document.forms[0].submit()" command didn't work, so I kept the original way of submitting by clicking the button. The submitting part works, but not the input file part.


That's the code I used


tell application "Safari"

do JavaScript "document.getElementById('gwt-uid-13').value='on'" in document 1

do JavaScript "document.forms[0].elements['swfFile'].value=" & "'" & SWFFilePath & "'" in document 1

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

delay 1

end tell


with SWFFilePath set as the POSIX path as well as the default file format

Macintosh HD:Users:myaccount:Desktop:Scrapbook:!动画.swf'

but it did not work.


If you've got the time, feel free to upload an .swf. The service is free and the server delats uploaded files after 15 min. (if you don't have any, you may try an empty .txt file instead - it won't get converted, but the point is just to get it into the input box).


In the meantime, I'll have a look at adayzdone's suggestion. It's not perfect (the downside being that it requires the window to be active, so I can't run the script in the background or with my screensaver & password on), but it's still more reliable than my sollution with navigating arrowkeyes.

Oct 12, 2012 3:02 PM in response to adayzdone

thx adayzdone for your suggestion,


Your original code (provided you'd add keystroke return twice, to confirm the choice) would work perfectly, if the filename was in English. It won't work this way with Chinese characters, because of the way Chinese Input works. However, I found a way arround the problem by using the clipboard and ended up with the following code:


set the clipboard to SWFFilePath

activate application "Safari"

tell application "System Events" to tell process "Safari"

keystroke "G" using {command down, shift down}

delay 1

keystroke "v" using {command down}

delay 1

keystroke return

delay 1

keystroke return

delay 1

end tell


It gives me a much better alternative to my option with making System Events pressing arrow keyes, though I'd still prefer a JavaScript-based sollution, because it doesn't require the window to be active. I believe there must be a way to input the file via JavaScript. In any case, I'll remember to press the "This helped me" button, before this thread is closed.

Uploading a File with AppleScript via JavaScript

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