I was looking for a way to select files in a directory based on patterns I am knowing from shell commands. So I created a helper that works with predicates to do just that:
- Code: Select all
' this is ukHelper.NSObject
Declare Class "NSPredicate"
Declare Function "NSPredicate" - (NSString *)predicateFormat
Declare Function "NSPredicate" - (BOOL)evaluateWithObject:(id)object
Declare Function "NSPredicate" + (NSPredicate *)predicateWithFormat:(NSString *)predicateFormat
Declare Function "NSPredicate" + (NSPredicate *)predicateWithValue:(BOOL)value
Declare Function "NSPredicate" - (NSPredicate *)predicateWithSubstitutionVariables:(NSDictionary *)variables
Declare Function "NSMutableArray" - (NSArray *)filteredArrayUsingPredicate:(NSPredicate *)predicate
Declare Sub "NSMutableArray" - (void)filterUsingPredicate:(NSPredicate *)predicate
Dim thePred As NSPredicate
' here I create the predicate based on a predicate template (this is the better approach)
Function filterArray2(theArray As NSMutableArray, Filter As String) As Array
Dim localPred As NSPredicate
Dim localArray As NSMutableArray
localArray=theArray
localPred = thePred.predicateWithSubstitutionVariables(Dictionary("Pattern" := Filter))
localArray.filterUsingPredicate(localPred)
Return(localArray)
End Function
' initialize the predicate template
Event Init()
thePred = NSPredicate.predicateWithFormat("SELF LIKE $Pattern")
End Event
The next part is a utility I use to work with ObjB project directories:
- Code: Select all
' this is Global
IBOutlet theTextView As NSTextView
Event AwakeFromNib()
Dim theProject As String
' here I set up a dictionary of the file types I am looking for in specific directories
Dim items As Dictionary("/objb/":="*.mm", "/objb/Parser/":="*.objb")
Dim counter As Integer
Dim itemPath As String
Dim resultArray As NSMutableArray
Dim fileCounter As Integer
Dim fullPath As String
Dim files As NSMutableArray
Dim myFileManager As NSFileManager
theProject = InputPanel("Please enter the name of the project","","")
' please enter the path of your Objective Basic projects here
theProject = "/Users/udo/Desktop/Objective Basic/" & theProject
For counter = 0 To items.Count() - 1
itemPath = Concat(theProject, items.Key(counter))
files = myFileManager.List(itemPath)
If (files.Count() > 0) Then
resultArray = ukHelper.filterArray2(files, items.Object(counter))
Log(resultArray)
For fileCounter = 0 To resultArray.Count() - 1
fullPath = Concat(itemPath, resultArray.Object(fileCounter))
theTextView.Append(fullPath & "\n")
Next
End If
Next
End Event
If you enter a project's name that is located in your Objective Basic project folder it'll show you files matching the supplied patterns for specific directories:
The project:
I use ukHelper as a singleton, so I don't have to instantiate it.
Regards
Udo