Probably should be simpler than I'm finding it

I am (trying) putting together a droplet script that iterates through a folder of images that is dropped on the script taking each one to Photoshop to be resized and then saved into an output directory, and also create an xml file in the output directory with information on the image. After each image has been processed I need to copy some other files into the output directory.

While I am fairly practiced in more C-like languages like PHP and I’ve scripted in BASH, this english-like stuff really throws me for a loop. I’ve found examples of iterators and scripts that create folders and text files, but when I put things together I get strange errors that I have no idea how to debug, like “File << class docf>> pic.jpg wasn’t found.”

If somebody would be so kind as to help me translate my pseudo code into applescript that works, I’d appreciate it:

make output directory on desktop with user specified name
create xml file in output directory
start iteration for all files in dropped folder
if file type = jpeg or tiff then
tell photoshop to resize and save into output directory with same name as original
append picture information (name and caption) to xml file
end if
end iteration
append ending lines onto xml file
copy other files from a specified source directory into output directory

I think I may be able to figure out what to use in the Photoshop tell block from Adobe’s reference .pdf.

In case it helps, I’m building a script to build a photo album for SimpleViewer http://airtightinteractive.com/simpleviewer/ which some of you might be familliar with. I’m also open to other methods to accomplishing this if anybody has suggestions.

The catch – has to run on 10.2.9 (that’s why I’m not using some of the other SimpleViewer solutions)

Thanks in advance, and long live user communities!

The catch is the killer, bachisanerd - virtually all of us have upgraded and only a few of us maintain the ability to test in 10.3, but not 10.2. AppleScript has matured since Cheetah. You’ll have to be the tester.

What you want to do isn’t a big deal, except that you don’t specify anything about the XML file.

Thanks for the reply.

I hadn’t figured AppleScript had changed much since OS X was introduced. That probably explains why some of the snippets I’ve found throw errors on my 10.3.9 machine.

If I knew how to append data to the end of a text file, I think I could figure out how to build the XML file. Here is an example of what the file needs to look, with data for just one image. The tag will be created many times by the iterator based on the name of the image.

<?xml version="1.0" encoding="UTF-8"?>

<SIMPLEVIEWER_DATA maxImageDimension=“480” title=“Example Title” imagePath=“” thumbPath=“”>

115-1503_IMG.jpg

</SIMPLEVIEWER_DATA>

I have no idea if this works in Cheetah; it does in Tiger which is all I can test it with: (it must be saved as an application, and files must be dropped on it)


on open someFiles
	set fileList to {}
	set FN to text returned of (display dialog "Please enter a name for the new folder" default answer "NewFolder")
	tell application "Finder"
		set newFiles to someFiles
		make new folder at desktop with properties {name:FN}
		repeat with aFile in newFiles
			if (name extension of contents of aFile) is in {"jpeg", "tiff", "jpg", "tif"} then
				-- do some magic
				set end of fileList to name of aFile
				move aFile to folder FN
			end if
		end repeat
	end tell
	
	set F to open for access (path to desktop as text) & FN & ":fileList.txt" with write permission
	set eof of F to 0
	try
		repeat with N in fileList
			write contents of N & return to F
		end repeat
		close access F
	on error e
		close access F
	end try
end open

Quite a catch. Jaguar only went up to 10.2.8. :wink:

I can’t help with the Photoshop stuff, but the script below might help you get started. When saved as an application, it becomes a droplet that takes each folder dropped on it, asks for a name for the corresponding output folder, creates the output folder, and in it an XML file listing the dropped folder’s TIFFs and JPEGs. (I’ve assumed the XML file has to be plain text with CR line endings, but that’s easily changed.) The ‘run’ handler is just to allow the script to be tested in Script Editor with the Finder’s selection.

The script (as it stands) works in both 10.2.8 and 10.4.8. If it does what you want, you’ll need to develop the processFile() handler at the bottom to perform the necessary image manipulation and resaving. Hope it helps.

(* This run handler's just for testing in Script Editor.
Delete it or comment it out when the script's working properly. *)
on run
	tell application "Finder"
		set droppedItems to selection
		repeat with thisItem in droppedItems
			set thisItem's contents to thisItem as alias
		end repeat
	end tell
	doTheBusiness(droppedItems)
end run

-- The 'open' handler for the droplet.
on open droppedItems
	doTheBusiness(droppedItems)
end open

-- Check that each dropped item's a folder and process it if it is.
on doTheBusiness(droppedItems)
	set fileInDrop to false
	
	repeat with thisItem in droppedItems
		set {folder:isFolder, package folder:isBundle} to (info for thisItem)
		if ((isFolder) and not (isBundle)) then
			processFolder(thisItem)
		else
			set fileInDrop to true
		end if
	end repeat
	
	if (fileInDrop) then
		activate
		display dialog "At least one of the items dropped was a file. This droplet only handles folders." buttons {"OK"} with icon note
	end if
end doTheBusiness

-- Process a dropped folder.
on processFolder(thisFolder)
	tell application "Finder"
		activate
		-- Get the current dropped folder's name and ask the user for an output folder name to use with it.
		set thisFolderName to thisFolder's name
		set {button returned:buttonClicked, text returned:outputFolderName} to (display dialog "Please enter a name for the output directory to be used for folder "" & thisFolderName & ""." default answer "" buttons {"Stop", "Skip", "OK"} default button "OK")
		if (buttonClicked is "Stop") then
			error number -128
		else if (buttonClicked is "Skip") then
			return
		end if
		-- Check here for legitimate name?
		
		-- Create the output folder and get a list of the TIFF and JPEG files in the dropped folder.
		set outputFolderPath to (make new folder at desktop with properties {name:outputFolderName}) as Unicode text
		try
			set pictureFiles to (thisFolder's files whose name extension is in {"jpeg", "jpg", "tiff", "tif"}) as alias list
		on error
			set pictureFiles to {(thisFolder's first file whose name extension is in {"jpeg", "jpg", "tiff", "tif"}) as alias}
		end try
	end tell
	
	-- Initialise the XML text as a list of entries.
	set xmlText to {"<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "<SIMPLEVIEWER_DATA maxImageDimension=\"480\" title=\"Example Title\" imagePath=\"\" thumbPath=\"\">", ""}
	set tagStart to tab & "<IMAGE>" & return & tab & tab & "<NAME>"
	set tagMiddle to "</NAME>" & return & tab & tab & "<CAPTION>"
	set tagEnd to "</CAPTION>" & return & tab & "</IMAGE>"
	
	
	repeat with thisFile in pictureFiles
		-- Process each file in turn.
		set {thisName, caption} to processFile(thisFile, outputFolderPath)
		-- Append its entry to the XML text list.
		set end of xmlText to tagStart & thisName & tagMiddle & caption & tagEnd
	end repeat
	
	-- Append the end tage to the XML text entry list and coerce the list to a single text.
	set end of xmlText to return & "</SIMPLEVIEWER_DATA>"
	set astid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to return
	set xmlText to xmlText as string
	set AppleScript's text item delimiters to astid
	
	-- Write the XML text to a file in the output folder. Change the name!
	set fRef to (open for access file (outputFolderPath & "My XML file.xml") with write permission)
	try
		set eof fRef to 0
		write xmlText to fRef
	end try
	close access fRef
end processFolder

-- Process a TIFF or JPEG file in a dropped folder.
-- thisFile is an alias to the image file.
-- outputFolderPath is the path to the output folder as unicode text.
on processFile(thisFile, outputFolderPath)
	set thisName to name of (info for thisFile)
	set caption to ""
	
	(* Development needed here. *)
	-- tell application Photoshop to process this file and save it as file (outputFolderPath & thisName).
	-- Set caption to the caption text.
	(*******)
	
	set {text:thisName} to (thisName as string) -- Convert the file name from Unicode to plain text for the XML file.
	
	return {thisName, caption} -- Return the file name and the caption for the XML file.
end processFile

good catch, 10.2.8 is right :smiley:

Thank you both for the help, you’ve given me more than enough to start on. I’m glad there are people out there that understand things I don’t. Hopefully I’ll be able to use this to learn some more about Applescript myself.

Thanks again

-BacH