Sunday, May 9, 2021

#1 2021-04-25 09:16:27 am

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

NSFileManager, enumeratorAtURL and pathsMatchingExtensions

Instead of using NSPredicate to filter directory enumeration of file extension.
There is a instance method: pathsMatchingExtensions
https://developer.apple.com/documentati … guage=objc

Here is a example.

Applescript:

use framework "Foundation"
use scripting additions

set thePath to POSIX path of (path to desktop)
my filesMatchingExtensions:{"scpt", "pdf", "webarchive"} inPath:thePath

on filesMatchingExtensions:theExtList inPath:thePath
   set theURL to current application's |NSURL|'s fileURLWithPath:thePath
   set manager to current application's NSFileManager's defaultManager()
   set enumOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
   set directoryEnumeratorAllObjects to (manager's enumeratorAtURL:theURL includingPropertiesForKeys:{} options:enumOptions errorHandler:(missing value))'s allObjects()
   return ((directoryEnumeratorAllObjects's |path|)'s pathsMatchingExtensions:theExtList) as list
end filesMatchingExtensions:inPath:

Last edited by Fredrik71 (2021-05-01 09:29:26 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#2 2021-05-01 03:22:50 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5378

Re: NSFileManager, enumeratorAtURL and pathsMatchingExtensions

Hi.

Fredrik71 wrote:

Applescript:


return ((directoryEnumerator's |path|)'s …


Is this a legitimate alternative to …

Applescript:

return ((directoryEnumerator's valueForKey:("path"))'s …

… or just something which happens to work? (I realise that 'directoryEnumerator' is an array of URLs here, not an enumerator.)


NG

Offline

 

#3 2021-05-01 09:07:51 am

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

Re: NSFileManager, enumeratorAtURL and pathsMatchingExtensions

Nigel Garvey wrote:

… or just something which happens to work?


Hi Nigel.

The variable maybe are little misleading it use allObjects() at end. It contains all the remaining objects of the enumerator https://developer.apple.com/documentati … guage=objc

So you are right its a array of URL

Maybe directoryEnumeratorAllObjects is better variable smile

Last edited by Fredrik71 (2021-05-01 09:11:28 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

#4 2021-05-01 09:44:25 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5378

Re: NSFileManager, enumeratorAtURL and pathsMatchingExtensions

Hi Fredrik.

I was actually questioning your use of |path| instead of valueForKey:("path").


NG

Offline

 

#5 2021-05-01 10:00:29 am

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

Re: NSFileManager, enumeratorAtURL and pathsMatchingExtensions

Sorry I misunderstood...

I know from the examples I did long time ago with NSURL and learn from that exercise I could use
path to translate fileURL to POSIX path. If that is the correct way or not I do not know.
https://developer.apple.com/documentati … guage=objc

Its also possible to do things like this...

Applescript:

return (directoryEnumeratorAllObjects's fileURL)'s |description|() as text

Applescript:

   return (directoryEnumeratorAllObjects's absoluteURL)'s |description|() as text

Ex.
directoryEnumeratorAllObjects's |path| works
directoryEnumeratorAllObjects's |path|() do not... same for example for above.

Here is other example.

Applescript:

set a to {key1:1}
set d to current application's NSDictionary's dictionaryWithDictionary:a

return (d's valueForKey:"key1") as integer --> 1
return (d's key1) as integer --> 1
return (key1 of d) as integer --> 1

So this will also work...

Applescript:

return ((|path| of directoryEnumeratorAllObjects)'s ...

Last edited by Fredrik71 (2021-05-01 12:52:38 pm)


if you are the expert, who will you call if its not your imagination.

Offline

 

#6 2021-05-02 07:25:20 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 871

Re: NSFileManager, enumeratorAtURL and pathsMatchingExtensions

This thread reminded me of something Shane wrote in his book:

Essentially, when you try to get a property without using parentheses, something called key-value coding is used.



So, if I understand correctly, the first and second lines that begin with "(theFiles's" in the following script are two different ways of accomplishing the same thing and both work as expected. It seems like the third line that begins with "(theFiles's" should also work but it doesn't, although |path|() does work in other circumstances.

Applescript:

use framework "Foundation"
use scripting additions

on getFiles(theFolder)
   set theFolder to current application's |NSURL|'s fileURLWithPath:theFolder
   set fileManager to current application's NSFileManager's defaultManager()
   set enumOptions to (current application's NSDirectoryEnumerationSkipsPackageDescendants as integer) + (current application's NSDirectoryEnumerationSkipsHiddenFiles as integer)
   set theFiles to (fileManager's enumeratorAtURL:theFolder includingPropertiesForKeys:{} options:enumOptions errorHandler:(missing value))'s allObjects()
   
   (theFiles's |path|) as list -- works OK
   (theFiles's valueForKey:("path")) as list -- works OK
   (theFiles's |path|()) as list -- unrecognized selector error
end getFiles

getFiles(POSIX path of (path to documents folder))


2018 Mac mini - macOS Catalina

Offline

 

#7 2021-05-02 01:00:31 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5378

Re: NSFileManager, enumeratorAtURL and pathsMatchingExtensions

peavine wrote:

This thread reminded me of something Shane wrote in his book:

Essentially, when you try to get a property without using parentheses, something called key-value coding is used.


Ah yes. Thanks, peavine. It was worth reading that entire note again.


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)