AppleScript.Compile
-------------------

Compiles the script and returns the Script ID.

| Component | Version | macOS | Windows | Linux | Server | iOS SDK |
|---|---|---|---|---|---|---|
| [AppleScript](component_AppleScript.md) | [1.0](newinversion10.md) | ✅ Yes | ❌ No | ❌ No | ✅ Yes, on macOS | ❌ No |

MBS( "AppleScript.Compile"; Script Text ) 

**MBS**( **"AppleScript.Compile";** /\* Compiles the script and returns the Script ID. \*/  
**$Script Text**) /\* This is the text of the script to compilee.g. "3 + 4" \*/ 

### 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](AppleScriptExecute.md) 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](AppleScriptLastErrorMessage.md)") \]   
 Exit Script \[ Text Result: \]   
End If  
  
\# now fill in values   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.SetPropertyValue](AppleScriptSetPropertyValue.md)"; $scriptID ; "MySubject"; AppleScript Properties::Subject ) \]   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.SetPropertyValue](AppleScriptSetPropertyValue.md)"; $scriptID ; "MyBody"; AppleScript Properties::Body ) \]   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.SetPropertyValue](AppleScriptSetPropertyValue.md)"; $scriptID ; "MyReceiver"; AppleScript Properties::Receiver ) \]   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.SetPropertyValue](AppleScriptSetPropertyValue.md)"; $scriptID ; "MySender"; AppleScript Properties::Sender ) \]   
  
\# check if it is there   
Show Custom Dialog \[ MBS ("[AppleScript.GetPropertyValue](AppleScriptGetPropertyValue.md)"; $scriptID ; "MySubject") \]   
  
\# run the script   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.Execute](AppleScriptExecute.md)"; $scriptID ) \]   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.Close](AppleScriptClose.md)"; $scriptID ) \]  
Compiler error:

 MBS ( "AppleScript.Compile"; "test++" )  
<small>  
Example result: \[MBS\] Failed to compile script with error -2741: Expected expression but found end of script. position: 7 length: 0</small>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](AppleScriptSetPropertyValue.md)"; $ScriptID ; "theID"; $theID ) \]   
Set Variable \[ $result ; Value: MBS ( "[AppleScript.Execute](AppleScriptExecute.md)"; $ScriptID ) \]   
Show Custom Dialog \[ "Result from AppleScript" ; $result \]   
\# free memory   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.Close](AppleScriptClose.md)"; $ScriptID ) \]  
Sets the note field for a contact via [AppleScript](component_AppleScript.md) 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](AppleScriptLastError.md)" ) &amp; ¶ &amp; MBS ( "[AppleScript.LastErrorMessage](AppleScriptLastErrorMessage.md)" ) \]   
\# run a query or multiple   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.SetPropertyValue](AppleScriptSetPropertyValue.md)"; $ScriptID ; "theID"; $theID ) \]   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.SetPropertyValue](AppleScriptSetPropertyValue.md)"; $ScriptID ; "theNote"; $theNote ) \]   
Set Variable \[ $result ; Value: MBS ( "[AppleScript.Execute](AppleScriptExecute.md)"; $ScriptID ) \]   
Show Custom Dialog \[ "Result from AppleScript" ; $result &amp; ¶ &amp; MBS ( "[AppleScript.LastError](AppleScriptLastError.md)" ) &amp; ¶ &amp; MBS ( "[AppleScript.LastErrorMessage](AppleScriptLastErrorMessage.md)" ) \]   
\# free memory   
Set Variable \[ $r ; Value: MBS ( "[AppleScript.Close](AppleScriptClose.md)"; $ScriptID ) \]  
### See also

- [AppleScript.Execute](AppleScriptExecute.md)
- [AppleScript.GetPropertyName](AppleScriptGetPropertyName.md)
- [AppleScript.GetPropertyValue](AppleScriptGetPropertyValue.md)
- [AppleScript.LastError](AppleScriptLastError.md)
- [AppleScript.PropertyCount](AppleScriptPropertyCount.md)
- [AppleScript.Run](AppleScriptRun.md)
- [AppleScript.SetPropertyValue](AppleScriptSetPropertyValue.md)
- [CNContact.SetValue](CNContactSetValue.md)
- [CNContact.Value](CNContactValue.md)
- [IsError](IsError.md)

### Release notes

- **Version 14.0**
    - Fixed [AppleScript.Compile](https://www.mbsplugins.eu/AppleScriptCompile.shtml) to clear last error for [AppleScript.LastErrorMessage](https://www.mbsplugins.eu/AppleScriptLastErrorMessage.shtml) function.
- **Version 13.2**
    - Improved [AppleScript.Compile](https://www.mbsplugins.eu/AppleScriptCompile.shtml) function to return error code and position.

### Blog Entries

- [MBS FileMaker Plugin, version 13.6pr1](https://www.mbsplugins.de/archive/2023-12-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Read and write notes in Contacts](https://www.mbsplugins.de/archive/2023-11-19/Read_and_write_notes_in_Contac/monkeybreadsoftware_blog_filemaker)
- [MBS FileMaker Plugin, version 13.2pr1](https://www.mbsplugins.de/archive/2023-04-01/MBS_FileMaker_Plugin_version_1/monkeybreadsoftware_blog_filemaker)
- [Send text message from FileMaker via iMessage](https://www.mbsplugins.de/archive/2019-03-10/Send_text_message_from_FileMak/monkeybreadsoftware_blog_filemaker)

This function checks for a license.

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

  
[AppleScript.Close](AppleScriptClose.md) - [AppleScript.DeterminePermissionToAutomateTarget](AppleScriptDeterminePermissionToAutomateTarget.md)

[HTML Version](AppleScriptCompile.shtml)