Get folder contents using "on open draggedItems"

Hi All,

I have a script, one of Apple’s included scripts (Convert to TIFF), that I’m changing to suit my needs. I would like the user to be able to drag folders or files onto the saved application bundle but am having a little trouble. I’m trying this:


property extension_list : {"jpg", "jpeg", "gif", "png", "pict", "pct"}

on open draggedItems
set these_items to every file of draggedItems whose name extension is in (extension_list)
-- I have actions here
end open

I need these_items to be a list of alias’ to every file whose extension is in extension_list. This is just a portion of the script.

Here is the entire script


property done_foldername : "TIFF Images"
property originals_foldername : "Original Images"
property newimage_extension : "tif"
-- the list of file types which will be processed 
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property type_list : {"JPEG", "GIFf", "PNGf", "PICT"}
-- 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 : {"jpg", "jpeg", "gif", "png", "pict", "pct"}
property desktop_path : (path to desktop)

on run
	display dialog "To use this application drop files onto it's icon" buttons {"OK"} default button 1 with title "Convert to TIFF" with icon note
end run

on open draggedItems
	set these_items to every file of draggedItems whose name extension is in (extension_list)
	tell application "Finder"
		if not (exists folder done_foldername of desktop_path) then
			make new folder at desktop_path with properties {name:done_foldername}
		end if
		set the results_folder to (folder done_foldername of desktop_path) as alias
		if not (exists folder originals_foldername of desktop_path) then
			make new folder at desktop_path with properties {name:originals_foldername}
			set current view of container window of desktop_path to list view
		end if
		set the originals_folder to folder originals_foldername of desktop_path
	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
	set tiffs_path to (path to desktop) & "TIFF Images"
	set originals_path to (path to desktop) & "Original Images"
	
	display dialog " The images have been converted to TIFF format." buttons {"Quit", "Open Folders"} default button 1 with title "Success!" with icon note
	copy the result as list to {the button_pressed}
	
	if button_pressed contains "Open Folder" then
		my open_tiffs(tiffs_path)
		my open_originals(originals_path)
	end if
end open

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 TIFF 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 sub-routine opens the originals folder
on open_originals(originals_path)
	tell application "Finder"
		open the folder (originals_path as string)
		set the toolbar visible of the window "Original Images" to false
		set properties of the icon view options of the window "Original Images" to {arrangement:arranged by name, icon size:128, shows icon preview:true, shows item info:true}
		set bounds of the window "Original Images" to {510, 50, 1010, 500}
	end tell
end open_originals

-- this sub-routine opens the tiff folder 
on open_tiffs(tiffs_path)
	tell application "Finder"
		open the folder (tiffs_path as string)
		set the toolbar visible of the window "TIFF Images" to false
		set properties of the icon view options of the window "TIFF Images" to {arrangement:arranged by name, icon size:128, shows icon preview:true, shows item info:true}
		set bounds of the window "TIFF Images" to {10, 50, 500, 500}
	end tell
end open_tiffs

Many Thanks,
Mark T

Mark you don’t need to do that because you are already checking further down the script in the first place, you do need to change one repeat as I’ve done below though. Give this a try =)


property done_foldername : "TIFF Images"
property originals_foldername : "Original Images"
property newimage_extension : "tif"
-- the list of file types which will be processed 
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property type_list : {"JPEG", "GIFf", "PNGf", "PICT"}
-- 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 : {"jpg", "jpeg", "gif", "png", "pict", "pct"}
property desktop_path : (path to desktop)

on run
	display dialog "To use this application drop files onto it's icon" buttons {"OK"} default button 1 with title "Convert to TIFF" with icon note
end run

on open draggedItems
	tell application "Finder"
		if not (exists folder done_foldername of desktop_path) then
			make new folder at desktop_path with properties {name:done_foldername}
		end if
		set the results_folder to (folder done_foldername of desktop_path) as alias
		if not (exists folder originals_foldername of desktop_path) then
			make new folder at desktop_path with properties {name:originals_foldername}
			set current view of container window of desktop_path to list view
		end if
		set the originals_folder to folder originals_foldername of desktop_path
	end tell
	
	try
		repeat with this_item in draggedItems
			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
	set tiffs_path to (path to desktop) & "TIFF Images"
	set originals_path to (path to desktop) & "Original Images"
	
	display dialog " The images have been converted to TIFF format." buttons {"Quit", "Open Folders"} default button 1 with title "Success!" with icon note
	copy the result as list to {the button_pressed}
	
	if button_pressed contains "Open Folder" then
		my open_tiffs(tiffs_path)
		my open_originals(originals_path)
	end if
end open

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 TIFF 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 sub-routine opens the originals folder
on open_originals(originals_path)
	tell application "Finder"
		open the folder (originals_path as string)
		set the toolbar visible of the window "Original Images" to false
		set properties of the icon view options of the window "Original Images" to {arrangement:arranged by name, icon size:128, shows icon preview:true, shows item info:true}
		set bounds of the window "Original Images" to {510, 50, 1010, 500}
	end tell
end open_originals

-- this sub-routine opens the tiff folder 
on open_tiffs(tiffs_path)
	tell application "Finder"
		open the folder (tiffs_path as string)
		set the toolbar visible of the window "TIFF Images" to false
		set properties of the icon view options of the window "TIFF Images" to {arrangement:arranged by name, icon size:128, shows icon preview:true, shows item info:true}
		set bounds of the window "TIFF Images" to {10, 50, 500, 500}
	end tell
end open_tiffs

Hi James,

Thanks for your reply. I tried your code but I still just get two empty folders. I’m dragging a folder with 2 JPEG’s in it onto the icon. It runs and doesn’t show any errors but appears to do nothing besides create the two folders in the first part of the on open handler. Any ideas?

It works fine if I drag images onto it, which it did before, but I’d like for users to be able to drag images or folders.

Thanks,
Mark T

It’s not very pretty, but it works =)


property done_foldername : "TIFF Images"
property originals_foldername : "Original Images"
property newimage_extension : "tif"
-- the list of file types which will be processed 
-- eg: {"PICT", "JPEG", "TIFF", "GIFf"} 
property type_list : {"JPEG", "GIFf", "PNGf", "PICT"}
-- 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 : {"jpg", "jpeg", "gif", "png", "pict", "pct"}
property desktop_path : (path to desktop)

on run
	display dialog "To use this application drop files onto it's icon" buttons {"OK"} default button 1 with title "Convert to TIFF" with icon note
end run

on open draggedItems
	tell application "Finder"
		if not (exists folder done_foldername of desktop_path) then
			make new folder at desktop_path with properties {name:done_foldername}
		end if
		set the results_folder to (folder done_foldername of desktop_path) as alias
		if not (exists folder originals_foldername of desktop_path) then
			make new folder at desktop_path with properties {name:originals_foldername}
			set current view of container window of desktop_path to list view
		end if
		set the originals_folder to folder originals_foldername of desktop_path
		set master_list to {}
		repeat with this_item in draggedItems
			set item_info to properties of this_item
			if kind of item_info is "Folder" then
				try
					set master_list to every file of entire contents of folder this_item as alias list
				on error
					set master_list to every file of entire contents of folder this_item as alias as list
				end try
			else
				set end of master_list to this_item
			end if
		end repeat
	end tell
	
	try
		repeat with this_item in master_list
			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
	set tiffs_path to (path to desktop) & "TIFF Images"
	set originals_path to (path to desktop) & "Original Images"
	
	display dialog " The images have been converted to TIFF format." buttons {"Quit", "Open Folders"} default button 1 with title "Success!" with icon note
	copy the result as list to {the button_pressed}
	
	if button_pressed contains "Open Folder" then
		my open_tiffs(tiffs_path)
		my open_originals(originals_path)
	end if
end open

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 TIFF 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 sub-routine opens the originals folder
on open_originals(originals_path)
	tell application "Finder"
		open the folder (originals_path as string)
		set the toolbar visible of the window "Original Images" to false
		set properties of the icon view options of the window "Original Images" to {arrangement:arranged by name, icon size:128, shows icon preview:true, shows item info:true}
		set bounds of the window "Original Images" to {510, 50, 1010, 500}
	end tell
end open_originals

-- this sub-routine opens the tiff folder 
on open_tiffs(tiffs_path)
	tell application "Finder"
		open the folder (tiffs_path as string)
		set the toolbar visible of the window "TIFF Images" to false
		set properties of the icon view options of the window "TIFF Images" to {arrangement:arranged by name, icon size:128, shows icon preview:true, shows item info:true}
		set bounds of the window "TIFF Images" to {10, 50, 500, 500}
	end tell
end open_tiffs

Hi James,

Awesome!

So another list was required so if the dropped item is a folder the folders can be eliminated from the list? I guess I thought my one liner of


set these_items to every file of draggedItems whose name extension is in (extension_list)

would just get any file of what was dropped on it.

Thanks for your help,
Mark T