I am having trouble performing Spotlight searches for files with apostrophes in their names. For example, I have a file with the name “getFolder’sSize.txt”. If I perform this search:
set theFolder to choose folder
set theFolder to quoted form of POSIX path of (theFolder as string)
set theText to quoted form of "*folder*"
set theExt to quoted form of "*.txt"
set searchString to "mdfind -onlyin " & theFolder & " " & quoted form of ("(kMDItemFSName = " & theText & "cdw) && (kMDItemFSName = " & theExt & "cd)")
do shell script searchString
I get lots of results, including the file “getFolder’sSize.txt”. If I perform this search:
set theFolder to choose folder
set theFolder to quoted form of POSIX path of (theFolder as string)
set theText to quoted form of "*folder's*"
set theExt to quoted form of "*.txt"
set searchString to "mdfind -onlyin " & theFolder & " " & quoted form of ("(kMDItemFSName = " & theText & "cdw) && (kMDItemFSName = " & theExt & "cd)")
do shell script searchString
I get this error:
If I try to use Metadata Lib to perform this search:
use script "Metadata Lib" version "2.0.0"
use scripting additions
set theFolder to choose folder
set theText to "*folder's*"
set theExt to "txt"
set fileTypeList to perform search in folders {theFolder} predicate string "kMDItemFSName ENDSWITH[c] '." & theExt & "' AND kMDItemFSName CONTAINS[c] '" & theText & "'"
I get this error:
I get the same error if the name is quoted.
Any help would be appreciated. Thanks.
Model: Mac mini (late 014)
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14
Thanks Shane. The first solution returned an empty list, but the second solution worked great. Is there a similar workaround when performing a search without Metadata Lib?
Hi. I’m not sure if you had some rationale for using mdfind; a standard shell find may be more flexible, as you can use regular expressions and more easily/understandably consider case. With a compound mdfind statement (1), the conjunctive phrase needs quoting. When compound, you don’t appear to be able to use the single quote character, although it works in the singular (2) form.
#1
do shell script "mdfind -onlyin " & (choose folder)'s POSIX path's quoted form & space & ("kMDItemFSName = " & "*folder*"'s quoted form & "c && " & "kMDItemFSName = " & "*.txt"'s quoted form)'s quoted form --changing to *folder's* won't work
#2
do shell script "mdfind -onlyin " & (choose folder)'s POSIX path's quoted form & space & ("kMDItemFSName = " & "*Folder's*"'s quoted form)
Thanks. Unfortunately, neither of those scripts worked (returned an empty list). I’m using mdfind because I want to search for files using Spotlight metadata attributes; if there’s another way to do that I’d love to know.
So, is it the case, then, that without Metadata Lib, you can’t do an Applescript Spotlight search for a name that has an apostrophe in it? That seems like a pretty big flaw in the system.