9 Replies Latest reply on Aug 10, 2017 8:45 AM by iaml

    Return a value from Applescript into FMP

    mattb

      Title

      Return a value from Applescript into FMP

      Post

      I'm trying to do something that seems rather simple and that I've done successfully on other platforms but I can't figure out how to do this in FMP.

      I've got this 3-line Applescript:

      ---------------------------------

      tellapplication "Finder"

      get capacity of startup disk

      endtell

      ---------------------------------

      This script returns a number (the size of the hard disk in bytes).  I would like this number returned to FMP so that I can store it in a field.  Simple?  Problem is I have no idea how to accomplish this.  I played around with the Perform Applescript script step but there's seemingly no ability to capture the Applescript's result, which is what I'm after here.  I played around with the Send Events script step, which seems to make reference to capturing results to the Clipboard, but am not understanding how this could achieve my desired result.

      When I used the OS9 version of Visual Foxpro, I used the "Runscript <Applescript> to <variable>" syntax which was very straightforward and gave me programmatic control of the Applescript's result.  There's got to be a way to do the same with FMP, but I'm lost.  If anybody can steer me in the right direction, or just outright tell me how to do this, I would really really appreciate it.  Thanks!

      Matt Bloomfield

        • 1. Re: Return a value from Applescript into FMP

          mattb:

          Thank you for posting.

          Here is one simple possibility that I've tested using OS X 10.6.4 and FileMaker Pro Advanced 11:

          tell application "Finder"
              set cap to get capacity of startup disk
          end tell

          tell application "FileMaker Pro Advanced"
              set cell "Field1" of current record to cap
          end tell

          Will this suffice or were you looking for another solution?

          TSuki
          FileMaker, Inc.

          • 2. Re: Return a value from Applescript into FMP
            FentonJones

            Be aware that the FileMaker field must be on the current layout when using this simple syntax for the target. It is possible to set a field which is not on the current layout if you also specify the layout.

            set cell "Field1" of current record of layout "some layout"

            But since "some layout" would be hard-coded into the AppleScript (hence breakable if you later change the layout name), I also prefer the simple syntax. If it matters, you can create a dedicated layout for AppleScript which has all the fields you needed, flip there and flip back, or has a known name (which you never change).

            P.S. FileMaker has some fairly good documentation about FileMaker and AppleScript, which they used to include with the application, but don't anymore (for some misguided reason, IMHO). If you work with AppleScript you need it.

            http://fmdl.filemaker.com/MISC/fmp10/fp/apple_events_reference_wwe.zip

            • 3. Re: Return a value from Applescript into FMP
              mattb

              TSuki:

              Yes, that did the trick - thank you!  I was not aware of the "set cell" Applescript command.  This is ultimately going into a compiled application, so will I need to change the first line of the Applescript to "Tell application <myapp name>" instead of "Tell application Filemaker Pro Advanced"?

              Fenton:

              Yes, I found scant documentation regarding Applescript implementation in the FMP 11 docs.  I even went to the FMP website and did a search on Applescript which turned up many links not this specific doc - so thanks for that...

              Matt Bloomfield

              • 4. Re: Return a value from Applescript into FMP

                mattb:

                Correct, when you create a runtime you can specify a name for it. You would use this name instead of "FileMaker Pro Advanced" in the AppleScript.

                TSuki
                FileMaker, Inc.

                • 5. Re: Return a value from Applescript into FMP
                  HOnza

                  Even though this post is 7 years old, it has got a new dimension with the release of FileMaker 16, because the suggested technique requires the fmextscriptaccess extended privilege to work, which on the other side may be unwanted security hole to your solution.

                   

                  I have, however, found a simple trick how to return numerical result without this extended privilege. Just return it as an error code:

                   

                  error number myResult

                   

                  Then use Set Error Capture [On] before the Perform AppleScript script step, and Get(LastError) to retrieve the result.

                  4 of 4 people found this helpful
                  • 6. Re: Return a value from Applescript into FMP
                    iaml

                    The error number trick was neat, except that Set Error Capture [On] does not suppress the error dialog box if the number returned is not a known error code. For example, I am using AppleScript to get the width of my total desktop on my dual 1920 x 1200 setup. When I return the result as an error, I get "Unknown Error: 3840." Once you OK that dialog, it proceeds correctly, but that's certainly not the desired effect. The function documentation does seem to suggest that this is the expected behaviour. So that's useful for low integer values but the other techniques seem to be necessary for larger values.

                    • 7. Re: Return a value from Applescript into FMP
                      beverly

                      Any plug-ins? I think I saw this error with plug-ins installed. Even though you are not using the plug-in for AS (or are you?)

                      Beverly

                      • 8. Re: Return a value from Applescript into FMP
                        iaml

                        Nope. No plug-ins. Running Advanced 16.

                        • 9. Re: Return a value from Applescript into FMP
                          iaml

                          Just as a follow-up to document things for future readers of this thread:

                          • As HOnza noted, the techniques described earlier in the thread require that fmextscriptaccess be turned on for the file in question when using FileMaker 16 (File > Manage > Security, grant extended privileges to selected users).
                          • As I found out (and confirmed through implementation) in another thread, you may omit the 'tell Application "FileMaker Advanced"' tell block and just use the commands directly, since you are operating in the FileMaker context if your AppleScript is being run within FileMaker. This also avoids the problem where you don't know whether users will be running Pro or Advanced.

                           

                          So you can simplify TSuki's AppleScript even more:

                          Tell application "Finder"
                               set cap to get capacity of startup disk
                          end tell

                          set cell "Field1" of current record to cap
                          1 of 1 people found this helpful