3 Replies Latest reply on Jun 28, 2010 2:53 AM by apw

    AppleScript Editor and FMP

    apw

      Title

      AppleScript Editor and FMP

      Post

      I developed a script in the AppleScript Editor, which I included in FMP as a "Perform Applescript". It works fine in the Editor.

      =========================

      setsong_fileto "Macintosh HD:Users:my_name:desktop:afplay_playlist.tab"

       

      set the_Songs to paragraphs of (read file song_file)

       

      set volume output volume 20

       

      repeatwitha_Songinthe_Songs

       

      seta_Songtoa_Song's contents

      if (counta_Song's words) > 0 then

      do shell script ("afplay " & quoted formofPOSIX pathofa_Song)

      else

      exitrepeat

      endif

       

      endrepeat

      =================
      Then I encountered two problems.
      FMP doesn't accept the second line; it objects to "file" (expected ",", but found class name) and, after removing "file", to "song_file" (expected ",", but found identifier).
      I simply don't understand this. It accepts "(read, file song_file), but then the object can't be found.
      Furthermore, if I change the temporary file on the desktop to another location, the file is not found, although the path is correct and the file definitely present.
      Any ideas?

        • 1. Re: AppleScript Editor and FMP
          FentonJones

          The main problem you're up against is that FileMaker does not like certain "AppleScript" commands (they are actually not built in to AppleScript, but are part of the "Standard Additions" Script Addition, which everyone has). Two it does not like are "read" and "write".

           

          The workaround is to use any other application to run the command. AppleScript developers commonly use the built-in background application "System Events" (better than using "Finder" as it does not tie up the Finder).

           

          tellapplication "System Events" to set the_Songs to paragraphs of (read file song_file)


           

          Another thing. The `afplay` command line tool will tie up the FileMaker application during play, if you call it as is from a FileMaker Perform AppleScript. There is a common Unix method for this (by others). This is an example, which anyone can run in Script Editor:


          setaudio_filetochoose filewith prompt "Choose an Audio File"

          set unix_file to quoted form of POSIX path of audio_file

           

          do shell script "afplay -q 1 " & unix_file & " &> /dev/null &"

          -- high quality (default is low, only 2 choices, as far as I know)

          -- /dev/null releases the current app to continue

           

           

          There is also a -v option, for volume. So you would not need to mess with the user's regular volume. It uses a different scale than the system volume. When I put 20 it just about blew out my speakers. It's more like 1 = 100%. It seems to support decimals, so .1 is "low volume". 

           

          do shell script "afplay -q 1 -v .1 " & unix_file & " &> /dev/null &"

           

          `afplay` has the most minimal manual you can imagine. To even see the options you have to run the -h (help) option, in Terminal. Here they are:

           

          afplay [option...] audio_file

           

          Options: (may appear before or after arguments)

            {-v | --volume} VOLUME

              set the volume for playback of the file

            {-h | --help}

              print help

            { --leaks}

              run leaks analysis  {-t | --time} TIME

              play for TIME seconds

            {-r | --rate} RATE

              play at playback rate

            {-q | --rQuality} QUALITY

              set the quality used for rate-scaled playback (default is 0 - low quality, 1 - high quality)

            {-d | --debug}

              debug print output

           

          • 2. Re: AppleScript Editor and FMP
            FentonJones

            If you want to stop play via afplay, you need to quit it. You can attach this to a button (Perform AppleScript, no script required).

             

            do shell script "killall afplay"

            -- stop playing

             

             

            P.S. If you have an OS < 10.5, there is an older command line tool, which you can download and install.

             

            http://www.hieper.nl/html/play.html

             

            It is similar to afplay in usage. It does not have quality or volume. It does have a "for time" option, so you can specify how long to play a song.

             

            There are also other applications, such as QuickTime Player, which has a nice little window with controls.

            • 3. Re: AppleScript Editor and FMP
              apw

              This is insider's information:smileyhappy: Yes, the detour over "System Events" solves the problem.

              I've learned several computer languages in my life by studying the manuals, but AppleScript amazes me every time; its manual does not cover 1% of its possibilities.

               

              Concerning the '/dev/null' stuff, I haven't forgotten your earlier lessons.

              This script is an alternative for a QTP script and is meant to create a list of songs (in Fmp) and subsequently to play them thru afplay outside the control of FMP.

              I even tell in this script, not shown, to quit FMP.

              Whereas the QTP scripts, I developed two alternatives, provide all kind of information and full control, this script plays music in the background; I will use your button to stop it.

               

              Thanks again.