Skip navigation

Uploading a File with AppleScript via JavaScript

884 Views 7 Replies Latest reply: Oct 12, 2012 3:02 PM by Queerly RSS
Queerly Calculating status...
Currently Being Moderated
Oct 12, 2012 12:08 PM

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?



MacBook Pro, Mac OS X (10.7.4)
  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    Oct 12, 2012 12:20 PM (in response to Queerly)

    again, it would be useful to have a link to the page you're working with.

  • adayzdone Level 2 Level 2 (150 points)
    Currently Being Moderated
    Oct 12, 2012 12:47 PM (in response to Queerly)

    Once the dialog is raised, try:


    set myFile to "/path/to/your/file.pdf"


    activate application "Safari"

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

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

        delay 2

        keystroke myFile

    end tell

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    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.

  • twtwtw Level 5 Level 5 (4,585 points)
    Currently Being Moderated
    Oct 12, 2012 12:57 PM (in response to Queerly)

    whoops, sorry, you don't want to use the colon-delimited path.  use a POSIX path instead:



More Like This

  • Retrieving data ...

Bookmarked By (0)


  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.