Convert FileMaker Value Lists to AppleScript Expression

Posted by simon 01/30/2009 at 11:15PM

Passing values between FileMaker and AppleScript can be a pain. Sure, you can use a named field and table in FileMaker to do this, but if you (or someone else) ever changes either of the names your AppleScript routine breaks. So, even though its not the most efficient way to do things, for short routines I often prefer to use FIleMaker’s ability to run a "Calculated AppleScript" (i.e., compile and run a script from a calculated text).

Other than efficiency, one problem with this approach is that you need to convert data into an AppleScript expression. This custom function makes it a simple task to pass a values list from FileMaker to an AppleScript subroutine:

// ValuesToApplescriptList
//
// Function Parameters:
//    fmpList: the return delimited list to convert (terminated by EOL -- watch for trailing carriage return!)

// If the input is null we don't want to create a list with a string and null as it's item.
// just create an empty list instead.

If (fmpList ≠ "";

   // Setup leading brace & qoutes.
   "{\"" &

   // Escape any qoutes in the input, and convert carriage returns into delimiters for applescript strings.
   Substitute (fmpList; ["\""; "\\\""]; ["¶"; "\",\""]) &

   // Terminate the last item in list and close the list expression.
   "\"}";

// else

   "{}"

)


Here’s an excerpt from FileMaker script that shows how you might use this function:

select file (list) 

# Get the list of files to display to user.
Set Variable [ $fileList; Value:Get (ScriptParameter) ] 

# Use applescript to throw up a dialog with list of files. 
Perform AppleScript [ Calculated AppleScript: "choose from list " & ValuesToApplescriptList ($fileList) & "with title \"Select File\"" ] 

 

If I pass the select file script the list of values "file ABC¶file XYZ¶lmnop" here’s the dialog I get from AppleScript:

select file dialog

 Now you may be asking "…but how do I easily get the value of the user’s selection from the AppleScript dialog?". For the answer to that you’ll have to wait for a later post. 

 

Comments

Leave a comment

  1. Eamon Byrne about 2 years later:

    I am trying to pull text, date and time field values into applescript and once I have got them to insert them into iCal, so if I could just get the value for example of field date1 I could then use it. Any thoughts

    Eamon

  2. Donovan about 2 years later:

    This question is more suited for a forum. Nonetheless, I recommend you start with this tutorial on scripting FileMaker.

  3. Simon Brown about 2 years later:

    @Eamon - One way is to use a Perform Applescript step using a calculated script. For example:

    Perform AppleScript [ Calculated AppleScript:

    "tell application \"iCal\"" & ¶ &
    "   try" & ¶ &
    "   show (first event of calendar \"Work Items\" whose uid = \"" & $iCal_UID & "\")" & ¶ &
    "   on error" & ¶ &
    "   display alert \"Couldn' t open event\" as critical message \"The event couldn't be opened. Either the event no longer exists or the Work Items calendar is missing.\" default button \"OK\"" & ¶ &
    "   end try" & ¶ &
    "   activate" & ¶ &
    "end tell" ]
    

    Or, less cryptically using a field containing the Applescript and the bBox_Applescript function:

    script__gt =

    on open_work_item (icalUID)
        tell application "iCal"
            try
                show (first event of calendar "Work Items" whose uid = icalUID)
            on error
                display alert "Couldn't open event" as critical message "The event couldn't be opened. Either the event no longer exists or the Work Items calendar is missing." default button "OK"
            end try
            activate
        end tell
    end
    

    which is executed with the following function call:

    bBox_Applescript (
       0;
       DEMO::script__gt; // Script text
       "open_work_item";  // handler name
       DEMO::ical_uid__gt;  // first handler parameter
    )
    
Comments