searching for folder and opening it

Hi everybody, I am getting nowhere with this.

Lets say the HD is called “HD1” and the folder “folder1”

I like to create a script that searches one hard disk (“HD1”) for the title of a specific folder (“folder1”) and next opens the folder.

Any tips or suggestions?

Thanks

Browser: Safari 537.86.5
Operating System: Mac OS X (10.9)

How many answers would be possible here :slight_smile:

First we can make a distinction between actually searching the volume or using spotlight for this.

Spotlight
using a do shell script:

-- kMDItemKind is an localized string
-- Folder should be Map if your system is Dutch for instance
do shell script "mdfind -onlyin '/Volumes/HD1' -0 'kMDItemKind = \"Folder\" && kMDItemFSName = \"folder 1\"' | xargs -0 -J % open \"%\""

Search disk

do shell script "find /Volumes/HD1 -type d -iname 'folder 1' -exec open {} \\; 2>/dev/null || true"

Searching spotlight can be done using an osax like AppleScript Toolbox or using AppleScriptObjC and NSPredicate. All these commands, including the do shell script example above makes use of the same metadata database called spotlight.

The find search is a bit different. The find shell command search the entire path recursive for any directory with the name ‘folder 1’. However in the shell bundles/packages are the same as directories and they’re not skipped. For an non indexed HD find is probably the fastest way to go, it can get unexpected results. Also you can solve this by searching by the script itself, use extended listing folder commands in osaxen like AppleScript Toolbox or use AppleScriptObjC using an NSPredicate.

Good morning DJ

Thanks for your quick reply. I saw it yesterday but only had time this this morning to look at it. Brilliant. The first solution works a dream, thank you. the second one not but since I have the first that is irrelevant. Thanks again I would not have been able to do this myself.

as you guys say dankjewel.

Hi DJ

I am implementing your script today (my last reply was just based on a quick test) and it does not work any longer.

I get: error “The files /Volumes/Data and /E/DataBases/FF do not exist.” number 1

I have amended the script as follows to point to a internal HD and a folder on it.

do shell script "mdfind -onlyin '/Volumes/Data E' -0 'kMDItemKind = \"Folder\" && kMDItemFSName = \"FF\"' | xargs -J % open \"%\""

Now I suspect this is because of the space in my HD name. I tried to solve this with an _ (Data_E) but than it returns: “”

FYI:
The file does exist on the HD.
There are several HD on my computer, and this one is not the startup disk.

Like Change Agent I can’t get the given code to work when I try to apply it to a volume name “Macintosh HD”.

-- kMDItemKind is an localized string
-- Folder should be Map if your system is Dutch for instance
--do shell script "mdfind -onlyin '/Volumes/Macintosh HD/' -0 'kMDItemKind = \"Dossier\" && kMDItemFSName = \"_X28544Æ’\"' | xargs -J % open \"%\""
# Fails with the error 
(*"The files /Volumes/Macintosh, /HD/Users/Important/pour, and /ebay/_X28544Æ’ do not exist." number 1*)

As I am curious I decided to split the instruction in two components.
1 - the one locating the folder
I starting searching a resource from which I may grab the localized string kMDItemKing.
As I don’t know where it is stored with this name, I searched for the string Folder and I got one (I guess that there are other ones but one is sufficient.

(path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle"
set Folder_loc to localized string "Folder" from table "InfoPlist" in bundle (result as «class furl»)

--set theFolder to do shell script "mdfind -onlyin '/Volumes/Macintosh HD/' -0 'kMDItemKind = \"Dossier\" && kMDItemFSName = \"_X28544Æ’\"'"

set theFolder to do shell script "mdfind -onlyin '/Volumes/Macintosh HD/' -0 'kMDItemKind = \"" & Folder_loc & "\" && kMDItemFSName = \"_X28544Æ’\"'"
--> "/Volumes/Macintosh HD/Users/Important/pour ebay/_X28544Æ’"

As this first part behaves well I understood that the culprit is the second part.

2 - the one trying to open the extracted folder.

# Example in the man for open :
do shell script "open '/Volumes/Macintosh HD/Applications/'" # WORKS !
# Define the path as it is extracted by part 1
set theFolder to "/Volumes/Macintosh HD/Users/Important/pour ebay/_X28544Æ’"
# Add the ending slash to match the man example
if theFolder does not end with "/" then set theFolder to theFolder & "/"
# I tried :
--do shell script "open " & quoted form of theFolder # which failed
# I tried also the hardcoded version :
do shell script "open '/Volumes/Macintosh HD/Users/Important/pour ebay/_X28544Æ’/'" # which failed too

(*error "sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file" number 2*)

I’m curious but I’m far to be patient so I decided to drop Shell Script the second part and use the Finder because my attempts to use System Events failed.

(path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle"
set Folder_loc to localized string "Folder" from table "InfoPlist" in bundle (result as «class furl»)

set thePaths to do shell script "mdfind -onlyin '/Volumes/Macintosh HD/' -0 'kMDItemKind = \"" & Folder_loc & "\" && kMDItemFSName = \"AppleScript\"'"
--> "/Volumes/Macintosh HD/Users/Important/PDFs/ Apple ƒ/AppleScript/Volumes/Macintosh HD/Users/yvankoenig/Desktop/Documents partagés/AppleScript/Volumes/Macintosh HD/Users/Important/Téléchargements/ThisService Starting Points/AppleScript/Volumes/Macintosh HD/Users/Important/Téléchargements/thisService ƒ/ThisService-2.0.2/ThisService Starting Points/AppleScript"
set thePaths to items 2 thru -1 of my decoupe(thePaths, "/Volumes/")
repeat with apath in thePaths
	set thePaths to POSIX file ("/Volumes/" & apath)
	tell application "Finder" to open thePaths
end repeat

#=====

on decoupe(t, d)
	local oTIDs, l
	set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
	set l to text items of t
	set AppleScript's text item delimiters to oTIDs
	return l
end decoupe

#=====

Does the job but I was forced to add some instructions because when several folders are found, mdfind return their pathnames with no separator.
I used /Volumes/ to split the flow in valid components.

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) lundi 23 mai 2016 21:43:04

Sorry I forgot the -0 argument in xargs when using the -0 argument in mdfind. I have updated the command above.

The problem was that the found results were not send as an argument to the command open as a whole. Sorry for the late reply, I overlooked your post ChangeAgent.

Thanks Yvan

It works! Much appreciated and I learn from what you are doing. Took me a bit to follow it but all the same.

Hi DJ! It works, super, many thanks for your help! I will use it in a FMP database.

Thanks DJ.
Here is an edited version which adjust itself to the localisation.

(path to library folder from system domain as text) & "CoreServices:CoreTypes.bundle"
set Folder_loc to localized string "Folder" from table "InfoPlist" in bundle (result as «class furl»)
-- kMDItemKind is an localized string grabbed in Folder_loc

set sourceFolder to "Macintosh HD"
set quotedPosixSourceFolder to quoted form of POSIX path of sourceFolder
set searchedFolder to "AppleScript"

do shell script "mdfind -onlyin " & quotedPosixSourceFolder & " -0 'kMDItemKind = \"" & Folder_loc & "\" && kMDItemFSName = \"" & searchedFolder & "\"' | xargs -0 -J % open \"%\""

@ChangeAgent, thanks for the feedback.

Yvan KOENIG running El Capitan 10.11.5 in French (VALLAURIS, France) mardi 24 mai 2016 12:05:01

Thanks Yvan :cool:

Just a beginner here. Put the edited version adjusted for localization into the script editor and nothing really happens. No errors but the specific folder does not open up.

Hi silba. Welcome to MacScripter.

It does work if your hard disk name’s “Macintosh HD” and you have a folder called “AppleScript” on it in an area that Spotlight’s allowed to scan. (I had to change the value of ‘sourceFolder’ to get it to work on my own machine and I just happen to have a folder called “AppleScript”. :slight_smile: )

Yvan: If you’re interested, there’s a shorter (but unpublicised) way to code this:

(path to «class csrv» as text) & "CoreTypes.bundle"

Or:

(path to "csrv" as text) & "CoreTypes.bundle"

Hi Nigel,
Thanks for the welcome and quick reply. Quite possible I am in the wrong thread for this. All I want to do is search local volumes for a particular folder and open it up (possibly adapt it for networked volumes).
Thanks

You should be able to avoid the localization stuff by changing the search. So instead of this:

do shell script "mdfind -onlyin " & quotedPosixSourceFolder & " -0 'kMDItemKind = \"" & Folder_loc & "\" && kMDItemFSName = \"" & searchedFolder & "\"' | xargs -0 -J % open \"%\""

use this:

do shell script "mdfind -onlyin " & quotedPosixSourceFolder & " -0 'kMDItemContentType = \"public.folder\" && kMDItemFSName = \"" & searchedFolder & "\"' | xargs -0 -J % open \"%\""