Googling from FileMaker

HI

I am new to Applescript. I keep intending to get started, but never seem to find the time.

I do pretty well in Filemaker Pro’s scripting language, and recently created a solution that builds a boolean search string, and opens a URL for Google in Safari. I would like to create an applescript that filemaker can call. The sole function of the applescript is to paste the clipboard contents into the search field in Google and execute or (hit return). I can’t seem to figure out how to do this.

All help appreciated!

Stefan Garr

Hi,
I think the better way to do this is just enter the search straight into google’s format
ususally it is formatted this way:
http://www.google.com/search?q=****enter your terms here seperated by a space or +****&ie=UTF-8&oe=UTF-8
just pop what you want into the indicated space and it will preform the search.
Here is how i did it:

set theSearch to "applescript filemaker"
tell application "Safari"
         make new document
	set the URL of the front document to "http://www.google.com/search?q=" & theSearch & "&ie=UTF-8&oe=UTF-8"
end tell

This code will do a google search for the words applescript and filemaker.
Hope this helps,
JO

I think JO is right, here’s a script that I use that doesn’t rely on Safari:

set use_exact_phrase to true
if use_exact_phrase = true then
	set the_quote to "%22"
else
	set the_quote to ""
end if

set old_delim to AppleScript's text item delimiters
try
	set the_clip to (get the clipboard) as string
	set AppleScript's text item delimiters to return
	set the_clip to (text items of the_clip)
	set AppleScript's text item delimiters to "+"
	set the_clip to the_clip as string
	set AppleScript's text item delimiters to " "
	set the_clip to (text items of the_clip)
	set AppleScript's text item delimiters to "+"
	set the_clip to the_clip as string
	set AppleScript's text item delimiters to old_delim
	open location ("http://www.google.com/search?q=" & the_quote & the_clip & the_quote & "&ie=UTF-8&oe=UTF-8")
on error the_error
	set AppleScript's text item delimiters to old_delim
	return the_error
end try

Jon

Hi, and thanks for the replies.

I think I need to clarify what I need a bit better… The actual search string that goes to clipboard will be different each time the script is run. It is constructed as follows:

  1. the filemaker user adds a new record and adds a text string (which is the title of a song he wants the lyrics for" to a field called title.
  2. The next script step copies the contents of the “title” field to the clipboard
  3. The next step inserts the text (") into a temporary holding field called scratch
  4. The next step pastes the clipboard contents (the title of the song) into the scratch field after the quotes
  5. The next step inserts a closing quote after the title (")
    6 the next step inserts the text “lyrics” into the scratch field
  6. the last field cuts the contents of the “scratch” field to the clipboard
  7. The next script step opens the URL for Google (actually I’m using FaganFinders Google search window here at: http://www.faganfinder.com/google2.html)

So the only thing missing is the last step which is to paste the clipboard contents to the input field which is already flashing and ready for input, then it would be nice if the script would also execute the search. One of the available script steps in Filemaker is “perform applescript”, and it offers a container to paste the script into.

Hope this makes more sense.

Thanks again!

Stefan

Um, what? You don’t need to set values to the clipboard, create dummy fields, clear them, or even use FileMaker Pro for this at all. Try this:

property the_title : "Someday Never Comes"

set the_title to text returned of (display dialog "Enter a song title:" default answer the_title buttons {"Cancel", "OK"} default button 2 with icon 1)
open location ("http://www.google.com/search?q=%22" & (my plus_the_string(the_title)) & "%22+lyrics&ie=UTF-8&oe=UTF-8")

on plus_the_string(the_string)
	set old_delim to AppleScript's text item delimiters
	my set_atid(return)
	set the_string to (text items of the_string)
	my set_atid("+")
	set the_string to the_string as string
	my set_atid(" ")
	set the_string to (text items of the_string)
	my set_atid("+")
	set the_string to the_string as string
	my set_atid(old_delim)
	return the_string
end plus_the_string

on set_atid(the_delim)
	set AppleScript's text item delimiters to the_delim
end set_atid

Jon

Hi

Sorry if I’m not being clear enough…

While I know I don’t “need” to use Filemaker Pro to look up a songs lyrics, that is the solution that I am building. The idea is to keep a database of song lyrics. I want to be able to enter the tiltle as a text string ONCE, click a button to run a Filemaker script that calls another script to finish the job. Is it possible to use Applescript to paste the contents of the clipboard to a field or no?

Thanks again

Stefan

How about this:

tell application "FileMaker Pro" to set the_title to cellValue of cell "title" of current record of database 1 --adjust the cell name as necessary
open location ("http://www.google.com/search?q=%22" & (my plus_the_string(the_title)) & "%22+lyrics&ie=UTF-8&oe=UTF-8")

on plus_the_string(the_string)
    set old_delim to AppleScript's text item delimiters
    my set_atid(return)
    set the_string to (text items of the_string)
    my set_atid("+")
    set the_string to the_string as string
    my set_atid(" ")
    set the_string to (text items of the_string)
    my set_atid("+")
    set the_string to the_string as string
    my set_atid(old_delim)
    return the_string
end plus_the_string

on set_atid(the_delim)
    set AppleScript's text item delimiters to the_delim
end set_atid

Jon

Jonn’s method is a good one. You can even go one step further, and parse the results, if you like. I’ve got some further reading on this at http://danshockley.com/formposting.php and especially at http://danshockley.com/form_processing.php. The second link is a tutorial on POSTing web forms from within FileMaker, and even helps you get started on parsing out results pages. In your case, you are just doing a GET request (the fields are actually IN the URL itself, rather than attached to the URL request separately), but you can get the idea from what I explain there.

If I understand you correctly, the Safari window is already open (with the cursor in the search field), and the search string is already on the clipboard. Since the clipboard is system wide this should be easy to do. If Safari isn’t open to the right page/the search field doesn’t contain the cursor then this won’t work. Otherwise


tell application "System Events"
tell process "Safari"
set frontmost to true
keystroke "v" using command down
end tell
end tell

This requires GUI scripting so “Enable access for assistive devices” must be turned on in the the “Universal Access” pane of System Preferences.

GUI Scripting is a hack for when an application doesn’t properly support AppleScript. In this case, you do not need to use the clipboard, so you shouldn’t. Imagine how annoying it would be if the user had something else THEY wanted in the clipboard (and nowhere else), and your script rudely wiped that out. Jonn8’s example is how it can be done without resorting to strange hacks. FileMaker lets you get the value from a field, and AppleScript lets you tag that onto a search URL. No GUI hacking required.

Hi Ray

Thanks for all your excellent help. As it happens, I had figured another way to do all this in Filemaker without even using Applescript. I did it by doing a Google search that looked like this: “song name” lyrics chords. I entered this in the search field in Google, then took the resulting URL and substituted the “song name” part of the field only in a calculated field. Then, I set the “Open URL” value equal to the Calculated Field Value. This worked like a champ.

The other approach, using your last piece of advice (pasting a calculated search string) also worked great, with the caveat that I had to program a pause for 5 seconds into the Filemaker script between the open URL for Google, and the Applescript that did the pasting.

I can see that the way I was insisting on doing it is really not the cleanest way.

I’m going to go back to the earlier advice you folks gave me and see if I can get that approach to sink in also!

Thanks again,

This is a great forum!

Stefan Garr

Oops! That, of course, is the cleanest way of all. In fact, that will even work on Windows. Of course, if Google changes their search string format (not likely) it may have problems. Also, you may want to substitute blank spaces with plus signs in your “song name” field. There are other characters that may cause problems, and I’m not sure if FileMaker makes those characters URL-safe before using them in the Open URL script step.

yeah, I forgot to mention about the blank spaces and +'s… I had to create a calc field that substituted a + for every space, then it was this calc field that was used in building the final calculated field/URL

Stefan