Selecting files for input Safari applescript

Solution 1:

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