trouble passing file to photoshop

Hello MacScripters,

I have two versions of the same script - one accepts folders dropped on it, one accepts files. This is pretty silly, so I’m trying to get a script to process either a folder or files, depending what’s dropped on it.

I found this script and it’s perfect to start with, but I’m having a bit of trouble at the point where I add in the photoshop business.

property type_list : {"JPG", "TIFF", "GIFf"} -- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property extension_list : {"jpg", "jpeg", "tif", "tiff", "gif"} -- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"}
property Exclude_list : {"EPS", "PDF", "PNG", "AI", "PSD"}
property ExcludeExtension_list : {"eps", "pdf", "png", "ai", "psd"}
property typeIDs_list : {} -- eg: {"public.jpeg", "public.tiff", "public.png"}
-- This droplet processes files dropped onto the applet 
on open these_items
	repeat with i from 1 to the count of these_items
		set this_item to item i of these_items as string
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else
			try
				set this_extension to the name extension of item_info
			on error
				set this_extension to ""
			end try
			try
				set this_filetype to the file type of item_info
			on error
				set this_filetype to ""
			end try
			try
				set this_typeID to the type identifier of item_info
			on error
				set this_typeID to ""
			end try
			if (folder of the item_info is false) and (alias of the item_info is false) and ¬
				((this_filetype is in the type_list) or (this_extension is in the extension_list) or ¬
					(this_typeID is in typeIDs_list)) then
				process_item(this_item)
			end if
		end if
	end repeat
end open

-- this sub-routine processes folders 
on process_folder(this_folder)
	set these_items to list folder this_folder without invisibles
	repeat with i from 1 to the count of these_items
		set this_item to alias ((this_folder as Unicode text) & (item i of these_items))
		set the item_info to info for this_item
		if folder of the item_info is true then
			process_folder(this_item)
		else
			try
				set this_extension to the name extension of item_info
			on error
				set this_extension to ""
			end try
			try
				set this_filetype to the file type of item_info
			on error
				set this_filetype to ""
			end try
			try
				set this_typeID to the type identifier of item_info
			on error
				set this_typeID to ""
			end try
			if (alias of the item_info is false) and ((this_filetype is in the type_list) or ¬
				(this_extension is in the extension_list) or (this_typeID is in typeIDs_list)) then
				process_item(this_item)
			end if
		end if
	end repeat
end process_folder

-- this sub-routine processes files 
on process_item(this_item)
	-- NOTE that the variable this_item is a file reference in alias format 
	tell application "Adobe Photoshop CC"
		open this_item showing dialogs never
	end tell
end process_item

I can see that it’s because the variable this_item is an alias, but I can’t figure out what to do with it. Once I’ve got that sorted I can add in the rest of the photoshop business. I think!

Thanks in advance, I am really just a copy and paste scripter, so go easy on me. :wink:

Model: iMac
AppleScript: 2.5.1
Browser: Safari 537.71
Operating System: Mac OS X (10.8)

Photoshop’s dictionary can tell you what kind of data it expects with the open command.

Here is a script that I have used in the past to process images in photoshop that might help you.

It has been a while since I have run it (2008 I think), but as I recall it handles both files and folders dropped on it. First it builds a list of files and folders, then for every folder (and it’s nested folders) it gets a list of files and processes them then goes on to process the list of files.

I think I might have some other similar routines laying around as well…


property ImageTypes : {"Thumbnail", "Watermarked Image", "Comp Image"}
property FileSizes : {thumbnail:{prefixName:"gallery_", res:72, MaxSize:175}, watermarked:{prefixName:"", res:72, MaxSize:500}, comp:{prefixName:"sample_", res:72, MaxSize:750}}
property WaterMarkFile : ""
property DonePath : ""
--property MyTimer : {startTime:0, endTime:0, fileCount:0}

on open DroppedItems
	CheckSettings()
	set TheFiles to {}
	set TheFolders to {}
	set PreviewTypes to choose from list ImageTypes with title "Preview Types" with prompt "Choose the preview types to create." OK button name "Process Files" with multiple selections allowed without empty selection allowed
	--set startTime of MyTimer to time of (current date)
	repeat with AnItem in DroppedItems
		if folder of (info for AnItem) then
			set end of TheFolders to AnItem
		else
			set end of TheFiles to AnItem
		end if
	end repeat
	repeat with AFolder in TheFolders
		tell application "Finder"
			set FileList to (every file of AFolder whose name extension is "jpg") as alias list
			set FolderName to name of AFolder
			set NewFolder to (DonePath & FolderName & ":") as string
			if not (exists folder NewFolder) then
				make new folder at alias DonePath with properties {name:FolderName}
			end if
		end tell
		ProcessFiles(FileList, NewFolder, PreviewTypes)
	end repeat
	set FileList to {}
	tell application "Finder"
		repeat with AFile in TheFiles
			if name extension of AFile is "jpg" then copy AFile to end of FileList
		end repeat
	end tell
	if FileList is not {} then ProcessFiles(FileList, NewFolder, PreviewTypes)
	activate me
	(* set endTime of MyTimer to time of (current date)
	set elapsedTime to (endTime of MyTimer) - (startTime of MyTimer)
	if elapsedTime < 60 then
		set elapsedTime to (elapsedTime as string) & " Seconds to process " & fileCount of MyTimer & " Files."
	else
		set elapsedTime to ((elapsedTime / 60) as string) & " Minutes to process " & fileCount of MyTimer & " Files."
	end if *)
	display dialog "Complete" --elapsedTime
	--set MyTimer to {startTime:0, endTime:0, fileCount:0}
end open

on run
	CheckSettings()
	set TheFolders to choose folder with prompt "Select the Folders to process." with multiple selections allowed without showing package contents
	--set startTime of MyTimer to time of (current date)
	set PreviewTypes to choose from list ImageTypes with title "Preview Types" with prompt "Choose the preview types to create." OK button name "Process Files" with multiple selections allowed without empty selection allowed
	repeat with AFolder in TheFolders
		tell application "Finder"
			set FileList to (every file of AFolder whose name extension is "jpg") as alias list
			set FolderName to name of AFolder
			set NewFolder to (DonePath & FolderName & ":") as string
			if not (exists folder NewFolder) then
				make new folder at alias DonePath with properties {name:FolderName}
			end if
		end tell
		ProcessFiles(FileList, NewFolder, PreviewTypes)
	end repeat
	activate me
	(* set endTime of MyTimer to time of (current date)
	set elapsedTime to (endTime of MyTimer) - (startTime of MyTimer)
	if elapsedTime < 60 then
		set elapsedTime to (elapsedTime as string) & " Seconds to process " & fileCount of MyTimer & " Files."
	else
		set elapsedTime to ((elapsedTime / 60) as string) & " Minutes to process " & fileCount of MyTimer & " Files."
	end if *)
	display dialog "Complete" --elapsedTime
	--set MyTimer to {startTime:0, endTime:0, fileCount:0}
end run

to CheckSettings()
	if WaterMarkFile is "" then set WaterMarkFile to (choose file with prompt "Choose a file to use as the watermark." of type {"8BPS"} without multiple selections allowed) as string
	tell application "Finder" to if not (exists file WaterMarkFile) then set WaterMarkFile to (choose file with prompt "Choose a file to use as the watermark." of type {"8BPS"} without multiple selections allowed) as string
	if DonePath is "" then set DonePath to (choose folder with prompt "Choose a folder to place the completed files in.") as string
	tell application "Finder" to if not (exists folder DonePath) then set DonePath to (choose folder with prompt "Choose a folder to place the completed files in.") as string
end CheckSettings

to ProcessFiles(FileList, DoneFolder, PreviewTypes)
	tell application "Adobe Photoshop CS4"
		with timeout of 300 seconds
			set display dialogs to never
			activate
			open file WaterMarkFile showing dialogs never
			set WaterMarkDoc to name of current document
			set ruler units of settings to pixel units
			repeat with AFile in FileList
				--set fileCount of MyTimer to (fileCount of MyTimer) + 1
				open AFile showing dialogs never
				set File2 to name of current document
				tell document File2
					set DocHeight to height
					set DocWidth to width
					--check for RGB
					if mode is not RGB then change mode to RGB
					--check for resolution s/b 72 dpi
					if resolution is not 72 then resize image resolution 72 resample method none
					if PreviewTypes contains "Comp Image" then
						--resize to comp size
						if DocHeight > DocWidth then
							resize image height (MaxSize of comp of FileSizes) resample method bicubic sharper
						else
							resize image width (MaxSize of comp of FileSizes) resample method bicubic sharper
						end if
						set DocHeight to height
						set DocWidth to width
						--save comp image
						save in (DoneFolder & prefixName of comp of FileSizes & File2) as JPEG with options {quality:8} with copying
					end if
					if PreviewTypes contains "Watermarked Image" then
						if PreviewTypes does not contain "Comp Image" then
							if DocHeight > DocWidth then
								resize image height (MaxSize of watermarked of FileSizes) resample method bicubic sharper
							else
								resize image width (MaxSize of watermarked of FileSizes) resample method bicubic sharper
							end if
							set DocHeight to height
							set DocWidth to width
							--resize to watermark size if needed
						else if (MaxSize of watermarked of FileSizes) is not (MaxSize of comp of FileSizes) then
							if DocHeight > DocWidth then
								resize image height (MaxSize of watermarked of FileSizes) resample method bicubic sharper
							else
								resize image width (MaxSize of watermarked of FileSizes) resample method bicubic sharper
							end if
							set DocHeight to height
							set DocWidth to width
						end if
					end if
				end tell
				if PreviewTypes contains "Watermarked Image" then
					set current document to document WaterMarkDoc
					duplicate layer "Logo" of document WaterMarkDoc to document File2
					set current document to document File2
				end if
				tell document File2
					if PreviewTypes contains "Watermarked Image" then
						set LogoBounds to bounds of layer "Logo"
						set LogoHeight to (item 4 of LogoBounds) - (item 2 of LogoBounds)
						set LogoWidth to (item 3 of LogoBounds) - (item 1 of LogoBounds)
						translate layer "Logo" delta x ((DocWidth / 2) - (LogoWidth / 2) + 4) delta y ((DocHeight / 2) - (LogoHeight / 2))
						scale layer 1 horizontal scale ((DocWidth - 20) / LogoWidth) * 100 vertical scale ((DocWidth - 20) / LogoWidth) * 100 anchor position middle center
						--save watermark image
						save in (DoneFolder & prefixName of watermarked of FileSizes & File2) as JPEG with options {quality:8} with copying
						delete art layer "Logo"
					end if
					if PreviewTypes contains "Thumbnail" then
						if DocHeight > DocWidth then --resize to thumbnail size
							resize image height (MaxSize of thumbnail of FileSizes) resample method bicubic sharper
						else
							resize image width (MaxSize of thumbnail of FileSizes) resample method bicubic sharper
						end if
						--save thumbnail image
						save in (DoneFolder & prefixName of thumbnail of FileSizes & File2) as JPEG with options {quality:8} with copying
					end if
				end tell
				close current document saving no
			end repeat
			close document WaterMarkDoc saving no
		end timeout
	end tell
end ProcessFiles

Hi Jerome,

In my scripts I use allways the command:
open file imagePath (the word “file” is not present in your last script but is present in your old script) where imagePath contains string of full path to the file. I never use alias command.
All works. I processed in this way millions of images doing preview, thumbs, watermark…

Hagi

If I remember correctly, in the first command to open the file the variable with the file path is a string so photoshop needs the file specifier. Later it is using the list of aliases built in the beginning. Since an alias is a file reference you don’t need to have it in the open command.

Thank you all for your help, I’ll have a play around with that script Jerome and see how I get on! :slight_smile:

Hi, Wasabi

Try it


-- this sub-routine processes files 
on process_item(this_item)
tell application "Finder"
				set theFile tothis_item as alias -- for Illustrator
				set theFileName to theFile as text -- for Photoshop
			end tell
   -- NOTE that the variable this_item is a file reference in alias format 
   tell application "Adobe Photoshop CC"
set display dialogs to never
					purge all caches
with timeout of 1800 seconds
try
						open file theFileName
on error the error_message number the error_number
		display dialog "Error in process_item “ Photoshop Step: " & the error_number & ". " & the error_message ¬
			buttons {"OK"} default button 1 with icon stop
	end try
end timeout
   end tell
end process_item