How do I remove the values in an array of values from another array?
use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
set applescriptList to {"a", "b", "c", "d", "e"}
set theValuesToRemove to "b"
set l to removeValueFromList(theValuesToRemove, applescriptList) --as list
-->{"a", "c", "d", "e"}
set applescriptList to {"a", {"b", "c"}, "d", "e"}
set theValuesToRemove to {"b", "c"}
set l to removeValueFromList(theValuesToRemove, applescriptList)
-->{"a", "d", "e"}
set applescriptList to {"a", "b", "c", "d", "e"}
set theValuesToRemove to {"b", "c"}
set l to removeValueFromList(theValuesToRemove, applescriptList)
-->{"a", "b", "c", "d", "e"} :( I would like this result to be {"a", "d", "e"}
on NSMutableArrayFrom(applescriptList)
set AS_NSArray to current application's NSMutableArray's alloc()'s init()
AS_NSArray's setArray:applescriptList
return AS_NSArray
end NSMutableArrayFrom
on removeValueFromList(theValuesToRemove, applescriptList)
set theNSArray to my NSMutableArrayFrom(applescriptList)
set theValuesToRemoveArray to current application's NSArray's arrayWithObject:(theValuesToRemove)
set theResult to theNSArray's removeObjectsInArray:theValuesToRemoveArray
return theNSArray as list
end removeValueFromList
removeValueFromList works for single values, a string, a number etc, but I can’t figure out the proper way to make it remove all the items in a list of values from the other array.
As you can see in the second call, I’m obviously telling it to remove the theValuesToRemove array from the applescriptList array; rather than telling it to remove the elements of theValuesToRemove array from the applescriptList array.
I’m particularly confused because I think the documentation reads as if it should works as I want…
[
func removeObjects(in: [Any])
].
(removeObjects(in:) | Apple Developer Documentation)
Removes from the receiving array the objects in another given array.
TIA