Components All New MacOS Windows Linux iOS
Examples Mac & Win Server Client Guides Statistic FMM Blog Deprecated Old

AppleScript.Run

The function compiles the AppleScript text and runs it.

Component Version macOS Windows Linux Server iOS SDK
AppleScript 1.0 ✅ Yes ❌ No ❌ No ✅ Yes, on macOS ❌ No
MBS( "AppleScript.Run"; Script Text )   More

Parameters

Parameter Description Example
Script Text This is the text of the script to run "3 + 4"

Result

Returns result of script.

Description

The function compiles the AppleScript text and runs it.
Any Result returned by the AppleScript is returned as the result of the calculation. This is similar to the Script Results that were introduced in FileMaker 8.0. This is much easier then using AppleScript.Compile, AppleScript.Execute and AppleScript.Close

Older plugin versions used to put quotes around result. 6.4 and newer don't include quotes.

This function does return "" in case AppleScript fails. If you need more error handling, please run AppleScript.Compile to get compile error for syntax errors.

Examples

Trigger a FileMaker Script Using OS Scripting

Let(
[
// --- the name of the script to run ------------------

ScriptName = "Triggered Script";
FileName = Get(FileName);

//------------------------------------------------------------

//--- don't need to edit anything below this line --------


Applescript = "do script " & Quote(ScriptName);

VBScriptText =
"FUNCTION DoFMSCript(FileName, ScriptName)" & "¶" &

"Set fmApp = CreateObject(\"FMPro.Application\")" & "¶" &
"fmApp.Visible = True" & "¶" &
"Set fmDocs = fmApp.Documents" & "¶" &
"For Each fmDoc In fmDocs" & "¶" &
"If InStr(LCase(fmDoc.fullname), LCase(FileName)) > 0 Then" & "¶" &
" fmDoc.dofmscript (ScriptName)" & "¶" &
"End If" & "¶" &
"Next" & "¶" &

"END FUNCTION";

ScriptID = MBS("WindowsScript.Create");
lang= MBS("WindowsScript.SetLanguage"; ScriptID; "VBScript");
addcode = MBS("WindowsScript.AddCode"; ScriptID; VBScriptText)

];
Case(
Get ( SystemPlatform ) = 1;

// Mac OSX Case
MBS(
"Applescript.Run";
Applescript
);

// Windows Case
MBS("WindowsScript.ExecuteFunction";ScriptID; "DoFMScript";FileName; ScriptName) &
MBS("WindowsScript.Close"; ScriptID)

)
)

Create A Folder

Let(
[
/*-----------------PARAMETERS--------------------*/
folderName = "My New Folder";

scriptText =
"set folder_name to " & Quote(folderName) & "¶" &
"tell application " & Quote("Finder") & "¶" &
" set f to make new folder at desktop" & "¶" &
" set the name of f to folder_name" & "¶" &
"end tell"

];
/*-------------------FUNCTION----------------------*/
MBS(
"Applescript.Run";
scriptText
)
)

/*------------------------------------------/
Creates a folder called "My New Folder on the users desktop
*/

Get Names From Address Book

Let(
[
/*-----------------PARAMETERS--------------------*/
scriptText =
"tell application \"Address Book\"" & "¶" &
" set the_list to \"\"" & "¶" &
" set person_list to the name of every person" & "¶" &
" repeat with this_name in person_list" & "¶" &
" set the_list to the_list & this_name & return" & "¶" &
" end repeat" & "¶" &
"end tell"

];
/*-------------------FUNCTION----------------------*/
MBS(
"Applescript.Run";
scriptText
)
)

/*------------------------------------------/
Creates a folder called "My New Folder on the users desktop
*/

Trigger A FileMaker Script (Custom Function)

MBS_TriggerScript ( "Triggered Script" ; Get ( FileName ) )


Custom Function Definition
/*###############################################

MBS_TriggerScript
created 10/26/06, by Todd Geist, todd@geistinteractive.com

Parameters: theScriptName, theFileName

Dependancies: MBS FileMaker Plug-in.

Notes: Uses VBScript and Applescript to run a script

################################################*/
Let(
[

Applescript = "do script " & Quote(theScriptName);

VBScriptText =
"FUNCTION DoFMSCript(FileName, ScriptName)" & "¶" &

"Set fmApp = CreateObject(\"FMPro.Application\")" & "¶" &
"fmApp.Visible = True" & "¶" &
"Set fmDocs = fmApp.Documents" & "¶" &
"For Each fmDoc In fmDocs" & "¶" &
"If InStr(LCase(fmDoc.fullname), LCase(FileName)) > 0 Then" & "¶" &
" fmDoc.dofmscript (ScriptName)" & "¶" &
"End If" & "¶" &
"Next" & "¶" &

"END FUNCTION";

ScriptID = MBS("WindowsScript.Create");
lang= MBS("WindowsScript.SetLanguage"; ScriptID; "VBScript");
addcode = MBS("WindowsScript.AddCode"; ScriptID; VBScriptText)

];
Case(
Get ( SystemPlatform ) = 1;

// Mac OSX Case
MBS(
"Applescript.Run";
Applescript
);

// Windows Case
MBS("WindowsScript.ExecuteFunction";ScriptID; "DoFMScript";theFileName; theScriptName) &
MBS("WindowsScript.Close"; ScriptID)

)

Open URL in VLC player:

Set Variable [ $url ; Value: "https://www.monkeybreadsoftware.com/filemaker/video/FMK2015.mp4" ]
Set Variable [ $r ; Value: MBS( "Applescript.Run"; "tell application \"VLC\" to GetURL \"" & $url & "\"") ]

Speak using AppleScript:

Set Variable [ $text ; Value: "Hello \"Test\"" ]
Set Variable [ $voice ; Value: "Daniel" ]
Set Variable [ $rate ; Value: 100 ]
Set Variable [ $pitch ; Value: 60 ]

Set Variable [ $text ; Value: Substitute($text; "\""; "\\\"") ]
Set Variable [ $r ; Value: MBS( "AppleScript.Run"; "say \"" & $text & "\" using \"" & $voice & "\" speaking rate " & $rate & " pitch " & $pitch ) ]

Show dialog:

Set Variable [ $r ; Value: MBS("AppleScript.Run"; "display dialog \"Did I start yet?\"") ]

Get sender of current email in Mail:

MBS( "AppleScript.Run";
"tell application \"Mail\"¶
    set theSelection to selection¶
    set theMessage to item 1 of theSelection¶
    return sender of theMessage as Unicode text¶
end tell")

Get content of current email in Mail:

MBS( "AppleScript.Run";
"tell application \"Mail\"¶
    set theSelection to selection¶
    set theMessage to item 1 of theSelection¶
    return content of theMessage as Unicode text¶
end tell")

See also

Blog Entries

This function checks for a license.

Created 18th August 2014, last changed 25th January 2024


AppleScript.PropertyCount - AppleScript.SetPropertyValue