Hi,
i need a repeat-routine to extract the most frequently items in a list, which contents (file kind) and amount (frequency of a file kind) can vary.
example:
(“avi”,“rtf”,“mov”,“txt”,“png”,“jpg”,“jpg”)
request:
get “jpg”
(keep in mind that this list is a created from a previous copy-handler, also its impossible to use ˜boolean’ queries to pass the results. )
Another step will be to copy 2 or more results into a list, if my list becomes like:
…to choose manually results if there are more items in this list, always after their amount.(in the above case: “avi”,“mov”,“jpg”); the biggest number of coponents in a groups is therefor 2!
Suggestions or similar examples??
i thank you in advance.
If your list is actualy files, there is an easier way than this. But this works with lists of text.
set myList to {"avi", "rtf", "mov", "txt", "png", "jpg", "jpg"}
MostCommonElementsOf(myList)
-- returns {2, {"jpg"}}
set myList to {"avi", "avi", "rtf", "mov", "txt", "png", "jpg", "jpg", "jpg2", "mov"}
MostCommonElementsOf(myList)
--returns {2, {"avi", "mov", "jpg"}}
on MostCommonElementsOf(aList)
set tid to AppleScript's text item delimiters
set myDelimiter to ASCII character 10
set myMaxItems to {}
set maxCount to 0
set AppleScript's text item delimiters to {myDelimiter}
set workString to myDelimiter & (aList as string) & myDelimiter
repeat with oneItem in aList
set AppleScript's text item delimiters to {oneItem & myDelimiter}
set currentCount to count of text items of workString
if maxCount < currentCount then
set maxCount to currentCount
set myMaxItems to {oneItem as text}
else if maxCount = currentCount then
if myMaxItems does not contain oneItem then
copy (oneItem as text) to end of myMaxItems
end if
end if
end repeat
set AppleScript's text item delimiters to tid
return {maxCount - 1, myMaxItems}
end MostCommonElementsOf
set tList to {"avi", "avi", "rtf", "jpg", "mov", "txt", "png", "jpg", "jpg", "jp2", "mov"}
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ", "
set L to tList as text
set AppleScript's text item delimiters to tid
set tValues to {}
set tFreq to {}
set numItems to count words of L
set tNums to {}
repeat with anItem in tList
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to contents of anItem
set end of tNums to (count (text items of L)) - 1
set AppleScript's text item delimiters to tid
end repeat
repeat with k from 1 to numItems
if item k of tNums > 1 then
if item k of tList is not in tValues then
set end of tFreq to item k of tNums
set end of tValues to item k of tList
end if
end if
end repeat
set Ans to {tValues, tFreq} --> {{"avi", "jpg", "mov"}, {2, 3, 2}}
Thanks aaalot for your support, guys- its always for a little time already, i search for methods to archive new filter tecnices for a bigger script,
for instance i’ve quite few time to update scripts and to insert your code. But your codes looks promising. !