How do you navigate a save/open dialog with AppleScript?

I have seen these two threads that touches what I want to do:

and they seem to indicate that you need to use GUI-scripting for the open/save dialogues. Is that really true??

I am trying to make Chrome upload a file. Opening the file chooser dialogue requires GUI-scripting¹ because of web related security concerns, but when the dialogue is open I expected it to be scriptable but, at least according to the threads linked above, it doesn’t seem to be the case. Can someone confirm this?

If it is not scriptable and I am left to use GUI-scripting - what is the best way to open a file?

I know about g-shift-cmd. If I list all UI elements of the sheet that appears after g-shift-cmd it has a lot of elements:

static text "/tmp/myfile.txt" of sheet 1 of sheet 1 of window "Google Chrome" of application process "Google Chrome"

button 1 of sheet 1 of sheet 1 of window "Google Chrome" of application process "Google Chrome"

text field 1 of sheet 1 of sheet 1 of window "Google Chrome" of application process "Google Chrome"

scroll area 1 of sheet 1 of sheet 1 of window "Google Chrome" of application process "Google Chrome"

The scroll area in turn has multiple levels of other UI elements.

I have tried to send a click to the button and double click to the text field and the static text, but nothing (useful) happens (sometimes the focus seems to change when I send a click).

I have never really thought about this sheet before. It feels like it is missing something - at least two buttons, Open/Goto, or something like that, and Cancel. It feels like my system has a hiccup here - no buttons??

In the context of GUI-scripting, operating on a button or other element referring its name feels like a reasonably stable solution.

How do I g-shift-cmd to a file and then open the file by emulating a (double)click on an element, referring said element by its name?

¹ Does anyone know if it is possible to launch Chrome/Safari in an “unsafe mode” so you can perform certain actions that normally are forbidden? Or other workarounds?

Yes, you have to use GUI scripting, but it’s not that hard.

First of all you have to bring the target application to the front.
Then – with GUI scripting – press ⇧⌘G, wait for the sheet to appear, set the value of the text field to the path and click the Go button.

For example

set thePath to "/tmp/myfile.txt"

activate application "Google Chrome"
tell application "System Events"
	tell window "Google Chrome" of process "Google Chrome"
		keystroke "g" using {command down, shift down}
		repeat until exists sheet 1
			delay 0.5
		end repeat
		set value of text field 1 of sheet 1 to thePath
		click button "Go" of sheet 1
	end tell
end tell

I don’t understand the reason why you have to use GUI scripting.

At the first time, Google Chrome require local access permission.
After seond time, Chrome can open local file.

set anAlias to choose file

tell application "Finder"
	set aURL to URL of anAlias
end tell

tell application "Google Chrome"
	tell window 1
		tell active tab
			set URL to aURL
		end tell
	end tell
end tell
1 Like

What is this supposed to do? What happens for me is that a choose file dialogue appears, I choose a file, Chrome activates and opens a Save dialogue, with the name of the file I selected in the Save As field, in my Download folder (I picked a file in /tmp).

I want to upload a file.

I thought you didn’t know about opening files using Google Chrome.
Uploading files via open dialog is very very strange to hear for me.
Because it’s not a safe way to do anything.

If you upload images to your WordPress blog, use XMLRPC using Applescript.
If you upload a file to Dropbox, use the REST API call using Applescript.
Uploading some files to an FTP server uses an FTP client using Applescript.

However, some older style social media or web systems may require an open dialog to upload files.

When I write an AppleScript textbook, I probably write this: “I don’t recommend operating the open/save dialog via GUI Scripting unless it’s a big deal.”

1 Like