Selecting files for input Safari applescript

applescriptautomatorsafari

I am trying to automate using Applescript uploading of files to a website. I have gotten to nearly the last step, but can't seem to get a Finder (file chooser) window to respond to scripts.

Here is what the site looks like (i can't give you the actual site, because it is a secure medical record).

enter image description here

The process to upload files is to hit "Add Images" which I can do in Apple/Javascript and it brings up a Finder (file chooser) window like this (again I can only show part because of privacy issues). There are standard "choose" and cancel" buttons on the bottom.

enter image description here

I can't find a way to get this Finder window to respond to Applescript. I have been trying things like:

tell application "System Events"
    keystroke tab
end tell

and numerous others using Finder etc.

Any ideas on how to pick a file out of this window? Or get to this window? Or even find the properties of this window?

Thanks.

Best Answer

It is not a "Finder" window. It is owned by Safari

You have not made clear how you know which file to choose. So I assume you will be partially hard coding it into the script.

This example assumes you are able to form a path string to the file.

This example is also written to click and add an image to an Answer on one of these Ask-different pages.

You already know how to click buttons with the Applescript/js

But you can use keystrokes to enter the command G+cmd+shift which will open a 'Go to..' sheet in the 'Choose' dialogue window.

You then keystroke your path to your file.

The next two buttons 'Go' and 'Choose' are the default ones so you can just keystroke Return to hit them.

(This image was uploaded using the script) enter image description here


activate application "Safari"
tell application "Safari"
    tell document 1

        do JavaScript "document.getElementsByClassName('wmd-button')[5].click()"
        delay 1

        do JavaScript "document.getElementById('filename-input').click()"
    end tell

end tell
tell application "System Events"
    keystroke "G" using {command down, shift down}
    delay 1
    keystroke "~/Desktop/image/image.png"
    delay 1
    keystroke return

    delay 1
    keystroke return

    delay 1

end tell

tell application "Safari"
    tell document 1
        do JavaScript "document.getElementById('add-picture').click()"
    end tell
end tell