Monday, May 25, 2020

#1 2020-05-21 10:02:05 pm

BabyG
Member
Registered: 2019-11-25
Posts: 5

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:

Applescript:

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


Filed under: mdfind, white space

Offline

 

#2 2020-05-22 12:11:15 am

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

Re: Trouble with white spaces

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:

Applescript:

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:

Applescript:

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


NG

Offline

 

#3 2020-05-22 12:50:32 am

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

Re: Trouble with white spaces

I test your code...

And with or without single quote in the second line of your code. It gave me different result.

Without single quote it give me character before --> File

With single quote it only give me directory name of --> File and Folder


Applescript:

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


I  have skied the highest mountain without logic, and still its the imagination that take me anywhere. Its the desire of something that matters for any knowledge I have or could learn.

Offline

 

#4 2020-05-22 12:58:16 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6362

Re: Trouble with white spaces

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

Applescript:

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*"}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2020-05-22 05:16:02 am

BabyG
Member
Registered: 2019-11-25
Posts: 5

Re: Trouble with white spaces

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

Offline

 

#6 2020-05-22 08:14:05 am

BabyG
Member
Registered: 2019-11-25
Posts: 5

Re: Trouble with white spaces

Nigel Garvey wrote:

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:

Applescript:

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:

Applescript:

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



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:

Applescript:

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.

Offline

 

#7 2020-05-22 04:51:19 pm

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

Re: Trouble with white spaces

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):

Applescript:

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


NG

Offline

 

#8 2020-05-22 05:52:39 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6362

Re: Trouble with white spaces

Or without quotes:

Applescript:

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"}


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#9 Yesterday 05:50:19 pm

BabyG
Member
Registered: 2019-11-25
Posts: 5

Re: Trouble with white spaces

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!

Offline

 

#10 Yesterday 06:18:43 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6362

Re: Trouble with white spaces

BabyG wrote:

1) I have tried using 'c' and '[c]' to make searches case insensitive, but I can't quite get it to work. Any suggestions?



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.

2) Is there a way to restrict searches to a certain level of subfolders?



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.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)