What is Best Way to Get User Selected Order of Items?

What is Best Way to Get User Selected Order of Items?

Let’s say I’ve got an AppleScript list of items, could be files, documents, Evernote Notes, email msg, etc, that need to be processed in an order selected by the user.

I know how to display a list using the [format]choose from list[/format] dialog, but it does not return the order the user selected the items.

So, what’s the best way to display a list, and get the user selected order of items?

TIA.

You have to order the list according to your needs before passing it to choose from list.
If you wish entries sorted alphabetically it’s quite easy to achieve.
If the ordering is not alphabetical you will have to design the algorithm applying it.

Yvan KOENIG running El Capitan 10.11.2 in French (VALLAURIS, France) jeudi 17 décembre 2015 10:41:13

AFAIK it’s not possible to get the items in the order the user has selected the items.

You could do it if you scripted it to allow only a single selection, and then looped it until either all of the items had been selected, or the user elected to continue.

Thanks. Perhaps you didn’t notice the main requirement:

Thanks, that just gave me an idea.

I could build a list for choose from list that has two “sections”, and repeat it until all items were selected or the user chose Cancel:


"
--- CHOOSE ONE IN THE ORDER YOU PREFER ---
Item 1
Item 2
Item 3
--- ITEMS CHOSEN ---
"

After the first selection:

"
--- CHOOSE ONE IN THE ORDER YOU PREFER ---
Item 1
Item 3
--- ITEMS CHOSEN ---
Item 2
"

Not as friendly as drag/drop, but better than nothing. :wink:

Right. Post back to this thread if you have any code issues.

Well, here’s my quick and dirty, non-optimized, non-error checking, solution.
It seems to work OK.

If anyone sees and issues, and or suggestions for improvements, please don’t hesitate to post.

Screenshot in mid-selection



set lstNotes to {">[1] Note 1", ">[2] Note 2", ">[3] Note 3"}
set lstSelected to {}
set numSelected to 0

set strChoiceHdr to "--- CHOOSE ONE IN THE ORDER YOU PREFER ---"
set strChosenHdr to "--- NOTES CHOSEN ---"

set lstDialog to {strChoiceHdr} & lstNotes & strChosenHdr & lstSelected
log lstDialog

repeat with iNote from 1 to (length of lstNotes)
	
	choose from list lstDialog with title "Title: Item Selection" with prompt "Prompt: Just pick one" default items (item 2 of lstDialog)
	
	set strItem to result
	--log strItem
	
	set strItemClean to text 2 thru (length of (strItem as text)) of (strItem as text)
	set lstDialog to removeItem(strItem, lstDialog) & strItemClean -- move strItem to bottom
	
	set lstSelected to lstSelected & {strItemClean}
	
end repeat -- Note list

log lstDialog
log lstSelected

set Ans to choose from list lstSelected with title "Note Selection" with prompt "Please confirm your selection items and their order" with empty selection allowed



--=========================== END OF MAIN SCRIPT ==================

on removeItem(pItem, pList)
	
	set strItem to pItem as string
	
	repeat with i from 1 to count of pList
		--log item i of pList
		if (item i of pList) is strItem then
			--log "*** FOUND THE ITEM TO REMOVE ***"
			set item i of pList to null
			exit repeat
		end if
	end repeat
	
	set pList to every text of pList
	--log pList
	
	return pList
	
end removeItem


BTW, is it possible to post an image here? If so, how?

OK, now that I have a basic “user-selected order” of items, I’d like to enhance that a bit.
I need to present the item title/name to the user for selection.
But I need the item ID, which is an index into the actual list of item objects (Evernote Notes in this case).

So, is there a way I can use “choose from list” and return the item ID?

I tried something like this, but it doesn’t like it:


set rec1 to {Title:"Note 1", id:1}
set rec2 to {Title:"Note 2", id:2}
set lstNotes to {rec1} & {rec2}
log lstNotes
choose from list lstNotes

I get this error on the last statement:
[format]error “Can’t make {id:1, Title:"Note 1"} into type string.” number -1700
from {id:1, Title:“Note 1”} to string[/format]
Any ideas?

choose from list does not work with records. Only strings & numbers are acceptable.
The dictionary tells all.

If you must use records you’ll be writing a heap of code to make this work.

Hey JM,

Just a little musing.


set myList to {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"}
set pickedItemList to {}
set chooseFromList to myList & {"=========="} & pickedItemList

repeat
	
	if length of myList = 0 then exit repeat
	
	set pickedItem to (choose from list chooseFromList with title "Test" with prompt ¬
		"Pick One:" default items {first item of chooseFromList} ¬
		multiple selections allowed false ¬
		without empty selection allowed)
	
	if pickedItem = false then
		
		exit repeat
		
	else
		
		set pickedItemList to pickedItemList & pickedItem
		
		repeat with i in myList
			if (contents of i) = (last item of pickedItemList) then
				set (contents of i) to missing value
				set myList to text of myList
			end if
		end repeat
		
		set chooseFromList to myList & {"=========="} & pickedItemList
		
	end if
	
end repeat

chooseFromList


Chris


{ MacBookPro6,1 · 2.66 GHz Intel Core i7 · 8GB RAM · OSX 10.11.2 }
¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯