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

AppleScript.Compile

Compiles the script and returns the Script ID.

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

Parameters

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

Result

Returns ScriptID or error.

Description

Compiles the script and returns the Script ID.
You can use the Script ID to run the script using the AppleScript.Execute function. The compiled script is stored in memory. This is useful if you have large script that needs to be run over and over again.

Examples

Compile a script

MBS( "Applescript.Compile" ; $Applescript )

Send email via AppleScript and properties:

Set Variable [ $script ; Value: "property MySubject : \"My Subject\"
property MyBody : \"My Body\"
property MyReceiver : \"test@test.test\"
property MySender : \"test@test.test\"

tell application \"Mail\"
        activate
        set NewMail to make new outgoing message with properties {visible:true, subject:MySubject, content:MyBody}
       
        tell NewMail
                make new to recipient at beginning of to recipients with properties {address:MyReceiver}
        end tell
       
        set the sender of NewMail to MySender
end tell" ]

# compile it
Set Variable [ $scriptID ; Value: MBS( "Applescript.Compile"; $script ]
If [ MBS("isError") ]
    Show Custom Dialog [ MBS("AppleScript.LastErrorMessage") ]
    Exit Script [ Text Result: ]
End If

# now fill in values
Set Variable [ $r ; Value: MBS( "AppleScript.SetPropertyValue"; $scriptID; "MySubject"; AppleScript Properties::Subject) ]
Set Variable [ $r ; Value: MBS( "AppleScript.SetPropertyValue"; $scriptID; "MyBody"; AppleScript Properties::Body) ]
Set Variable [ $r ; Value: MBS( "AppleScript.SetPropertyValue"; $scriptID; "MyReceiver"; AppleScript Properties::Receiver) ]
Set Variable [ $r ; Value: MBS( "AppleScript.SetPropertyValue"; $scriptID; "MySender"; AppleScript Properties::Sender) ]

# check if it is there
Show Custom Dialog [ MBS("AppleScript.GetPropertyValue"; $scriptID; "MySubject") ]

# run the script
Set Variable [ $r ; Value: MBS( "AppleScript.Execute"; $scriptID) ]
Set Variable [ $r ; Value: MBS( "AppleScript.Close"; $scriptID) ]

Compiler error:

MBS( "AppleScript.Compile"; "test++" )

Example result: [MBS] Failed to compile script with error -2741: Expected expression but found end of script. position: 7 length: 0

Query contacts app for Note for a contact:

# Read Contact Note via AppleScript
Set Variable [ $theID ; Value: "773E1A42-ABCD-4CCF-88DE-E9C64BEE6D33:ABPerson" ]
# Compile AppleScript
Set Variable [ $ScriptID ; Value: MBS( "AppleScript.Compile"; "property theID : \"\"¶ tell application \"Contacts\"¶ set theContact to the first person whose id is theID¶ return note of theContact¶ end tell" ) ]
# run a query or multiple
Set Variable [ $r ; Value: MBS( "AppleScript.SetPropertyValue"; $ScriptID; "theID"; $theID ) ]
Set Variable [ $result ; Value: MBS( "AppleScript.Execute"; $ScriptID) ]
Show Custom Dialog [ "Result from AppleScript" ; $result ]
# free memory
Set Variable [ $r ; Value: MBS( "AppleScript.Close"; $ScriptID) ]

Sets the note field for a contact via AppleScript functions:

# Set Contact Note via AppleScript
Set Variable [ $theID ; Value: "773E1A42-ABCD-4CCF-88DE-E9C64BEE6D33:ABPerson" ]
Set Variable [ $theNote ; Value: "Best Hotdogs in town!" ]
# Compile AppleScript
Set Variable [ $ScriptID ; Value: MBS( "AppleScript.Compile"; "property theID : \"\"¶ property theNote : \"\"¶ tell application \"Contacts\"¶ set theContact to the first person whose id is theID¶ set note of theContact to theNote¶ save¶ return note of theContact¶ end tell" ) ]
Show Custom Dialog [ "Result from AppleScript" ; MBS( "AppleScript.LastError" ) & ¶ & MBS( "AppleScript.LastErrorMessage" ) ]
# run a query or multiple
Set Variable [ $r ; Value: MBS( "AppleScript.SetPropertyValue"; $ScriptID; "theID"; $theID ) ]
Set Variable [ $r ; Value: MBS( "AppleScript.SetPropertyValue"; $ScriptID; "theNote"; $theNote ) ]
Set Variable [ $result ; Value: MBS( "AppleScript.Execute"; $ScriptID) ]
Show Custom Dialog [ "Result from AppleScript" ; $result & ¶ & MBS( "AppleScript.LastError" ) & ¶ & MBS( "AppleScript.LastErrorMessage" ) ]
# free memory
Set Variable [ $r ; Value: MBS( "AppleScript.Close"; $ScriptID) ]

See also

Release notes

Blog Entries

This function checks for a license.

Created 18th August 2014, last changed 17th November 2023


AppleScript.Close - AppleScript.DeterminePermissionToAutomateTarget