Reveal search findings, shellscript mdfind with variable like search

Hi all,

i wrote this script to find a subfolder in the document folder without knowing its full name and i try to open it (yet to be coded).

Does anyone know a way to reveal the findings or to open the path/paths (the result of thefile)?
Does anyone have an idea how to put the search term “E10” into a variable?

I am kind of stuck here :slight_smile:

Here is the code:

set SPfad to POSIX path of (path to documents folder)
set thefile to do shell script "/usr/bin/mdfind -onlyin " & quoted form of SPfad & " 'kMDItemFSName LIKE \"E10'"

Thanks a lot for the help and have a nice weekend.

Hi. Something along these lines should work.


set searchTerm to "E10"

tell ((do shell script "mdfind -onlyin " & (path to documents folder)'s POSIX path & space & (("kMDItemKind == Folder") & " && " & ("kMDItemFSName" & " == " & ("*" & searchTerm & "*")'s quoted form))'s quoted form)'s paragraphs) to if not it is {} then
	its item 1 as POSIX file
	tell application "Finder" to open result
else
	"No indexed folder contains that term... better luck next time."
end if

Hi Marc Anthony,

thanks a lot for your support.
Got a nice knote in my head now. :stuck_out_tongue:

Unfortunatly your code does not work for me.
No matter what folder i put in the document folder while runing the script I always get:

For example i put the folder “E102323” in. Still no positiv finding.
With my script the folders were found but i was not able to open them or to put the search criteria into a variable.

Maybe i missed sth. thanks again in advance.

Marc’s script works for me, although it’s excruciatingly and unnecessarily obfuscated. (The unobfuscated form’s bad enough!) It only opens the first find and you may have to wait for Spotlight to scan the documents folder again before any recently added items show up in the results.

Thanks Nigel and Anthony,

unfortunatly its not working for me.

With my script i find the folders/files:

“/Users/Felix/Documents/E10.rtf
/Users/Felix/Documents/E10
/Users/Felix/Documents/E1022323”

It would be totally fine to just open the first
and ignor the rest.

Nigel do you have an idea how to keep the script a little less unobfustcated?

Well. As I said above, the unobfuscated version’s hardly any better. :slight_smile:


set searchTerm to "E10"

set matches to (do shell script ("mdfind -onlyin " & quoted form of POSIX path of (path to documents folder) & space & ¬
	quoted form of ("kMDItemKind == Folder && kMDItemFSName == \"*" & searchTerm & "*\"")))
if (matches is not "") then
	set firstMatch to (paragraph 1 of matches) as POSIX file
	tell application "Finder" to open firstMatch
else
	"No indexed folder contains that term... better luck next time."
end if

The “first” folder the script finds is the one which just happens to be at the top of the list returned by mdfind. They seem to be returned in creation order on my machine, not name order, so oldest first. But I haven’t looked into this very thoroughly.

Marc developed the script on the understanding that you wanted to find and open a subfolder, but the first item listed in your post immediately above is a file. Would you care to clarify what you actually want?

Hello Nigel,

the idea is to find the first folder.
I just put the file in for testing.

just removed it…

This is the path: /Users/Felix/Documents/E10/

set searchTerm to "E10"

set SPfad to POSIX path of (path to documents folder)

do shell script "mdfind -onlyin " & (path to documents folder)'s POSIX path & space & (("kMDItemKind == Folder") & " && " & ("kMDItemFSName" & " == " & ("*" & searchTerm & "*")'s quoted form))'s quoted form)'s paragraphs) to if not it is {} then
	its item 1 as POSIX file
	tell application "Finder" to open result
else
	"No indexed folder contains that term... better luck next time."
end if

With this script i only get the else argument

With my script however:

set SPfad to POSIX path of (path to documents folder)
set thefile to do shell script "/usr/bin/mdfind -onlyin " & quoted form of SPfad & " 'kMDItemFSName LIKE \"E10'"

I find every likewise folder
Result:

…i guess there is a brainbug. :lol:

I don’t think “LIKE” is an mdfind operator. On my machine, your script returns a subset of items from the folder, including the three “E10…” folders I created there yesterday. The other items are all files, none of which have “E10” in their names. :confused:

The ‘do shell script’ part of Marc’s script returns just the three “E10…” folders. Note that the search is case sensitive and I haven’t been able to make the case-insensitivity operator [c] work in this context. A case-insensitive search would therefore have to be …

set SPfad to POSIX path of (path to documents folder)
set thefile to do shell script "/usr/bin/mdfind -onlyin " & quoted form of SPfad & " 'kMDItemKind == Folder && (kMDItemFSName == \"E10*\" || kMDItemFSName == \"e10*\")'"

… which of course requires two versions of the search term.

Not strictly relevant for your purposes, but I’ve found that where the search term contains apostrophes or single-quotes, my version of Marc’s script does better than the original. The relevant difference is that mine wraps the search term in escaped double-quotes, while his uses the quoted form of a quoted form. The quoted form of the quoted form of “NG’s AppleScripts” is “‘\’‘NG’\‘’\‘\’‘’\''s AppleScripts’\‘’'”, whereas what’s needed with nested single-quotes is “‘\’‘NG’\\\''s AppleScripts’\‘’'”.

Hi again.

As proven LIKE in mdfind works. The case sensitivity is irrelevant.

This is exactly what i want. I don’t know the full name but i know there is only one sub folder in the given path folder which has the part “E10”… and sth in it.

So the question is how do i set the “E10” to a variable and how can I open the found sub folder which has the found path (result of the search mdfind).

Just assume there is only one folder with this attribute (name: E10…) in the documents folder.

Still no clue … so far.

If you know there’s only one such item in the folder, and that it’s your subfolder, you could try this:

set SPfad to POSIX path of (path to documents folder)
set thefile to do shell script "/usr/bin/mdfind -onlyin " & quoted form of SPfad & " -name 'E10'" -- Wildcard not needed.

Or if you do need to check it’s a subfolder, perhaps:

set SPfad to POSIX path of (path to documents folder)
set thefile to do shell script "/usr/bin/mdfind -onlyin " & quoted form of SPfad & " 'kMDItemKind == Folder && kMDItemDisplayName == \"E10*\"'"

If neither of these work, I don’t know what else to suggest. Your computer seems to be following different rules from Marc’s or mine. Maybe you have to use your localised term instead of “Folder”. :confused:

Dear Anthony,
Dear Nigel,

I got it working.

set searchTerm to "E101113"
set SPfad to POSIX path of (path to documents folder)
set thefile to ((do shell script "mdfind -onlyin " & SPfad & space & (("kMDItemFSName" & " == " & (searchTerm & "*")'s quoted form))'s quoted form)'s paragraphs)

tell application "Finder" to open thefile as POSIX file
return thefile

works perfect! :slight_smile:

Who is Anthony? :o

It appears that the problem was that your item was—rather unexpectedly—a file, however, realize that by removing the leading wildcard, it no longer means “contains” but “begins with.” Your edit will fail if there’s a space in the path; paths should always be quoted. Removing my original if consideration also opens the door for potential problems.

Nigel was correct that the second quoted form in my example may cause certain characters to not be properly interpreted; one quoted form is sufficient:

do shell script "mdfind -onlyin " & (path to documents folder)'s POSIX path's quoted form & space & "kMDItemFSName = " & ("*" & searchTerm & "*")'s quoted form

I was also not able to implement the case sensitive option using mdfind and can only assume there’s a bug; that could be circumvented by using a shell find, rather than mdfind; it works in that setting.

I’m curious how your original code could possibly have worked, as it’s absent an equals operand and features the LIKE mystery operand; that may have been a misreading of the live option, as I find no documentation in the man page or online for its existence. Would you mind posting (1) which OS version you’re using, and (2) the actual result from the event log when you ran that code?

–edited for grammar/typo

Hello Marc Anthony,

i am sry for the confusion. I think the “LIKE” term was just ignored.

For your interest i use this script to search for several folders after an sql requested list creation in the program called Ragtime. The list gives me several findings and the script gives me the possibilty to find the folder in the file system.
Just perfect! Many thanks for your input guys!

tell application "RagTime 6.6"
	set searchTerm to value of selection of window 1 as string
end tell

tell application "Finder"
	try
		set SPfad to "/Volumes/Ablage/Dokumentation/Produkte/" as text
		set thefiles to ((do shell script "mdfind -onlyin " & SPfad & space & (("kMDItemFSName" & " == " & (searchTerm & "*")'s quoted form))'s quoted form)'s paragraphs)
		
		tell application "Finder" to open first item of thefiles as POSIX file
		#set dialog1 to first item of thefiles
		#display dialog dialog1
		
	on error
		display dialog "nothing found!"
	end try
end tell

:smiley:

Hello again,

the next problem is how to set up just a sub folder find.
I tried this: :stuck_out_tongue:

tell application "Finder"
	set searchTerm to the selection the clipboard
	try
		set SPfad to "/Volumes/Ablage/Dokumentation/Produkte/" as text
		
		set thefiles to ((do shell script "mdfind -onlyin " & SPfad & space & (("kMDItemKind" & " == " & space & "folder" & space & "AND" & space & "kMDItemFSName" & " == " & (searchTerm & "*")'s quoted form))'s quoted form)'s paragraphs)
		
		tell application "Finder" to open first item of thefiles as POSIX file
		set dialog1 to first item of thefiles
		display dialog dialog1
		
	on error
		display dialog "nix da"
	end try
end tell

The Return was:

tell application “Finder”
the clipboard
→ “E120277,”
do shell script “mdfind -onlyin /Volumes/Ablage/Dokumentation/Produkte/ ‘kMDItemKind == folder AND kMDItemFSName == ‘\’‘E120277,‘\’‘’"
→ error number -10004
end tell
tell current application
do shell script "mdfind -onlyin /Volumes/Ablage/Dokumentation/Produkte/ 'kMDItemKind == folder AND kMDItemFSName == ‘\’'E120277,
’\’‘’”
→ “”
end tell
tell application “Finder”
open file “:1”
→ error number -10010
display dialog “nix da”
→ {button returned:“OK”}
end tell
Ergebnis:
{button returned:“OK”}

Do you guys know whats this all about?
Thanks in advance

Felix

Just found… it was “folder”… case sensivity correct is “Folder”
nevermind thanks anyways.

I hope someone can use this code somehow.
Kind regards
Felix

tell application "Finder"
	#set searchTerm to the selection as string
	set searchTerm to the selection the clipboard
	#set searchTerm to "E114197"
	try
		set SPfad to "/Volumes/Ablage/Dokumentation/Produkte/" as text
		
		set thefiles to ((do shell script "mdfind -onlyin " & SPfad & space & (("kMDItemContentType" & " == " & space & "public.folder" & space & "&&" & space & "kMDItemFSName" & " == " & (searchTerm & "*")'s quoted form))'s quoted form)'s paragraphs)
		
		tell application "Finder" to open first item of thefiles as POSIX file
		#set dialog1 to first item of thefiles
		#display dialog dialog1
		
	on error
		display dialog "nix da"
	end try
end tell

If I understand correctly, you may clean a bit your code :

-- tell application "Finder" # USELESS
#set searchTerm to the selection as string
set searchTerm to the selection the clipboard
#set searchTerm to "E114197"
try
	set SPfad to "/Volumes/Ablage/Dokumentation/Produkte/" as text
	
	set thefiles to ((do shell script "mdfind -onlyin " & SPfad & space & (("kMDItemContentType" & " == " & space & "public.folder" & space & "&&" & space & "kMDItemFSName" & " == " & (searchTerm & "*")'s quoted form))'s quoted form)'s paragraphs)
	
	tell application "Finder" to open first item of thefiles as POSIX file
	#set dialog1 to first item of thefiles
	#display dialog dialog1
	
on error
	display dialog "nix da"
end try
-- end tell # USELESS

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 7 février 2019 19:55:05

Hi Yvan,

thanks for the hint. You are absolutely right. :slight_smile:
The clean code is:



set searchTerm to the selection the clipboard 

try
   set SPfad to "/Volumes/Ablage/Dokumentation/Produkte/" as text # your path
   
   set thefiles to ((do shell script "mdfind -onlyin " & SPfad & space & (("kMDItemContentType" & " == " & space & "public.folder" & space & "&&" & space & "kMDItemFSName" & " == " & (searchTerm & "*")'s quoted form))'s quoted form)'s paragraphs)
   
   tell application "Finder" to open first item of thefiles as POSIX file

on error
   display dialog "nix da"
end try