You are not logged in.
Pages:: 1
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
Hi.
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
Online
… 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
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
Hi Fredrik.
I was actually questioning your use of |path| instead of valueForKey:("path").
NG
Online
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
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 Monterey - Script Debugger 8
Offline
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
Online
Pages:: 1