Duplicate, Rename and Modify

Hi All -

My goal is to make an AppleScript that will do some image manipulations. What I need to do is duplicate the original image file, rename it to the same name with the suffix “_edited” before the extension, and then put that in a variable so that I can manipulate it. This is what I have so far:


set this_file to choose file without invisibles
try
	tell application "Finder" to duplicate this_file to desktop with replacing
	-- NEED TO RENAME FILE HERE AND ASSIGN TO VARIABLE this_file
	tell application "Image Events"
		launch
		set this_image to open this_file
		copy dimensions of this_image to {W, H}
		if W is less than H then
			set the scale_length to (H * 640) / W
			set the scale_length to ¬
				round scale_length rounding as taught in school
		else
			set the scale_length to (W * 640) / H
			set the scale_length to ¬
				round scale_length rounding as taught in school
		end if
		scale this_image to size scale_length
		crop this_image to dimensions {640, 360}
		save this_image with icon
		close this_image
	end tell
on error error_message
	display dialog error_message
end try

Thanks in adanve

Hi,

replace

 tell application "Finder" to duplicate this_file to desktop with replacing

with


tell application "Finder"
		set this_file to (duplicate this_file to desktop with replacing) as alias
		set {name:Nm, name extension:Ex} to this_file
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set name of this_file to Nm & "_edited." & Ex
	end tell

Awesome!! THANKS! :smiley:

Last question, how would I also make this script work as a droplet in addition to the current way?

Hi,

Finder(ridu), but to late … :wink:

set this_file to choose file without invisibles
set TargetDirectory to path to desktop as text

tell application "Finder"
	set oldName to name of this_file --filename
	set theSuffix to name extension of this_file --suffix
	set newName to text 1 thru -5 of oldName & "_edited." & theSuffix -- create a new name
	duplicate this_file to folder TargetDirectory with replacing
	set name of file oldName of folder TargetDirectory to newName --name the duplicated file
	set NewFile to (file newName as alias) --your alias to work with in image events
end tell


Have a nice evening

Hans

you can use this both as applet or droplet


on run
	open (choose file with multiple selections allowed)
end run

on open theseItems
	repeat with anItem in theseItems
		processFile(anItem)
	end repeat
end open

on processFile(aFile)
	try
		copy aFile to this_file
		tell application "Finder"
			if class of this_file is folder then return
			set this_file to (duplicate this_file to desktop with replacing) as alias
			set {name:Nm, name extension:Ex} to this_file
			if Ex is missing value then set Ex to ""
			if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
			set name of this_file to Nm & "_edited." & Ex
		end tell
		-- NEED TO RENAME FILE HERE AND ASSIGN TO VARIABLE this_file
		tell application "Image Events"
			launch
			set this_image to open this_file
			copy dimensions of this_image to {W, H}
			if W is less than H then
				set the scale_length to (H * 640) / W
				set the scale_length to ¬
					round scale_length rounding as taught in school
			else
				set the scale_length to (W * 640) / H
				set the scale_length to ¬
					round scale_length rounding as taught in school
			end if
			scale this_image to size scale_length
			crop this_image to dimensions {640, 360}
			save this_image with icon
			close this_image
		end tell
	on error error_message
		display dialog error_message
	end try
end processFile



note, that scale to size scales automatically to the maximum value (width or height)

thanks again!