Saving an open photo to external drive with existing name & file type?

Hi,
Newbie to scripting (though I done a lot of Excel VBA) and I am trying to do something simple. I have a photo open that I am viewing in ‘preview’ and I want to have a script to save the open file in a certain folder on my drive called “iMac documents” (an external drive) in a folder called “Nature Photos” with the same name it currently is and in the same file type as it currently is.

The best I’ve come up with is shown below patched together from various code I’ve seen on the help sites but it doesn’t seem to be working…Thanks in advance!

on savefile()
set theOutputFolder to choose folder with prompt “Select an output folder:”

set theImage to choose file of type "JPEG"

set theOutputFolder to choose folder

tell application "Image Events"
	launch
	set theImageReference to open theImage
	tell theImageReference
		
		set theImageName to name
		
		save in (theOutputFolder as string) & theImageName & ".jpg"
		close
	end tell
end tell

end savefile

Also is there a way to manually step through the code like one can do in Excel VBA to see how it works?
Thanks,
Chet

Hi, Chet. Welcome to MacScripter.

I think the reason the image isn’t being saved in your script is because it hasn’t been modified in Image Events. Your code works otherwise.

But if you just want to copy an image to another location, it’s simpler to do it directly.

on savefile()
	set theImage to choose file of type {"JPEG"}
	
	set theOutputFolder to choose folder with prompt "Select an output folder:"
	
	tell application "Finder" to duplicate theImage to theOutputFolder
end savefile

savefile()

Unfortunately, Preview’s not scriptable. But it is possible to save displayed images directly from it (in an unsightly but convenient manner) using what’s known as GUI Scripting. This sends messages to an application via its user interface rather than injecting them directly into its brain. It has to be enabled by checking “Enable access for assistive devices” in System Preferences’s “Univeral Access” pane.

set destFolder to POSIX path of ("iMac documents:Nature Photos:") -- This should be the path to your destination folder.

tell application "System Events"
	tell application process "Preview"
		-- Make sure Preview's in front to receive keystrokes.
		set frontmost to true
		-- Prior to Mac OS 10.7, the keystroke for "Save As." was Shift-Command-"s". I think it's Option-Command-"s" from Lion on, but I'm not absolutely sure. THIS NEEDS TO BE CHECKED. Shift-Command-"g" lowers the "Go to the folder:" sheet.
		if ((system attribute "sys2") < 7) then
			keystroke "sg" using {command down, shift down}
		else
			keystroke "s" using {command down, option down}
			keystroke "g" using {shift down, command down}
		end if
		-- Wait for the "Go to the folder:" sheet and set the value of its text field to the POSIX path of the destination folder. (Uses GUI Scripting.)
		tell text field 1 of sheet 1 of sheet 1 of (first window whose subrole is "AXStandardWindow")
			repeat until (it exists)
				delay 0.2
			end repeat
			set value to destFolder
		end tell
		-- OK the "Go to the folder:" and "Save As:" sheets respectively.
		keystroke return
		keystroke return
	end tell
end tell

A similar script which doesn’t require “Enable access for assistive devices” is the one below. The only difference is that it laboriously types the folder path into the text field instead of just setting the field’s value.

set destFolder to POSIX path of ("iMac documents:Nature Photos:")

tell application "System Events"
	set frontmost of application process "Preview" to true
	if ((system attribute "sys2") < 7) then
		keystroke "sg" using {command down, shift down}
	else
		keystroke "s" using {command down, option down}
		keystroke "g" using {command down}
	end if
	-- Type the POSIX path of the destination folder into the text field.
	keystroke destFolder
	-- OK the "Go to the folder:" and "Save As:" sheets respectively.
	keystroke return
	keystroke return
end tell

By the way, these fora have [applescript] and [/applescript] tags which format posted code as above. You can either write them into your posts or select the code in the posting window and click the “Applescript” button just above it.