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

This opens “Find on webpage” but I think I understand what you are trying to demonstrate.

Does that button really exist? This is how this sheet looks for me:

I haven’t really noticed before but it feels like this is not following Apple’s own UI guidelines. When using it manually, AFAIK, you have to double click on the high lighted line or press enter to open the file. It is the same in Finder.

Older versions of MacOS (I have one machine with Big Sur/Mac OS 11) have a button on this sheet but it seems to be missing in Sequoia?

Shift-Cmd-G must be pressed in the open/save dialog window.

Yes, but you can’t (?) open that with AppleScript, at least not without “hacks”.

I’m not very good at English, so maybe I didn’t fully catch what lagr meant…

But if I understood correctly, you just need to type the text in the “Move to Folder” UI and hit return, right?

I’m mainly not using Google Chrome, so I made an example and a short video using Microsoft Edge instead.

tell application "Microsoft Edge"
	activate
	tell application "System Events"
		tell application process "Microsoft Edge"
			tell front window
				tell button 1 of sheet 1 of sheet 1
					click
					keystroke "/tmp\n\n"
										keystroke return
				end tell
			end tell
		end tell
	end tell
end tell

Hope it helps! :blush:

Are you sure this should work? I had to rewrite it a bit:

tell application "System Events"
	tell process "Chrome"
		activate
		set frontWin to front window
		repeat until exists (sheet 1 of frontWin whose description is "open")
			delay 0.2
		end repeat
		tell application "System Events"
			tell application "Google Chrome" to activate
			delay 3
			keystroke "g" using {command down, shift down}
			delay 3
		end tell
		set dialogSheet to sheet 1 of frontWin
		delay 1
		set gotosheet to sheet 1 of dialogSheet
		set theElementsGS to every UI element of gotosheet
		set props1 to get properties of button 1 of gotosheet
		set value of text field 1 of gotosheet to filePath
		click button "Go" of gotosheet
	end tell
end tell

The result of this line:

set theElementsGS to every UI element of gotosheet

follows here:

get every UI element of sheet 1 of sheet 1 of window "ChatGPT - Google Chrome" of application process "Google Chrome"
		--> {
static text 1 of sheet 1 of sheet 1 of window "ChatGPT - Google Chrome" of application process "Google Chrome", 
button 1 of sheet 1 of sheet 1 of window "ChatGPT - Google Chrome" of application process "Google Chrome", 
text field 1 of sheet 1 of sheet 1 of window "ChatGPT - Google Chrome" of application process "Google Chrome", 
scroll area 1 of sheet 1 of sheet 1 of window "ChatGPT - Google Chrome" of application process "Google Chrome"}

and

set props1 to get properties of button 1 of gotosheet

returns

get properties of button 1 of sheet 1 of sheet 1 of window "ChatGPT - Google Chrome" of application process "Google Chrome"
		--> {
minimum value:missing value, 
orientation:missing value, 
position:{856, 397}, 
class:button, 
accessibility description:"Close", 
role description:"button", 
focused:false, 
title:"", 
size:{18, 18}, 
help:missing value, 
entire contents:{}, 
enabled:true, 
maximum value:missing value, 
role:"AXButton", 
value:missing value, 
subrole:missing value, 
selected:missing value, 
name:missing value, 
description:"Close"}

Are you sure that there should be a clickable button in this sheet?