Find duplicate items in list, modify both/all of them?

I’m writing a script that displays a list of applications so that a user can select the one he wants to use.

I start with a text list that looks like this (call it longNameList):

I copy the list to a second list, and trim the strings to look like this on the menu (call it shortNameList):

But, when there are duplicates on the second list (as there are here with two TextEdit items), I want to make the list look like this, so that the duplicate item are shown with their full paths:

I can find many posts explaining how to find duplicates in a list, and how to count duplicates in a list, but I haven’t found any that would let me do something like this (pseudocode):

if item n in shortNameList is a duplicate then
set item n in shortNameList to item n in longNameList
end if

Is this possible? I would be very grateful for any help with doing this.

I think I see how to accomplish the task in the starting post, but I’m still not clear on the syntax.

Basically, I think i should do this:

  1. copy shortNameList to tempList

  2. then, repeat with m from 1 to shortNameList’s length and do this test:

    if m is equal to any of (items 1 through m-1) and (items m+1 to tempList’s length) then
    set m in shortNameList to m in longNameList

I think the logic of this is correct, but I can’t get the syntax correct.

Is this a problem that might interest any of the experts here? Thanks again for any help.

Maybe there is a simpler way, but try this


set displayList to {}
set appList to {"/Applications/TextWranger.app", "/Applications/iWork '09/Pages.app", "/Volumes/Old OS X/Applications/TextEdit.app", "/Applications/TextEdit.app"}

set {TID, text item delimiters} to {text item delimiters, "/"}
repeat with i from 1 to (count appList)
	set currentApp to last text item of item i of appList
	set idx to indexOfItemInList(currentApp, displayList, count displayList)
	if idx is not 0 then
		set item idx of displayList to item indexOfItemInList(currentApp, appList, i - 1) of appList
		set end of displayList to item i of appList
	else
		set end of displayList to currentApp
	end if
end repeat
set text item delimiters to TID
choose from list displayList

on indexOfItemInList(anItem, aList, maxValue)
	repeat with i from 1 to maxValue
		if item i of aList ends with anItem then return i
	end repeat
	return 0
end indexOfItemInList

That seems absolutely perfect, Stefan. Thank you!

I’m still trying to understand the syntax, but I hope that by looking at it for a long time, I’ll figure out how it works. Meanwhile, it works!

the script builds up the display list from the application list.

The indexOfItemInList() handler returns the index of an given item in a given list with a given length.
It’s called twice:
First to check the occurrence of the item in the display list, if it’s not in the list, the app name will be appended to the display list.
Second to determine the corresponding path in the application list, the former app name will be replaced by the path and path of the current item will be appended to the display list.

I guess the script will fail if there are more than two occurrences of the same application name

Hmm… Yes, I see. Now that I understand the syntax, I’ll experiment with this. Perhaps there would be some way to repeat the first test, excluding any duplicate item found, until there are no further duplicates?