folder actions not working...

I am using the example script that comes with OSX,

property done_foldername : "JPEG Images"
property originals_foldername : "Original Images"
property newimage_extension : "jpg"
-- the list of file types which will be processed 
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property type_list : {"TIFF", "GIFf", "PNGf", "PICT", "JPEG"}
-- since file types are optional in Mac OS X, 
-- check the name extension if there is no file type 
-- NOTE: do not use periods (.) with the items in the name extensions list 
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"} 
property extension_list : {"tif", "tiff", "gif", "png", "pict", "pct", "jpg", "jpeg"}


on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if not (exists folder done_foldername of this_folder) then
			make new folder at this_folder with properties {name:done_foldername}
		end if
		set the results_folder to (folder done_foldername of this_folder) as alias
		if not (exists folder originals_foldername of this_folder) then
			make new folder at this_folder with properties {name:originals_foldername}
			set current view of container window of this_folder to list view
		end if
		set the originals_folder to folder originals_foldername of this_folder
	end tell
	try
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
			set the item_info to the info for this_item
			if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
				tell application "Finder"
					my resolve_conflicts(this_item, originals_folder, "")
					set the new_name to my resolve_conflicts(this_item, results_folder, newimage_extension)
					set the source_file to (move this_item to the originals_folder with replacing) as alias
				end tell
				process_item(source_file, new_name, results_folder)
			end if
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
			tell application "Finder"
				activate
				display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
			end tell
		end if
	end try
end adding folder items to

on resolve_conflicts(this_item, target_folder, new_extension)
	tell application "Finder"
		set the file_name to the name of this_item
		set file_extension to the name extension of this_item
		if the file_extension is "" then
			set the trimmed_name to the file_name
		else
			set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
		end if
		if the new_extension is "" then
			set target_name to file_name
			set target_extension to file_extension
		else
			set target_extension to new_extension
			set target_name to (the trimmed_name & "." & target_extension) as string
		end if
		if (exists document file target_name of target_folder) then
			set the name_increment to 1
			repeat
				set the new_name to (the trimmed_name & "." & (name_increment as string) & "." & target_extension) as string
				if not (exists document file new_name of the target_folder) then
					-- rename to conflicting file
					set the name of document file target_name of the target_folder to the new_name
					exit repeat
				else
					set the name_increment to the name_increment + 1
				end if
			end repeat
		end if
	end tell
	return the target_name
end resolve_conflicts

-- this sub-routine processes files 
on process_item(source_file, new_name, results_folder)
	-- NOTE that the variable this_item is a file reference in alias format 
	-- FILE PROCESSING STATEMENTS GOES HERE 
	try
		-- the target path is the destination folder and the new file name
		set the target_path to ((results_folder as string) & new_name) as string
		with timeout of 900 seconds
			tell application "Image Events"
				launch -- always use with Folder Actions
				set this_image to open file (source_file as string)
				
				save this_image as JPEG in file target_path with icon
				close this_image
			end tell
		end timeout
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_item

this works fine, but i want to add a resize to the image events method, so i changed the method to this…


on process_item(source_file, new_name, results_folder)
	-- NOTE that the variable this_item is a file reference in alias format 
	-- FILE PROCESSING STATEMENTS GOES HERE 
	try
		-- the target path is the destination folder and the new file name
		set the target_path to ((results_folder as string) & new_name) as string
		with timeout of 900 seconds
			tell application "Image Events"
				launch -- always use with Folder Actions
				set this_image to open file (source_file as string)
				scale this_image to size 400
				save this_image as JPEG in file target_path with icon
				close this_image
			end tell
		end timeout
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_item

and this script gives me an error and doesnt work. Any ideas why this would error? Thanks.

Moved to AppleScript | OS X forum

Your process_item handler works for me, so you aren’t sending it the proper argument types if it is the source of the error. Tell us what the error message is.

i get an error of -1708 which is “Event wasn’t handled by an Apple event handler”. but it only happens when i add the scale to 400, i take that line away, and it works with no problems.

Does that depend on the type of file you have added or does it fail every time?

Im not really sure, im taking images from a camera, all the same format. So when i use the scale method, it fails with the error. but without the scale, it works just fine. Sorry to be so repetitive but im a litte stunned that such a simple thing would stop this script from working. I dont remember the message, but the error does say something about type and skipping something. Dont remember now. One thing, when i use this script, its on a Japanese computer. Not sure if that would make a difference.

It might, CG.

Are the downloads RAW?

they are taken from the camera as .JPG. What does CG mean?

cinegod = CG

haha

I can’t figure out why your script fails because when I set it up on my machine (dual-core G5/2.3, OS X 10.4.7), it works perfectly. Starting with a folder with the script attached, I dropped a few images on it, it created two internal folders, the JPG folder contained a scaled version of the images I dropped and the original was moved as it should have been. :confused:

There was one difference between my version and yours - I put ‘my’ in front of the call to the processing handler:

my process_item(source_file, new_name, results_folder)

That’s a nice script and works in my mac.
I got it to work with ghostscript converting pdfs to tiffs, somehow. And to my suprise it puts originals pdfs to “Original files”-folder and converted tiffs to “TIFF files”-folder, in my case.
But, because I’m a novice with applescript, how would I make it to do next thing:
- in order to make new folder “TIFF files”-folder it should make a new folder with original file name minus extension (.pdf).

  • and perhaps add date after it
  • and put converted tiff-files to it?
    There is subroutine/handler? (resolve_conflicts) where script removes or add extension.
    If only I knew how to use that trimmed_name variable at the beginning where it makes done_foldername (TIFF-files property).

Any ideas? Thanx

Question: You want ‘someName.pdf’ to be converted to ‘someName.tif’ and then to be saved alone in its own folder named ‘someName[shortDate]’?

The best script for a Finder sortable short date is Nigel Garvey’s:

set {year:y, month:m, day:d} to (current date)
set shortDate to text 3 thru -1 of ((y * 10000 + m * 100 + d) as string)
--> 060918

Here’s one of many ways to extract a base name:

set f to (choose file) -- for illustration, needs an alias
tell (info for f) to set {Nm, Ex} to {name, name extension}
set BN to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
BN

To Adam’s question: Yes, that’s what I want and there maybe tens of pages in pdfs.
I took a scriptquote down here to seen what I’ve changed and there are comments where I’d like to make changes but don’t know what and how. That script works as a folderaction ok, but I need separate folders for each dropped pdffile which will be converted to tiff files for a imageserver.
I’ve been trying to set trimmed_name to done_foldername and some other things but script won’t do anything.
Also I’ve managed to do a folderactionscript the way You wrote there above, which works alone, but don’t know how to add here. And is it necessary at all? If I’m right extension removing is there in script, except date, but that is not necessary.
Hope You understand what I’m trying to say because english is not my native language.
Thanks for Your earlier advice!

property done_foldername : "tif Files" -- want to remove this line or make it changing when original filename changes
property originals_foldername : "Original Files"
property newimage_extension : "tif"
-- the list of file types which will be processed 
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property type_list : {"TIFF"}
-- since file types are optional in Mac OS X, 
-- check the name extension if there is no file type 
-- NOTE: do not use periods (.) with the items in the name extensions list 
-- eg: {"txt", "text", "jpg", "jpeg"}, NOT: {".txt", ".text", ".jpg", ".jpeg"} 
property extension_list : {"eps", "ps", "pdf"}


on adding folder items to this_folder after receiving these_items
	tell application "Finder"
		if not (exists folder done_foldername of this_folder) then
			make new folder at this_folder with properties {name:done_foldername}
		end if
		set the results_folder to (folder done_foldername of this_folder) as alias
		(*
		if statement above would make new done_folder named as a new originalfile when dropped to this folder where this acts as a folderaction.
		Down there is a subroutine (on resolve_conflicts...) where extension is removed and we have trimmed_name variable. But how can I use it here or above somehow or make a donefolder down there?
		*)
		if not (exists folder originals_foldername of this_folder) then
			make new folder at this_folder with properties {name:originals_foldername}
			set current view of container window of this_folder to list view
		end if
		set the originals_folder to folder originals_foldername of this_folder
	end tell
	try
		repeat with i from 1 to number of items in these_items
			set this_item to item i of these_items
			set the item_info to the info for this_item
			if (alias of the item_info is false and the file type of the item_info is in the type_list) or (the name extension of the item_info is in the extension_list) then
				tell application "Finder"
					my resolve_conflicts(this_item, originals_folder, "")
					set the new_name to my resolve_conflicts(this_item, results_folder, newimage_extension)
					set the source_file to (move this_item to the originals_folder with replacing) as alias
				end tell
				process_item(source_file, new_name, results_folder)
			end if
		end repeat
	on error error_message number error_number
		if the error_number is not -128 then
			tell application "Finder"
				activate
				display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
			end tell
		end if
	end try
end adding folder items to

on resolve_conflicts(this_item, target_folder, new_extension)
	tell application "Finder"
		set the file_name to the name of this_item
		set file_extension to the name extension of this_item
		if the file_extension is "" then
			set the trimmed_name to the file_name
		else
			set the trimmed_name to text 1 thru -((length of file_extension) + 2) of the file_name
		end if
		if the new_extension is "" then
			set target_name to file_name
			set target_extension to file_extension
		else
			set target_extension to new_extension
			set target_name to (the trimmed_name & "." & target_extension) as string
		end if
		if (exists document file target_name of target_folder) then
			set the name_increment to 1
			repeat
				set the new_name to (the trimmed_name & "." & (name_increment as string) & "." & target_extension) as string
				if not (exists document file new_name of the target_folder) then
					-- rename to conflicting file
					set the name of document file target_name of the target_folder to the new_name
					exit repeat
				else
					set the name_increment to the name_increment + 1
				end if
			end repeat
		end if
	end tell
	return the target_name
end resolve_conflicts

-- this sub-routine processes files 
on process_item(source_file, new_name, results_folder)
	-- NOTE that the variable this_item is a file reference in alias format 
	-- FILE PROCESSING STATEMENTS GO HERE 
	try
		set the source_item to the quoted form of the POSIX path of the source_file
		-- the target path is the destination folder and the new file name
		set the target_path to the quoted form of the POSIX path of (((results_folder as string) & new_name) as string)
		with timeout of 900 seconds
			do shell script ("/usr/local/bin/gs -sDEVICE=tiffg4 -dNOPAUSE -dBATCH -r150 -sOutputFile=%d" & target_path & " " & source_item)
		end timeout
	on error error_message
		tell application "Finder"
			activate
			display dialog error_message buttons {"Cancel"} default button 1 giving up after 120
		end tell
	end try
end process_item

Model: Powerbook G4 1.25
AppleScript: 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hello

I am new on this forum and just discovered the cinegod’s question.

The error he met may be due to the existence of a char whose code is higher than 127 in the file name.
It’s an image Events oddity which stroke in 10.3.x but is killed in 10.4.x
With such a char in a name (or even in the pathname), Image Events was unable to reach the file’s properties and so it failed on the set scale instruction.

I described my workaround in an other thread.

Yvan KOENIG (from FRANCE mardi 19 septembre 2006 15:36:58)