0 Replies Latest reply on Apr 3, 2014 11:03 PM by tunesmith

    Script / Applescript cannot always find file

    tunesmith

      Title

      Script / Applescript cannot always find file

      Post

           Hi, apologies in advance for the complexity of this question, but I am finding it is repeatable behavior.

           The use case is that I have a container with an audio file, and I want to be able to play the audio while seeing its minutes and seconds tick by, like in quicklook, or like how Bento does it.  Filemaker's audio playing behavior doesn't have a timer.  My solution is to open the contents of the container in a separate simple media application that will play the audio (and display the elapsed time) while I take notes in my Filemaker record.  I've hooked this script up to a button.

           So here's the problem as succinctly as I can explain it.  In a "Recording" layout that has a container, I have a button that has this script:

           
      Set Variable [$path; Value:Get ( TemporaryPath )]
      Set Variable [$file; Value:GetAsText (Recordings::Song File)]
      Set Variable [$filename; Value:Middle ( $file ; Position ( $file ; “/“ ; 1 ; 
          PatternCount ( $file ; “/“ ) ) + 1 ; 1000 )]
      Set Variable [$fullpath; Value:$path & $filename]
      Export Field Contents [Recordings::Song File; “$fullpath”]
      Set Variable [$script; Value:Let (
          thepath = Middle ( $fullpath  ; Position ( $fullpath ; "/" ; 1 ; 2 ) ; Length ( $fullpath) ) ;
      
      "
      set appPath to path to application \"MPlayerX\"  ¶
      set p to POSIX file " & Quote(thepath) & "¶
      tell application \"Finder\" to open file p using appPath"
      )]
      Show Custom Dialog [$script]
      Perform AppleScript [$script]

           The takeaway is that it then generates and invokes some applescript that looks, for example, like this:

           

      setappPathtopath toapplication "MPlayerX"

           

      setptoPOSIX file "/private/var/folders/v1/f5_s4swx4bsf59wwwdv45g4w0000gn/T/S11/1-02 Blame It On My Youth.mp3"

           

      tell application "Finder" to open file p using appPath

           If I run the entire filemaker script, I get these dialogue box errors: "Finder got an error: AppleEvent handler failed.", and "Unknown Error: -10000".  And, if I paste that generated applescript (above) into AppleScript Editor, and run it, I get the same error:

           

      error "Finder got an error: AppleEvent handler failed." number -10000 fromfile (file "Macintosh HD:private:var:folders:v1:f5_s4swx4bsf59wwwdv45g4w0000gn:T:S11:1-02 Blame It On My Youth.mp3”)

           This is despite the fact that the file does exist at that path on disk (I can verify this through the terminal.)  Okay, here is where it gets tricky.  Keep in mind I have already pasted that script into AppleScript Editor.

           I then edit the button to point to a slightly different broken script I have.  You can pretty much ignore this since it is broken:

           
      Set Variable [$path; Value:Get ( TemporaryPath )]
      Set Variable [$file; Value:GetField (GetValue (Get( ScriptParameter) ); 1) )]
      Set Variable [$filename; Value:Middle ( GetAsText($file) ; Position ( GetAsText($file) ; “/“ ; 1 ; PatternCount ( GetAsText($file) ; “/“ ) ) + 1 ; 1000 )]
      Set Variable [$fullpath; Value:$path & $filename]
      Set Variable [$script; Value:Let (
          thepath = Middle ( $fullpath  ; Position ( $fullpath ; "/" ; 1 ; 2 ) ; Length ( $fullpath) ) ;
      
      "
      set appPath to path to application \"MPlayerX\"  ¶
      set p to POSIX file " & Quote(thepath) & "¶
      tell application \"Finder\" to open file p using appPath"
      )]
      Export Field Contents [Recordings 2::Song File; "$fullpath"]
      Perform AppleScript [$script]

           The point is that it points to an incorrect related record that does not exist (Recordings 2 instead of Recordings, which this layout cannot access).  Pressing the button and invoking the script yields the expected following three dialog boxes:  

           
                  
      1.                "" could not be created on this disk.  Use a different name, make more room on the disk, unlock it or use a different disk.
      2.           
      3.                Finder got an error: Can't make file "Macintosh HD:private:var:folders:v1:f5_s4swx4bsf59wwwdv45g4w0000gn:T:S11:" into type file.
      4.           
      5.                Unknown Error: -1700.
      6.      

           Then, I edit the button again to point back to the original, first script I described above.  I click the button, and... it works.  It launches my mp3 in my mp3 player and plays it.  Not only that, but if I go run the script in my AppleScript Editor (which I have not changed since I first ran it), it works there too.

           Then, if I quit filemaker and restart it, and click the button... it continues to work.  Even though that deletes and recreates the mp3 file on disk, in that S11 directory.

           However, if I quit filemaker and wait a long time - like a few hours - then come back and restart it, and click the button... it no longer works.  Until I switch to the other broken script, try to run it, and then switch back... and it works again.

           It is as if Applescript can't always see a file on disk unless something else happens too?  And somehow that broken script (or the act of switching away from the first script and then switching back) make that other thing happen, so that applescript can finally see the mp3?  Does anyone have any ideas on this?  I know it's kind of weird, but like I said, this is completely repeatable - the only thing I haven't figured out is how long to wait until it reverts to the "broken" behavior again.