Wednesday, February 20, 2019

#1 2019-02-02 02:19:23 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

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 smile


Here is the code:

Applescript:

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.

Last edited by xilefotto (2019-02-02 02:54:33 am)


Filed under: Search, Script, shell, mdfind

Offline

 

#2 2019-02-03 04:27:54 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 851

Re: Reveal search findings, shellscript mdfind with variable like search

Hi. Something along these lines should work.

Applescript:


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

Offline

 

#3 2019-02-05 02:02:20 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

Re: Reveal search findings, shellscript mdfind with variable like search

Hi Marc Anthony,

thanks a lot for your support.
Got a nice knote in my head now. 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:

"No indexed folder contains that term... better luck next time."



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.

Last edited by xilefotto (2019-02-05 02:02:40 am)

Offline

 

#4 2019-02-05 08:45:00 am

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

Re: Reveal search findings, shellscript mdfind with variable like search

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.


NG

Offline

 

#5 2019-02-05 10:35:15 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

Re: Reveal search findings, shellscript mdfind with variable like search

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?

Offline

 

#6 2019-02-05 12:14:10 pm

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

Re: Reveal search findings, shellscript mdfind with variable like search

xilefotto wrote:

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.  smile

Applescript:


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?


NG

Offline

 

#7 2019-02-06 12:45:41 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

Re: Reveal search findings, shellscript mdfind with variable like search

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/

Applescript:

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

"No indexed folder contains that term... better luck next time."



With my script however:

Applescript:

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:

/Users/Felix/Documents/E10




..i guess there is a brainbug. lol

Offline

 

#8 2019-02-06 04:48:32 am

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

Re: Reveal search findings, shellscript mdfind with variable like search

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.  hmm

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 …

Applescript:

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*'\\'''".

Last edited by Nigel Garvey (2019-02-06 04:49:51 am)


NG

Offline

 

#9 2019-02-06 06:42:58 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

Re: Reveal search findings, shellscript mdfind with variable like search

Hi again.

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.  hmm



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.

Offline

 

#10 2019-02-06 07:42:43 am

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

Re: Reveal search findings, shellscript mdfind with variable like search

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

Applescript:

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:

Applescript:

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".  hmm


NG

Offline

 

#11 2019-02-06 08:41:22 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

Re: Reveal search findings, shellscript mdfind with variable like search

Dear Anthony,
Dear Nigel,

I got it working.

Applescript:

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! smile

Last edited by xilefotto (2019-02-06 09:23:51 am)

Offline

 

#12 2019-02-06 08:32:17 pm

Marc Anthony
Member
From:: Dallas, TX
Registered: 2006-04-27
Posts: 851

Re: Reveal search findings, shellscript mdfind with variable like search

xilefotto wrote:

Dear Anthony,


Who is Anthony? yikes

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:

Applescript:

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

Last edited by Marc Anthony (2019-02-07 04:52:38 pm)

Offline

 

#13 2019-02-07 03:00:24 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

Re: Reveal search findings, shellscript mdfind with variable like search

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!

Applescript:

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

big_smile

Offline

 

#14 2019-02-07 11:14:27 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

Re: Reveal search findings, shellscript mdfind with variable like search

Hello again,

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

Applescript:

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

Offline

 

#15 2019-02-07 11:17:06 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

Re: Reveal search findings, shellscript mdfind with variable like search

Just found.. it was "folder"... case sensivity correct is "Folder"
nevermind thanks anyways.

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

Applescript:

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

Last edited by xilefotto (2019-02-07 11:26:37 am)

Offline

 

#16 2019-02-07 12:55:28 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 3325

Re: Reveal search findings, shellscript mdfind with variable like search

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

Applescript:

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

Offline

 

#17 2019-02-08 12:07:22 am

xilefotto
Member
Registered: 2019-02-01
Posts: 10

Re: Reveal search findings, shellscript mdfind with variable like search

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



Hi Yvan,

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

Applescript:



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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)