Ok, i have a script that outputs a shell find script for audio wav files as a POSIX path to the file name. It also returns a list of the file names that may exist, if there is more than one file having the name. Sometimes there are files 1-X. i.e. (\Volumes\Works\file1.wav, \Volumes\Works\file2.wav, \Volumes\Works\file3.wav…)
I can open 1 posix path with “reveal posix file .…” if i end the path at the the containing folder, but how do i essentially say “open the folder containing the files that you found.” The only thing i can think of is to drop all characters or text back to “Works” but that directory may change and isn’t what i’d like to do. Am i missing some syntax or command that i can use. The goal is to take the POSIX file(s) that was found and open the finder window with them highlighted. That way i can simply hit the space bar and “quicklook” them. I’ve done this with automator but am trying to script it now.
I was going to upload the code but ran into problems when i tried to open the .app on another computer and it told me that the classic environment was not supported, so i don’ have the specific example.
One way would be to ask Finder for the container of each file and open that. There are other techniques, too like using text item delimiters to extract just the portion of the string before the final slash. Or, depending on your pathnames, you might be able to do it all inside the shell script (find, xargs, dirname, and open).
set posixpathnameList to {¬
"/Users/username/Documents/Go SGFs/godojo/Kogo's Joseki Dictionary.sgf", ¬
"/Users/username/Documents/Go SGFs/GTL - others/4742-StaticE-dato-Konoca.sgf", ¬
"/Users/username/Documents/Go SGFs/GTL - others/4853-dfelcan-Kountch-andsok75.sgf"}
openDirsOfPosixPathnames(posixpathnameList)
to openDirsOfPosixPathnames(posixpathnameList)
set alreadyOpenedDirs to {}
repeat with posixpathname in posixpathnameList
set posixpathname to contents of posixpathname -- deref implict ref in "repeat with x in l" loop
set fileRef to POSIX file posixpathname
tell application "Finder" to set dir to container of fileRef as alias
set dirPath to POSIX path of dir
if alreadyOpenedDirs does not contain dirPath then
tell application "Finder" to open dir
set end of alreadyOpenedDirs to dirPath
end if
end repeat
return alreadyOpenedDirs
end openDirsOfPosixPathnames
That is generalized for the case where all the files are not in a single directory. It is a bit verbose because Finder does not really work directly with POSIX file. The alreadyOpenedDirs could be eliminated without a huge impact in most situations (it is there to prevent Finder from “trashing” around opening and re-opening various dirs if there are lots of dirs represented in the list and the list is not sorted so that all the files from each dir are listed consecutively).
Given a posix path to a file, here’s how you can open a Finder window and select it…
set theFile to "/Users/hmcshane/Desktop/test.txt"
tell application "Finder"
set theWindow to make new Finder window at front
set target of theWindow to (POSIX file theFile)
end tell
do shell script "/usr/bin/find ~/Downloads -iname '*.jpg' -maxdepth 1"
set foundItems to paragraphs of result
repeat with i from 1 to count foundItems
set item i of foundItems to (item i of foundItems as POSIX file)
end repeat
tell application "Finder"
activate
reveal foundItems
end tell
set theFile to "/Backup/Recordings$/Ray Cano Dv/08123456.m4a"
tell application "Finder"
set theWindow to make new Finder window at front
set target of theWindow to (POSIX file theFile)
end tell
I"m getting an error on the last line when i run this. Don’t know why.
-- gets case number and returns it as a record and then converts it to text
display dialog "What is the Case number? (Please enter without hyphen)" default answer "" buttons {"F2-Go..."} default button 1
set caseId to (text returned of result) as text
log caseId
-- remove hyphen if it was included
if third character of caseId is equal to "-" then
set cutYear to text 1 thru 2 of caseId
set cutCase to text 4 thru 9 of caseId
set caseId to cutYear & cutCase as string
end if
log caseId
-- searches for the audio file
set thefolder to "Backup:Recordings$"
set thefolder to quoted form of POSIX path of thefolder
set theFiles to (do shell script "find " & thefolder & " -type f -name " & caseId & "*.*")
--processes the results of shell script in loop and gets them to HFS
set foundItems to paragraphs of result
repeat with i from 1 to count foundItems
set item i of foundItems to (item i of foundItems as POSIX file)
end repeat
--opens results with Finder
tell application "Finder"
activate
reveal foundItems
end tell
@Bruce- i haven’t been able to find the beginner post for shell scripts, is there one. The most confusing thing has been each time i’ve seen a find shell script they all have different “controls or switches?”