Trouble with white spaces

I feel like I must be missing something. I’m trying to write a script to find files by certain criteria using Spotlight metadata. However, if I try to find a file with white space in the name, nothing is returned. For example:

set theFolder to quoted form of POSIX path of (path to home folder)
set theText to quoted form of "*whatever and ever*"
set searchString to "/usr/bin/mdfind -onlyin " & theFolder & " ' kMDItemDisplayName = " & theText & " ' "
do shell script searchString

Nada. No problem finding the file if the search term is just “whatever”. Am I doing something wrong, or is this broken? Thanks.

Model: Mac Mini 2020
AppleScript: 2.5
Browser: Safari 605.1.15
Operating System: macOS 10.14

Hi.

Possibly it’s because you’ve got the quoted form of the item name inside text which is itself single quoted, which means that the quoted form bit is effectively unquoted in the search string. You could try the outer string in double quotes instead:

set theFolder to quoted form of POSIX path of (path to home folder)
set theText to quoted form of "*whatever and ever*"
set searchString to "/usr/bin/mdfind -onlyin " & theFolder & " \"kMDItemDisplayName = " & theText & "\""
do shell script searchString

Or nest quoted forms:

set theFolder to quoted form of POSIX path of (path to home folder)
set theText to quoted form of "*whatever and ever*"
set searchString to "/usr/bin/mdfind -onlyin " & theFolder & " " & quoted form of ("kMDItemDisplayName = " & theText)
do shell script searchString

You can also use my Metadata Lib and dispense with quoting altogether:

use AppleScript version "2.5" -- macOS 10.11 or later
use script "Metadata Lib" version "2.0.2"
use scripting additions

set resultAnyList to perform search in folders {"~/Desktop"} predicate string "kMDItemDisplayName LIKE %@" search arguments {"*whatever and ever*"}

Thanks to all! I never cease to be amazed at how incredibly helpful people on this site are.

So I tried both and they work - thanks so much. Now I have another problem. When I try to narrow down the search based on file extension, I get an error:

set theFolder to quoted form of POSIX path of (path to home folder)
set theText to quoted form of "*whatever and ever*"
set theExt to "*.txt"
set searchString to "/usr/bin/mdfind -onlyin " & theFolder & " " & quoted form of ("kMDItemDisplayName = " & theText) & " && (kMDItemFSName = " & theExt & ")"
do shell script searchString

error “sh: kMDItemFSName: command not found” number 127

Any suggestions? Thanks again.

Using the nested quoted form approach, “*.txt” needs to be quoted too and it appears that the two search conditions should be bracketed together under one quoted form. (They’re both in the same string within the shell script command):

set theFolder to quoted form of POSIX path of (path to home folder)
set theText to quoted form of "*whatever and ever*"
set theExt to quoted form of "*.txt"
set searchString to "/usr/bin/mdfind -onlyin " & theFolder & " " & quoted form of ("(kMDItemDisplayName = " & theText & ") && (kMDItemFSName = " & theExt & ")")
do shell script searchString

Or without quotes:

use AppleScript version "2.5" -- macOS 10.11 or later
use script "Metadata Lib" version "2.0.2"
use scripting additions

set resultAnyList to perform search in folders {"~/Desktop"} predicate string "(kMDItemDisplayName LIKE %@) AND (kMDItemFSName ENDSWITH[c] %@)" search arguments {"*whatever and ever*", ".txt"}

Many many thanks to you both again. I had not used Metadata Lib before, but I’ve now downloaded it and started to play with it (however, the project I am working on makes use of Dialog Toolkit Plus, and I’ve also enjoyed using FileManager and Myriad Tables - thanks for those, Shane!).

I have 2 more questions related to the project if you don’t mind:

  1. I have tried using ‘c’ and ‘[c]’ to make searches case insensitive, but I can’t quite get it to work. Any suggestions?
  2. Is there a way to restrict searches to a certain level of subfolders?

Enjoy the weekend!

The above Metadata Lib snippet shows how to use [c] – straight after the operator. I think for mdfind the c goes at the end of the predicate string.

Metadata Lib’s perform search command has a just in parameter that does what you want. However, Spotlight itself always returns recursive results, so this works by performing a search and discarding the unwanted ones.

Thanks again! Having a great time playing with Metadata Lib.