Iβve got a great sort handler that works fine, except if the list contains missing value. How can I make the sort handler handle/exclude/remove missing value?
Thanks.
It currently fails with this error:
-[NSNull _fastCStringContents:]: unrecognized selector sent to instance 0x7fffd48490e0
use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation"
use scripting additions
set myList to {"one", missing value, "two", "three", "four"}
set mySortedList to my sortListMakeUnique(myList)
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on sortListMakeUnique(pList) -- @List @Sort @Unique @Dups @ASObjC @Shane
(* VER: 1.1 2017-11-22
---------------------------------------------------------------------------------
PURPOSE: Remove Duplicate Items, and Sort List
PARAMETERS:
β’ pList β list β List of items
RETURNS: list β
AUTHOR: JMichaelTX, refactored script by ShaneStanley & Chris Stone
REF:
1. 2015-11-01, @ShaneStanley, Everyday ASObjC 3rd ed, p77
See p55 for more complex sorts
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
*)
set theSet to current application's NSSet's setWithArray:pList
set anArray to theSet's allObjects()
return (anArray's sortedArrayUsingSelector:"compare:") as list
end sortListMakeUnique
--~~~~~~~~~~~~~~~ END OF handler sortListMakeUnique ~~~~~~~~~~~~~~~~~~~~~~~~~
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on sortListMakeUnique(pList) -- @List @Sort @Unique @Dups @ASObjC @Shane
(* VER: 1.2 2018-07-12
---------------------------------------------------------------------------------
PURPOSE: Remove Duplicate Items, and Sort List
PARAMETERS:
β’ pList β list β List of items
RETURNS: list β
AUTHOR: JMichaelTX, refactored script by ShaneStanley & Chris Stone
REF:
1. 2015-11-01, @ShaneStanley, Everyday ASObjC 3rd ed, p77
See p55 for more complex sorts
2. 2018-07-12, ShaneStanley, Late Night Software Ltd.
How to Make ASObjC Sort Handle missing value?
http://forum.latenightsw.com/t/how-to-make-asobjc-sort-handle-missing-value/1412/2
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
*)
set theSet to current application's NSSet's setWithArray:pList
set anArray to theSet's allObjects()
--- Remove Array Elements with missing value ---
set thePred to current application's NSPredicate's predicateWithFormat:"SELF != nil"
set anArray to anArray's filteredArrayUsingPredicate:thePred
return (anArray's sortedArrayUsingSelector:"compare:") as list
end sortListMakeUnique
--~~~~~~~~~~~~~~~ END OF handler sortListMakeUnique ~~~~~~~~~~~~~~~~~~~~~~~~~