I’m attempting to create a custom array of Music/iTunes sort name records as an NSArray as per my own NSRegularExpression which I will later compare against the current state of sort name records (theSortName), the goal being to only update records based on further conditions after getting a short list of theID.
In the example below, I have the desired outcome in the array myTest which takes a considerable amount of time with 15,000 records.
I’m experimenting with theRegexMutableArray to see if this will speed things up,
Should I continue down the NSMutable array path to potentially save time?
Does this save much time rather than building/adding to the end of a new array?
Is there a better way somehow with valueForKey?
Can I somehow parse the full array to sortString(someText)?
tell application "Music"
set theID to current application's NSArray's arrayWithArray:(get persistent ID of every file track in library playlist 1)
set theName to current application's NSArray's arrayWithArray:(get name of every file track in library playlist 1)
set theSortName to current application's NSArray's arrayWithArray:(get sort name of every file track of library playlist 1)
set theRegexMutableArray to current application's NSMutableArray's arrayWithArray:(get name of every file track in library playlist 1)
end tell
set myTest to {}
repeat with i from 1 to count of theID
set end of myTest to sortString(theName's objectAtIndex:(i - 1))
end repeat
repeat with i from 1 to count of theID
(theRegexMutableArray's replaceObjectAtIndex:(i - 1) withObject:(sortString(theRegexMutableArray's objectAtIndex:(i - 1)))) --edited, working but still slow
end repeat
on sortString(someText)
set theNSString to current application's NSString's stringWithString:someText
set theOptions to (current application's NSRegularExpressionDotMatchesLineSeparators as integer) + (current application's NSRegularExpressionAnchorsMatchLines as integer) + (current application's NSCaseInsensitiveSearch as integer)
set theRegEx to current application's NSRegularExpression's regularExpressionWithPattern:"^(A |The )(.*$)" options:theOptions |error|:(missing value)
set theNSString to theRegEx's stringByReplacingMatchesInString:theNSString options:0 range:{location:0, |length|:theNSString's |length|()} withTemplate:"$2"
return theNSString
end sortString
I’m new to Objective-C, I’m still trying to understand translation of code signatures from the documentation and how to query arrays from a database perspective. Appreciate the ITLibrary Framework suggestions in recent posts, thanks! So after I’ve applied regex to an entire array in an optimized manner, I’d like some help to understand the following a little better…
Ive created myReferenceArray, with just 3 key/value pairs for now which should help me cross reference theRegexMutableArray with current state theSortName using NSPredicate. I’m sure there is a better way. Am I creating the myReferenceArray correctly? What else could be improved on here?
set myReferenceArray to {}
repeat with i from 1 to count of theID
set end of myReferenceArray to {theID:theID's objectAtIndex:(i - 1), theSortName:theSortName's objectAtIndex:(i - 1), theRegexMutableArray:theRegexMutableArray's objectAtIndex:(i - 1)}
end repeat
set myReferenceArray to current application's NSArray's arrayWithArray:myReferenceArray
set thePred to current application's NSPredicate's predicateWithFormat:"theRegexMutableArray CONTAINS theSortName" -- edited, working
set theFilteredResult to (myReferenceArray's filteredArrayUsingPredicate:thePred)
Thank you.