Aided by Adam Bell’s great tutorial (http://macscripter.net/articles/460_0_10_0_C/) on Spotlight scripting, I was able to set up a Spotlight query to find scripts and other specific documents. After filtering some spurious results that Spotlight delivers on my machine (sometimes PDF files turn up here…) a list of results is presented, where the user can select a document to be opened.
In the last line of the script, opening that selection produces an error (“Can’t get POSIX file …”)
What’s wrong here…?
try
set fCreat to file creator of (info for (path to frontmost application))
on error
set fCreat to "unknown"
end try
beep
set r to button returned of (display dialog "Which kind do you want to search..?" buttons {"All", fCreat, "A specific one"} default button 2 with icon 1)
if r ≠"All" and r ≠fCreat then
set this_item to choose file with prompt "Which kind of file to search for..?"
--set {folder:Fl, name:Nm, name extension:Ex} to (info for this_item)
tell application "Finder" to set Q to this_item's name extension
else
set Q to ""
end if
beep
set Q2 to text returned of (display dialog "Enter a phrase to search:" default answer "" buttons {"Cancel", "OK"} default button 2) --"periodical" -- to make clear what the query is, but could be in the shell script.
-- quoted form is not really necessary for ".scpt", but it's good practice in shell scripts and does no harm in a query argument.
set t to paragraphs of (do shell script "mdfind " & Q2 & " -onlyin / " & Q) --where it contains "LSPrebindings" -- mdfind returns a newLine delimited string
-- filter nonsense
set tot to {}
repeat with tt in t
if "LSPrebindings" is not in tt and "Webhistory" is not in tt and "vRge08Message" is not in tt and "vRgeMessage" is not in tt and ".brbk" is not in tt then set tot to tot & tt
end repeat
set t to tot
if t ≠{} then
set tD to choose from list t
else
display dialog "Nothing found !" buttons {"OK"} default button 1
end if
set tPos to tD's item 1
if tD is not false then tell application "Finder" to open POSIX file tPos