on run
set pdffilename to (the clipboard) as text
set command to "/usr/bin/mdfind -name " & quoted form of pdffilename
set output to paragraphs of (do shell script command)
if output is not {} then
set command to "open -a 'Adobe Reader.app' " & quoted form of (item 1 of output)
do shell script command
end if
end run
First of all the script will only open the PDF if it finds a result. So please enter the following line in a new Terminal window to see if the mdfind command actually finds your PDF:
mdfind -name 'nameofyourpdf.pdf'
Second, the script might not be able to open the found PDF with Acrobat, so you might want to use the following line, which just uses the default app to open the document:
set command to "open " & quoted form of (item 1 of output)
If all this does not help, you might want to try this script to track the error:
on run
try
set pdffilename to (the clipboard) as text
set command to "/usr/bin/mdfind -name " & quoted form of pdffilename
set output to paragraphs of (do shell script command)
if output is not {} then
set command to "open -a 'Adobe Reader.app' " & quoted form of (item 1 of output)
do shell script command
else
tell me
activate
display dialog "We could not find a PDF named '" & pdffilename & "' on your Mac." buttons {"OK"} default button 1 with icon caution
end tell
end if
on error errmsg number errnum
tell me
activate
display dialog "Sorry, an error occurred:" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon stop
end tell
end try
end run
but I think the solution should be very, very simple:
All I’m trying to to is this:
Paste the contents of the clipboard in spotlight
and open it
I want to do this from a Filemaker script… Filemaker copies the contents of a field and runs an Applescript that pastes the content in Spotlight and opens the file)
My script assumes that the file name was already copied to the clipboard, so if you run it from the Script Editor while a file name is on the clipboard, it should give some result.
To verify if the script is working at all at your site, you may want to try this version, which asks you for the PDF file name:
on run
try
tell me
activate
display dialog "Please enter a PDF file name:" default answer ""
set dlgresult to result
end tell
set pdffilename to text returned of dlgresult
set command to "/usr/bin/mdfind -name " & quoted form of pdffilename
set output to paragraphs of (do shell script command)
if output is not {} then
set command to "open -a 'Adobe Reader.app' " & quoted form of (item 1 of output)
do shell script command
else
tell me
activate
display dialog "We could not find a PDF named '" & pdffilename & "' on your Mac." buttons {"OK"} default button 1 with icon caution
end tell
end if
on error errmsg number errnum
tell me
activate
display dialog "Sorry, an error occurred:" & return & return & errmsg & " (" & errnum & ")" buttons {"OK"} default button 1 with icon stop
end tell
end try
end run
Of course it is possible to open the Spotlight window, from within AppleScript, but afterwards it is not easy to get the found search results.
Therefor using the mdfind command line utility is much better, as it also queries the Spotlight database, but directly returns the found results.
Then you can use the initial script, because this already takes the PDF file name from the clipboard:
on run
-- here we take the PDF file name from the clipboard
set pdffilename to (the clipboard) as text
-- this command searches Spotlight for the PDF file
set command to "/usr/bin/mdfind -name " & quoted form of pdffilename
set output to paragraphs of (do shell script command)
-- here we open the found PDF file with Adobe Acrobat
if output is not {} then
set command to "open -a 'Adobe Acrobat Pro.app' " & quoted form of (item 1 of output)
do shell script command
end if
end run