Go to spotlight (from Filemaker), find clipboard contents and open it

Good morning everybody…

I am an absolute beginner in AppleScript and would like to get some help with my problem:

From Filemaker I would like to:

  • go to Spotlight
  • find the contents of the clipboard (filename.pdf)
  • open it in acrobat

this should be very easy
but since this is my very first try…

Greetz,

Dick

Hi,

That would be my first idea:


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

Have a nice Sunday!

Martin

I am sorry…

but it does not work

:frowning:

Hi Dick,

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

Hope that helps!

Martin

Nope…

:frowning:

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)

name of the file: “Offerte_20105028_1036.pdf”

I’m sorry to not understand the Terminator

:frowning:

Dick

Hi Dick,

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.

Best regards,

Martin

Hi martin

What happens, when I run the script, is:

a screen pops up in which I can paste the name of te pdf
(this should be done by applescript, since it’s already on the clipboard)

when I paste the contents of the clipboard…
I get an error message:

I have changed this in the script to:
Adobe Acrobat Pro

and now it works!

the only thing that needs to change is the automatic paste of the clipboard

How to do this?

greetz

dick

Hi Dick,

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

Hallelujah!!!

works like a dream!!!

thanks

Welcome to MacScripter :smiley: