Droplet File order

Hi,

I’ve built a droplet that needs to process files in a specific order.

  1. Is there a known order in which a number of files dropped on a droplet are presented to the script? Initial tests don’t show any obvious order (i.e. don’t match finder, not alphabetical. Searchs of the web, script sites, and apple documentation didn’t turn up anything - maybe this is a cocoa specified thing)

  2. Once I have the list is there an easy way to present the list to the user and have them sort them (i.e. drag them around) so the script can process them in the desired order?

  3. Can you supply/point me to example scripts that need to do something similar? (I’ve looked at “join pdf” droplets which need to get the pages in the right order but they don’t appear to deal with this)

Thanks for any help,
Brad

My experience is that files are usually processed in the order in which they are selected in the Finder.

I believe it is the order in which they are selected in the Finder (and this is not limited to Cocoa). To which “join pdf” scripts are you referring? My join pdf script:

http://homepage.mac.com/jonn8/as/dist/Join_PDFs.hqx

does indeed, to a limited scope, address this issue (i.e., sort by name only).

Jon

IMHO I don’t think it’s possible to claim that they’re passed in 'the order they were selected in the Finder".

What order would that be if the user hit Cmd-A?

What about if they dragged a box to select multiple files?

Or maybe they drop a folder of files - technically the files aren’t selected at all.

In short I think the best you can say is ‘undefined’. You’ll need to develop your own rules/interface for ordering files in a droplet.

Well, I’m not sure if that is the only criteria but the Finder selection order does make a difference. You can use the following code saved as an application to see how the selection order effects the order the items are processed in the list:

on open the_files
	set ordered_files to {}
	repeat with this_file in the_files
		set end of ordered_files to ((this_file as string) & return)
	end repeat
	do shell script "echo " & quoted form of (ordered_files as string) & " > ~/Desktop/ordered_files.txt"
end open

Once the file is created, subsequent runs will overwrite the file but if you have it open in an obliging application (such as BBEdit), it will automatically update the file for you as the file is edited by the AS application. In this way you can try selecting some files and dropping it on the application to see the sort order then try choosing the same files but in a different order to see if the files are processed in a different order. This has long been a headache for scripters and has been covered in many threads. There are several solutions available for sorting files based on any number of criteria including Finder file name sort order, alphabetical sort order, creation date, modification date, label, etc. (or the reverse of any of the above).

Jon

Hi,

Think I had a theory that the item you click on and drag matters in the list order. Anyway, about the choosing list order, You can try something like the following. Firstlly, the ‘choose from list’ command shows only 2 buttons in its dialog. As a workaround, you can allow the user to choose commands from the list also. Here’s a quick example that you should modify 'cause I did it quickly:

set the_list to text items of "abcde"
set the_commands to {"Move Up", "Move Down", "Done", "Reset"}
set the_instructions to "(use Shift + Apple + Key for seperate items)"
copy the_list to user_list
set the_count to 0
repeat
	set the_count to the_count + 1
	choose from list ({"Commands:"} & the_commands & {"Your ordered list:"}) & user_list with prompt ¬
		("Choose command & list item:" & return & the_instructions) with multiple selections allowed
	set user_choices to result
	if user_choices is false then error number -128
	set the_command to item 1 of user_choices
	try
		set user_choice to item 2 of user_choices
		if the_command is "Move Up" then
			set user_list to MoveListItemUp(user_list, user_choice)
		else if the_command is "Move Down" then
			set user_list to reverse of MoveListItemUp(reverse of user_list, user_choice)
		else if the_command is "Done" then
			exit repeat
		else
			copy the_list to user_list
		end if
	on error err_mess
		if the_command is "Done" then exit repeat
		if the_command is "Reset" then
			copy the_list to user_list
		else
			display dialog "Incorrect Entry: try again." buttons "OK" default button "OK"
		end if
	end try
end repeat
return user_list

on MoveListItemUp(the_list, the_item)
	set i to 0
	set item_found to false
	repeat with this_item in the_list
		set item_content to (contents of this_item)
		set i to i + 1
		if item_content is the_item then
			if i is not 1 then
				set item i of the_list to item (i - 1) of the_list
				set item (i - 1) of the_list to item_content
			end if
			set item_found to true
			exit repeat
		end if
	end repeat
	if not item_found then error -128
	return the_list
end MoveListItemUp

There are other ways to present this dialog depending on your preference.

Otherwise, some free ways to make your own dialog is with Smile and AppleScipt Studio. Smile is easier, but the user needs it to be installed. AppleScript Studio is harder.

gl,

Hi,

Thanks for the input. I will try the Applescript Studio approach.

The pdf combining script was:
CombinePDF

Regards,
Brad

I’ve been having exactly the same problem, trying to use Applescript with Photoshop to process a folder of numbered images. I’ve found a solution for my problem, but I’m not sure how universally applicable it is. I hope it helps someone.


on open files_dropped
	tell application "Finder"
		set files_ to sort files_dropped by name
	end tell

	tell application "Adobe Photoshop CS2"
		activate
		repeat with file_ in files_
			set filePath to file_ as string
			open alias filePath
		end repeat
	end tell
end open