Wednesday, December 7, 2022

Announcement

MacScripeter.net will transition to a new Discourse server soon. Watch this space for the date and time of the transition. Please expect the site to experience periods of outage during this transition. See this and this for more details.

#1 2021-04-15 06:46:46 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 1092

NSFileManager, enumeratorAtURL and lastPathComponent

Example of a script that use lastPathComponent to filter a directory return POSIX path.

I try other approach to extract the NSResourceKey from the enumeration to search for NSURLNameKey but I find it to be slower and the approach below.

So when will it be a good approach to use:
NSResourceKey in enumeration ??, anyone like to share some example.


Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

property NSDirectoryEnumerationSkipsHiddenFiles : a reference to current application's NSDirectoryEnumerationSkipsHiddenFiles
property NSDirectoryEnumerationSkipsPackageDescendants : a reference to current application's NSDirectoryEnumerationSkipsPackageDescendants

set thePath to POSIX path of (choose folder)

my searchWithFilename:{"pbs.txt"} inPath:thePath

on searchWithFilename:theName inPath:thePath
   set theURL to current application's |NSURL|'s fileURLWithPath:thePath
   set manager to current application's NSFileManager's defaultManager()
   set directoryEnumerator to (manager's enumeratorAtURL:theURL includingPropertiesForKeys:{} options:((NSDirectoryEnumerationSkipsHiddenFiles as integer) + (NSDirectoryEnumerationSkipsPackageDescendants as integer)) errorHandler:(missing value))'s allObjects()
   set predicate to current application's NSPredicate's predicateWithFormat:"lastPathComponent ==[c] %@" argumentArray:theName
   set anArray to ((directoryEnumerator's |path|)'s filteredArrayUsingPredicate:predicate) as list
end searchWithFilename:inPath:

Last edited by Fredrik71 (2021-04-15 07:23:11 am)


Node-RED makes it easy to automate IoT

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)