Jump to content

A tip for troubleshooting AppleScripts


This topic is 2904 days old. Please don't post here. Open a new topic instead.

Recommended Posts

One thing I really like about FMP is that you can specify a "calculation" for a perform AppleScript script step. However, this does leave something to be desired. Before each quote in the AS, you have to do a backslash, and at the end of each line, the paragraph symbol.

A problem I've had is that it's difficult to debug AppleScripts entered in FMP scripts. If the script is "static" -- i.e., you don't construct the script using various FM fields, etc. -- it's easy enough, because you can create the script in AppleScript editor, debug, and then copy and paste into FMP and enter the appropriate backslashes, etc.

However, this doesn't work well if the script is "dynamic," where you're creating the script in a calculation field based on various FM fields. I have found a pretty good solution, however.

First, create the script as a variable. Call it $script, for instance. So, the calculation field for the perform AppleScript would simply say $script.

Now, insert a halt script command just prior to the perform AS command in your FM script And prior to that command, create a text file where the contents of the file are $script. I use the Troi File plug-in, but there may be another way to do that.

At a minimum, with the Troi plug-in, you can open up the resulting text file in AppleScript editor, and execute it there. This is very beneficial, because the error handling is much better when executing an AS via the AS editor as opposed to directly through FM.

Let me know if I'm explaining myself clearly, and/or if you have any questions. I will say that this saved me at least an hour of debug time today -- it's a worthwhile thing to do for complicated AS execution through FM.

Link to comment
Share on other sites

Provided you have FM Advanced – which for serious developing is a must anyway – then you can step through the script (or set a breakpoint) and retrieve the variable's calculated AppleScript from the Data Viewer to test it in the (Apple)Script Editor.

 

However, this does leave something to be desired. Before each quote in the AS, you have to do a backslash, and at the end of each line, the paragraph symbol.

 

An alternative is to use List() and Quote(), as in:

List (
"tell application " & Quote ( "MyApplication" ) ;
  "set my var to (some expression)" ;
"end"
)
Link to comment
Share on other sites

However, this doesn't work well if the script is "dynamic," where you're creating the script in a calculation field based on various FM fields. I have found a pretty good solution, however.

 

A more typical approach is store the AS syntax in a preferences table in a text field and put placeholders in it where the FM content needs to go, something that can be easily targeted through a Substitute() call (like <<my_field_goes_here>>)

 

Then in your script, you retrieve the AS template, set it in a the variable, do the substitute.

As eos mentioned you don't need to output to a text file, you can just copy the value from the Data Viewer and plop it in the AS Script Editor.

 

This approach lets you make small tweaks to the AS template without having to find it in all the scripts where you use it.

AND since you store the template as text, there is no need whatsoever to escape all the quotes.

Link to comment
Share on other sites

This topic is 2904 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.