I am trying to find all of the records within an Array that meet certain criteria using Foundation’s predicate method, but have been unsuccessful.
I have an array of records
set PDFRecord to {{PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-001.pdf", EORPageNumber:"1", PrepositionWord:"el", EORTotalPageCount:"2", OCRMethod:"ap"}, {PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-001.pdf", EORPageNumber:"1", PrepositionWord:"of", EORTotalPageCount:"2", OCRMethod:"ts"}, {PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-002.pdf", EORPageNumber:"2", PrepositionWord:"ot", EORTotalPageCount:"2", OCRMethod:"ap"}, {PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-003.pdf", EORPageNumber:"1", PrepositionWord:"of", EORTotalPageCount:"2", OCRMethod:"ap"}, {PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-003.pdf"}}
I want to group all of the records by the last three digits of their pdf name, so that, for an example, all of the records whose property PDFFile ends with 001 are grouped together, and all of the records of whose whose property PDFFile ends with 002 are grouped together, and group each group as the script increments the last three digits by 1, that is to say to 003, 004 and so on.
I then want to analyze each group to compare the data of its records in regard to their EORPageNumber and EORTotalPageCount.
My attempt with the following approach woefully failed.
set thePredicate to current application's NSPredicate's predicateWithFormat:"PDFFile =[c] '001"
set theFiles to (folderContentsArray's filteredArrayUsingPredicate:thePredicate)'s valueForKey:"path
I have also tried Shane Stanley’s example from https://macscripter.net/viewtopic.php?pid=203093 but also without success.
set PDFRecord to {{PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-001.pdf", EORPageNumber:"1", PrepositionWord:"el", EORTotalPageCount:"2", OCRMethod:"ap"}, {PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-001.pdf", EORPageNumber:"1", PrepositionWord:"of", EORTotalPageCount:"2", OCRMethod:"ts"}, {PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-002.pdf", EORPageNumber:"2", PrepositionWord:"ot", EORTotalPageCount:"2", OCRMethod:"ap"}, {PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-003.pdf", EORPageNumber:"1", PrepositionWord:"of", EORTotalPageCount:"2", OCRMethod:"ap"}, {PDFFile:"/Users/alan/Desktop/PDFDirectory2022-10-13-003.pdf"}}
set theFilter to {"001"}
set thePreds to {}
repeat with each in theFilter
set end of thePreds to (current application's NSPredicate's predicateWithFormat:" (self[1] CONTAINS %@)" argumentArray:{each})
end repeat
set theArray to (current application's NSArray's arrayWithArray:PDFRecord)
set theListOfLists to (theArray's filteredArrayUsingPredicate:(current application's NSCompoundPredicate's andPredicateWithSubpredicates:thePreds)) as list
How can I write an AppleScript to group the records by their final three digits, analyze the data, and then move on to the next set of records, whose PDFFile’s path name is incremented by 1?