7 Replies Latest reply: Oct 12, 2012 3:02 PM by Queerly
Queerly Level 1 Level 1 (0 points)

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)
  • 1. Re: Uploading a File with AppleScript via JavaScript
    twtwtw Level 5 Level 5 (4,690 points)

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

  • 2. Re: Uploading a File with AppleScript via JavaScript
    Queerly Level 1 Level 1 (0 points)

    Silly me! Some people will never learn, won't they! Here you go:

     

    https://www.google.com/doubleclick/studio/swiffy/

     

    Again, pleeding the same,old excuse. I'm not stupid, I'm just foreign.

  • 3. Re: Uploading a File with AppleScript via JavaScript
    adayzdone Level 2 Level 2 (150 points)

    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

  • 4. Re: Uploading a File with AppleScript via JavaScript
    twtwtw Level 5 Level 5 (4,690 points)

    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.

  • 5. Re: Uploading a File with AppleScript via JavaScript
    twtwtw Level 5 Level 5 (4,690 points)

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

    '/Users/myaccount/Desktop/Scrapbook/!动画.swf'

  • 6. Re: Uploading a File with AppleScript via JavaScript
    Queerly Level 1 Level 1 (0 points)

    @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.

  • 7. Re: Uploading a File with AppleScript via JavaScript
    Queerly Level 1 Level 1 (0 points)

    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.