how can I set the path of a 'save as' dialog box

G’day from Oz

I know I’ve seen it on these pages before, but have spent several hours fruitlessly searching.

Can anyone tell me please how to fill in a save path for a file in a ‘Save As’ dialog box? I know how to keystroke in the name, but entering a path eludes me.

TIA

Regards

Santa

You need to save the path as an alias:
Path taken from front most window:

tell application "Finder"
	activate
	set source_folder to (folder of the front window) as alias
	set file_path to ((source_folder as string) &"File Name") as alias
	end tell

or in a folder to be selected by user:

tell application "Finder"
	set fldr to (get selection) as alias
	set file_path to ((fldr as string) &"File Name") as alias
end tell

or if on desktop:

tell application "Finder"
	set fldr to path to desktop folder as alias
	set file_path to ((fldr as string) & "File Name") as alias
end tell

I’m a little confused by your question - the mention of using ‘keystroke’ implies that you’re trying to automate the naming of the file, even though the use of ‘Save As’ would imply you’re letting the user do it.

Therefore I’m going out on a limb and I"m going to guess that you want the user to name the file, but that you want the Save As dialog to be in a default location when it opens.

If that’s the case, ‘choose file name’ has a ‘default location’ parameter, as well as a ‘default name’:

set file2save to (choose file name default location (path to desktop) default name "yourfile.txt")

Note this won’t actually save your file - it will pass back a file reference defining the filename/path the user defined. It’s up to you to use that to save the data in the specified location. If you’re scripting some other application, most support some kind of ‘in’ parameter to their save command.

G’day Gents.

Sorry for the confusion; I want to GUI script the save position in the ‘Save As’ dialog box.

I’m using illustrator, and want the file to save in a different position from where it opened. To make matters worse, at present they default to the desktop.

I have to use the dialog box for a couple of reasons, so I want to enter the save position path into the dialog box, so the user just picks the options they want in the box, then hits return. No navigating to the folder.

I’ve seen it somewhere here on these pages in the past, but did not bookmark it. From memory it involved sending a “/” keystroke to the path box. I’ve tried that with no success.

Regards

Santa

I’ve managed to find out that if I deselect the entry for the Save As name, then hit slash(/), a pop up sheet drops down and allows me to enter a folder path.

Problem is, I can’t duplicate deselecting the name entry using GUI scripting. Manually, I deselect the entry by clicking on the ‘Devices’ column of the navigation box. None of the following work however.

Can anyone tell me how to ‘deselect’ the entry point (the cursor) in the name entry please.

Regards

Santa


tell application "System Events" to tell process "Adobe Illustrator"
	set frontmost to true -- activates Illustrator
	click menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1
	delay 0.2
	tell window "Save As"
		keystroke "test"
		set filesavepath to "TAZ.VOL1/JOBS/31568/3-ENGRAVE READY"
		if value of checkbox 1 is 0 then click checkbox 1-- opens navigation dialog
	end tell
	set selected of text field 1 of window "Save As" to false -- doesn't work
	select row 4 of outline 1 of scroll area 1 of splitter group 1 of group 2 of window "Save As" -- selects row, but doesn't deselect name entry point
	select (rows of outline 1 of scroll area 1 of splitter group 1 of group 2 of window "Save As" whose value is "Leopard") -- doesn't do anything at all
	try
		tell static text "Leopard" of row 4 of outline 1 of scroll area 1 of splitter group 1 of group 2 of window "Save As"
			set {xPosition, yPosition} to position
			set {xSize, ySize} to size
		end tell
		-- modify offsets if hot spot is not centered:
		click at {xPosition + (xSize div 2), yPosition + (ySize div 2)} --doesn't work
	end try
end tell

with GUI scripting do this in the dialog box:

press ⇧⌘G
wait for the sheet
“keystroke” the path (it must be a POSIX path!)
press OK

G’day Stefan.

Thanks for that, it brings up the dialog box fine.

Unfortunately there is still something wrong with the way my script works. I’m entering the path as a POSIX formatted path, but the ‘Save As’ dialog box does not go to the folder. I’ve tried with and without preceeding and trailing slashes, but it still doesn’t want to recognize the entry, and won’t show the path to the folder.

Anyone got any advice on what i might be doing wrong please? Note that TAZ.VOL1 is an external volume, but I’ve unsuccessfully tried the routine on the boot volume as well.

Regards

Santa



tell application "System Events" to tell process "Adobe Illustrator"
	set frontmost to true -- activates Illustrator
	click menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1
	delay 0.2
	tell window "Save As"
		keystroke "test"
		set filesavepath to "/TAZ.VOL1/JOBS/31568/3-ENGRAVE READY/"
		if value of checkbox 1 is 0 then click checkbox 1
		key down shift
		key down command
		keystroke "g"
		key up shift
		key up command
		delay 0.4
		keystroke filesavepath
		delay 0.2
		click button "Go" of sheet 1
	end tell
	
end tell

Fixed…

Obviously just creating a string in the form of a POSIX path isn’t good enough. Why I wonder?


tell application "System Events" to tell process "Adobe Illustrator"
	set frontmost to true -- activates Illustrator
	click menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1
	tell application "Finder"
		set filesavepath to POSIX path of "TAZ.VOL1:JOBS:31568:3-ENGRAVE READY"
	end tell
	delay 0.2
	tell window "Save As"
		keystroke "test"
		if value of checkbox 1 is 0 then click checkbox 1
		key down shift
		key down command
		keystroke "g"
		key up shift
		key up command
		delay 0.4
		keystroke filesavepath
		delay 0.2
		click button "Go" of sheet 1
	end tell
	
end tell


The POSIX path of “TAZ.VOL1:JOBS:31568:3-ENGRAVE READY” is “/Volumes/TAZ.VOL1/JOBS/31568/3-ENGRAVE READY”. That’s why the first approach didn’t work.

Be careful with the key down command. If your script errors after a key down but before you set it to “up” again, the setting stays after the script is finished running! Very awkward to fix as you have to navigate around as if you are holding the command key down or the shift key.

The safer approach is to apply it to each keystroke as needed:

keystroke "g" using {command down, shift down}

the most reliable way to avoid timing problems is to wait for certain UI elements

tell application "System Events" to tell process "Adobe Illustrator"
	set frontmost to true -- activates Illustrator
	click menu item "Save As..." of menu 1 of menu bar item "File" of menu bar 1
	set filesavepath to POSIX path of "TAZ.VOL1:JOBS:31568:3-ENGRAVE READY"
	repeat until exists window "Save As"
		delay 0.5
	end repeat
	tell window "Save As"
		keystroke "test"
		if value of checkbox 1 is 0 then click checkbox 1
		keystroke "g" using {command down, shift down}
		repeat until exists sheet 1
			delay 0.5
		end repeat
		tell sheet 1
			keystroke filesavepath
			click button "Go"
		end tell
	end tell
end tell

Note: telling the Finder to get a POSIX path is useless.

G’day, and thank you Gentlemen; the script has been duly amended.

Regards

Santa