Tuesday, May 11, 2021

#1 2021-02-02 02:48:39 pm

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

Script that returns all files that contain a specific string

I wrote the following to return all files in a folder which contain " - ".

Applescript:

use framework "Foundation"
use scripting additions

set sourceFolder to POSIX path of (choose folder)

getFiles(sourceFolder, " - ")

on getFiles(sourceFolder, theString)
   set fileManager to current application's NSFileManager's defaultManager()
   set theFolder to current application's |NSURL|'s fileURLWithPath:sourceFolder
   set folderContents to fileManager's contentsOfDirectoryAtURL:theFolder includingPropertiesForKeys:{} options:(current application's NSDirectoryEnumerationSkipsHiddenFiles) |error|:(missing value)
   set thePred to current application's NSPredicate's predicateWithFormat_("(FSName CONTAINS[c] %@)", theString)
   set theFiles to folderContents's filteredArrayUsingPredicate:thePred
   return theFiles as list
end getFiles

The script returns the following error, apparently because the format of the predicate is not correct. I spent an hour or more online and with Shane's book but couldn't get the script to work. Thanks for the help.

error "[<NSURL 0x600002b91260> valueForUndefinedKey:]: this class is not key value coding-compliant for the key FSName." number -10000

Last edited by peavine (2021-02-02 03:22:31 pm)


2018 Mac mini - macOS Catalina

Offline

 

#2 2021-02-02 03:59:44 pm

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

Re: Script that returns all files that contain a specific string

Hi peavine.

The name of an item represented by an NSURL is its lastPathComponent. Similarly with a path represented by an NSString.


NG

Offline

 

#3 2021-02-02 05:08:26 pm

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

Re: Script that returns all files that contain a specific string

Thanks Nigel. That works great.


2018 Mac mini - macOS Catalina

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)