« Monkeybread Software … | Home | FileMaker Developer C… »

Tip of the day: AppleScript with Properties to pass file path

Today we discussed on how to best get the file path into an AppleScript. We'd like to pass it as POSIX path and no the old Mac path format. And we prefer to use properties to pass values into the script. The advantage is that we can compile once and run several times with different values. Finally putting values in properties avoids any escaping problems when putting a text into a script directly. So here is the final code:

// we have some FolderItem dim f as FolderItem = SpecialFolder.Desktop.Child("test.key") // and build a script dim lines() as string // here we declare a property for the script: lines.Append "property filePath : """"" // and convert path from posix file path to AppleScript path lines.Append "tell application ""Finder""" lines.Append " set f to filePath as POSIX file" lines.Append "end tell" // and use path for Keynote lines.Append "tell application ""Keynote""" lines.Append " activate" lines.Append " set mydoc to open file f" lines.Append "end tell" // compile the script dim s as String = Join(lines, EndOfLine.Macintosh) dim a as new AppleScriptMBS a.Compile s if a.Lasterror <> 0 then MsgBox str(a.Lasterror)+" "+a.Error.BriefMessage end if // fill the property a.ScriptPropertyValue("filePath") = f.NativePath // run it a.Execute if a.Lasterror <> 0 then MsgBox str(a.Lasterror)+" "+a.Error.BriefMessage end if
21 07 16 - 23:57