Hiho,
I’m trying to compile a list of all the unique paragraph style names used in a Microsoft Word document. Thus far I have the following:
tell application “Microsoft Word”
tell document 1
set docStyles to paragraph style of contents of every paragraph
end tell
end tell
This script returns a bloated list with multiple occurrences of a given paragraph style whenever a document contains more than one paragraph of the same paragraph style.
Is there a simple way to distill this list into a subset containing nothing but the unique items from this list? The ordering of these items is not important to me, I simply need to remove all duplicate items.
Can anyone point me in the right direction?
Many thanks.
–Stephen
Someone who knows Microsoft Word scripting better than me can probably point out a way to get exactly what you need from the application. Until then, here is how to remove duplicate items from a list:
set firstList to {1, 2, 2, 3, 4, 4, 4}
set secondList to RemoveDuplicates(firstList)
secondList --> {1, 2, 3, 4}
-- Handlers should go at the very top or bottom of your script:
--
on RemoveDuplicates(inputList)
set outputList to {}
repeat with i from 1 to length of inputList
-- We make testItem a single-item list to ensure
-- that sublists of the inputList are properly
-- looked for in the outputlist.
--
set thisItem to item i of inputList
set testItem to {thisItem}
if (outputList does not contain testItem) then
set end of outputList to thisItem
end if
end repeat
return outputList
end RemoveDuplicates
If you’re dealing with an exceptionally large list of items, there are some techniques for speeding up the list processing:
set firstList to {1, 2, 2, 3, 4, 4, 4}
set secondList to UniqueItems(firstList)
secondList --> {1, 2, 3, 4}
on UniqueItems(a)
-- A "Serge" object. Accessing items of a list is done
-- faster with object references. Why? We don't know.
--
script o
property p : a
end script
-- "Serge" doesn't speed up appending to a list
--
set b to {}
repeat with i from 1 to a's length
-- A "Garvey" tell statement. In some situations, the 2 calls
-- to "it" are faster than 2 calls to "o's p's item i".
--
-- Er... the above asserion is one I haven't tested in a while,
-- so I'm probably wrong. ;-)
--
tell o's p's item i to if ({it} is not in b) then set b's end to it
end repeat
return b
end UniqueItems
Thank you. That works like a charm!
–Stephen