6 Replies Latest reply on Apr 20, 2010 10:47 AM by FentonJones

    applescript script step causing issues when "delay" is used



      applescript script step causing issues when "delay" is used


      I'm using FMP Advanced 10, and am on 10.6.1. I'm an intermediate FM user.


      I now have an applescript which is part of a larger script which is meant to do the following. I need to insert an .mp3 file into a container. So I created a field which has the POSIX path to the file. The script copies the contents of that field. Then it goes to the container field on the layout. Then it executes an applescirpt which is meant to insert the .mp3. The script is:



      tell application "System Events"

      tell process "FileMaker Pro Advanced"

      click menu item "QuickTime…" of menu 1 of menu bar item "Insert" of menu bar 1

      tell window 1

      keystroke "g" using {command down, shift down} //this should open Go To Folder

      keystroke "v" using {command down} //this pastes the path, which has been checked and is POSIX correct. We're getting here OK.

      end tell

      tell window 1

      key code 36 //this clicks Enter. By the time it gets here, it just beeps twice and does nothing further, indicating it can't click Enter, Enter

      end tell

      tell window 1

      key code 36 //this clicks Enter

      end tell

      end tell

      end tell


      This works in applescript itself perfectly, but when run in FMP, it keeps beeping and not performing.

      I diagnosed this to one error: delay. Anytime I put a delay of any kind in the script, it never opens the Insert QuickTime... window, and so balks at everything after. But if there are no delays, it runs smoothly to the point after it has pasted the path into the Go To Folder sheet, unfortunately, so fast, that by the time it gets there, all it can do is beep twice (I can't click Enter!).

      Can someone tell me what's going on here?


        • 1. Re: applescript script step causing issues when "delay" is used

          I can't really tell you what is going wrong. I have done quite a bit with FileMaker and AppleScript, including running Unix shell scripts. But I have very seldom had to use GUI scripting (except some messing about with changing fonts). I just don't think you need to. All you really need to do is give the Insert Quicktime a FileMaker syntax path, no AppleScript needed for that.


          Set Variable [ $mp3; "moviemac:/Macintosh HD/Users/fej/Music/iTunes/iTunes Music/Alison Krauss/Forget About It/Maybe.mp3" ]

          Go To Field [ mp3 container ]

          Insert Quicktime [ $mp3 ]


          So you'd just need to take your POSIX path, add the "moviemac:" prefix, and the drive name. Which you can get via the Get (SystemDrive) function. It returns it in the syntax: /Macintosh HD/

          So you'd need to remove the trailing slash, since your Unix path likely starts with a slash. 

          • 2. Re: applescript script step causing issues when "delay" is used

            No, it works perfectly as is.


            Thanks very much... of course this was the right way to do it. :) 

            • 3. Re: applescript script step causing issues when "delay" is used

              FMP 11, iMac, OS 10.6.3


              I have imported my iTunes files into a FMP programme, including the file path or location which looks like this:




              I can play the song directly using an 'open URL' connection, but that moves the control to iTunes, and I want to play a (list of) song(s) within FMP and controlled by FMP.

              So I am trying to reformat the file path to suit QuickTime

              After reformatting the file path I can use the method proposed in your message, but I don't seem to get the right format; my latest effort was:


              "file:/Iomega HDD/albumdisk/ALBUMS/Music/Abba/The Definitive Collection - Abba/1-01 People Need Love.mp3"


              Do you see what I am doing wrong?

              • 4. Re: applescript script step causing issues when "delay" is used

                One thing that is wrong is that you have an "absolute" file path (beginning from the root of the hard drive), but a "relative" prefix. Try using "filemac:" instead of "file:".


                There are various ways to play a sound file. Yes, on most Macs, just opening the file is going to launch iTunes and play it, in a big window; because iTunes is the default application for such things, unless you change it. Which is a system-wide change, applying to all music. You cannot easily do this from FileMaker, nor really should you.


                If you use AppleScript, you have more options and more control. It is possible to tell iTunes to play it in a minimized window, or even hide the window.


                Also, as you say, you can specifically use Quicktime instead, via AppleScript. Quicktime also opens it in a little window, but it's a cute little window, with pause icon, etc.. 


                Quicktime supports both a URL path, which you can target via an Open URL path (just like FileMaker). Or you can use AppleScript's regular open with a Mac path (with colons). Since you have a URL path (Location_URL is my FileMaker field):


                set url_path to cell "Location_URL" of current record


                tell application "QuickTime Player"


                       open URL url_path

                       play document 1

                end tell



                "activate" brings the little QT window to the front, so you know it's there. Alternatively you could not do that, or even make it non-visible. But then you'd need little AppleScript buttons to Pause, Stop, Close, Quit, whichever.



                If you want total background operation, with no icon in the Dock, etc., there is, on newer Macs, a built-in audio player via command line: afplay


                You can run this from FileMaker, via AppleScript's do shell script command.

                It requires a proper Unix POSIX path (which is similar to, but not the same as a URL path).


                do shell script "afplay '/System/Library/Sounds/Hero.aiff' &> /dev/null &"


                The redirect to dev/null is needed, or it ties up FileMaker while it's playing; since FileMaker started it by running the AppleScript.


                Once it starts a music file, it is going to continue until it's finished, or you stop it. You can do this by killing the afplay process.


                do shell script "killall afplay"




                • 5. Re: applescript script step causing issues when "delay" is used

                  Thank you for your reaction and excellent suggestions.:smileyhappy:


                  My original question is no longer relevant, as the problem is solved in the QT approach.


                  I couldn't find any track of "afplay", although I bought this iMac only six weeks ago.


                  I settled for the Quick Time player and it works perfectly.

                  I must confess that I never took time to learn AppleScript, but it seems a good investment to learn it, as it offers much flexibility and possibilities for controlling what is going on.

                  • 6. Re: applescript script step causing issues when "delay" is used

                    Yes, the little QT window looks and works nicely. 


                    RE: afplay

                    It is a Mac command line tool. It is in the /usr/bin/ folder. But since the /usr folder is not visible in the regular hard drive Finder window you'll not see it. You can use the Finder menu, Go to folder, /usr

                    There are tons of command line tools in the /bin/ and in /sbin/ folders, which do all kinds of things. Be careful not to move them.


                    AppleScript can run them via: do shell script "command line"

                    FileMaker can run AppleScript, via Perform AppleScript


                    This extends the power of FileMaker, when the situation calls for it; either internet access (curl, ftp), file manipulation, text processing (awk, grep), etc.. 'afplay' is a later addition, to the basic Unix commands (and ones later added by Apple).