AppleScript with FileMaker
AppleScript with FileMaker
- Subject: AppleScript with FileMaker
- From: David Crowe <email@hidden>
- Date: Fri, 22 Jun 2018 09:00:00 +0200
I have embedded a lot of AppleScripts inside FileMaker and eventually figured
out the best way to do this. There appear to be two methods, but the second
method has at least two sub-methods that are much more powerful than using it
in the most obvious way.
I. Method I. Static String.
Let’s examine the first method first, the default when you open up the “Perform
Applescript” script step, “Native AppleScript”. This allows you to insert a
text string to be executed, for example, the not very useful:
tell application "Finder"
activate
open (path to documents folder)
end tell
This is good for testing and playing around, but not advisable for anything
complicated. The main problem is that the string is static and cannot be
created with FileMaker tools (concatenation, calculations etc). If you wanted
to include something from the current FileMaker record you would have to script
this in AppleScript.
II. Method 2. Calculated String
If we want all the FileMaker calculation capabilities we have to use the second
method, “Calculated Applescript”. As I warned you there are three different
ways to use this, but let’s just take the most obvious one. Our script now
becomes:
"tell application \”Finder\”¶
activate¶
open (path to documents folder)¶
end tell”
Note the escaped quotes and ¶ at the end of every line (the actual linefeeds
are optional, but good for readability)
If we put the folder name in a FileMaker field named “Test::folder", we can
have something like:
"tell application \”Finder\”¶
activate¶
open (path to " & Test::folder & ")¶
end tell”
Now the sky’s the limit, but if our script gets long and complicated it is
really hard to test outside filemaker, and writing it is a pain, with all the
escaped quotes and the need for “¶” at the end of every line.
III. Method 3, Outsourced script.
So one approach is to outsource the majority of the script to an external file,
so FileMaker code like:
"property x : load script file ((path to home folder as string) &
\"test.scpt\")¶
tell x to OpenApplications(\"" & Test::folder & "\”)"
in conjunction with a script like:
on OpenApplications(afolder)
tell application "Finder"
activate
open folder ((path to home folder as string) & afolder)
end tell
end OpenApplications
So, if “Test::folder” contains “Music” it will open the Music folder in your
home directory. The script itself is not terribly useful.
The AppleScript within FileMaker rarely has to change, and the bulk of script
can be edited and debugged in the script editor of your choice. The script can
be run outside FileMaker which is nice when an AppleScript fails in the context
of a long FileMaker script.
The problems with this are that you end up with a large number of external
files that are now part of your FIleMaker application which otherwise would be
a single file. If you move them, all hell breaks loose. Furthermore, some
things are easier to do inside FileMaker (such as adapt your scripts to work
when running FileMaker Pro or FileMaker Pro Advanced) are difficult. If you
need to provide data from a lot of fields the Applescript code within FileMaker
is still very ugly.
IV. Method 4. Capture the actual AppleScript.
You cannot run the calculated string in an external editor for two reasons.
First of all, it contains escaped quote marks, strange line markers (¶) and
FileMaker calculations. So, if you had your entire script within a FileMaker
calculated string, how would you debug it?
Well, it’s actually quite simple (although it took me a long time to think of
it). Write a simple FileMaker script that takes an AppleScript as a parameter,
copies it to a FileMaker field and then executes it. If you then want to do
some debugging you just take the executed source from the FileMaker field and
copy it into a script editor. Once you debug it you have to edit changes back
into FileMaker watching for escaping quotes and line endings, but if the
changes are small it’s not so bad. If you completely rewrite the script
externally, then with an editor you need to globally change all straight double
quotes to \” and all line endings to ¶. Again, not bad once you get used to it.
Now all your applescripts are in your single FileMaker file, and you can even
do things like “tell application x” where “x” is a variable (FileMaker field).
I hope this little essay is useful, and perhaps other people have diverse
opinions.
Then there’s the ugly question of how to get data from your AppleScript back
into FileMaker, but that’s another story.
- David Crowe
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden