Platforms to show: All Mac Windows Linux Cross-Platform

Back to AppleScriptMBS class.

AppleScriptMBS.Binary as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
property Apple Script MBS MacClassic Plugin 3.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The binary representation of the current compiled script.
Example
// create our own compiled AppleScript file

dim a as AppleScriptMBS
dim b as BinaryStream
dim f as FolderItem
dim r as ResourceFork

a=new AppleScriptMBS

// compile a simply applescript:
a.Compile "beep"

// Mac OS X format
f=GetFolderItem("My Apple Script X")

b=f.CreateBinaryFile("applescript")
b.Write a.Binary
b.Close

// Mac OS 9 format
f=GetFolderItem("My Apple Script 9")

r=f.CreateResourceFork("applescript")
r.AddResource a.Binary,"scpt",128,""
r.Close

You can save and load this value to store scripts.
Lasterror is set.
(Read and Write computed property)

AppleScriptMBS.close

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.4 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The destructor.
Example
dim a as new AppleScriptMBS
// later
a.close

There is no need to call this method except you want to free all resources of this object now without waiting for Xojo to do it for you.

AppleScriptMBS.Compile(text as string)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Compiles the given source code.
Example
// the code below workarounds a limitation in AppleScript on processing unicode strings

dim theScript as new AppleScriptMBS
dim scripttext as string
dim chars(-1) as string
dim n as string
dim i,c as Integer

const text="Hello World" // add unicode characters here!

c=len(text)
for i=1 to c
n=hex(asc(mid(text,i,1)))
while len(n)<4
n="0"+n
wend
chars.Append n
next

scripttext="display dialog (Åsdata utxt"+join(chars,"")+"Åt as Unicode text)"

MsgBox scripttext

// must use unicode to avoid error -1753
scripttext=ConvertEncoding(scripttext,encodings.UTF16)
theScript.UnicodeText=true
theScript.compile ScriptText
theScript.execute

Lasterror is set.
Text should be in MacRoman or UTF16 text encoding for best results.

AppleScriptMBS.CountScriptProperties as Integer

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns the number of properties in the script.
Example
dim s as String
dim a as AppleScriptMBS
dim i,c,cc as Integer
dim z,t as String

s=s+"property hello : ""Hallo Leute"""+chr(13)
s=s+"property just : ""Just a test"""+chr(13)
s=s+"display dialog hello"+chr(13)
s=s+"return just"+chr(13)

MsgBox "The script:"+chr(13)+s
a=new AppleScriptMBS

a.Compile s

MsgBox str(a.CountScriptProperties)+" properties"

Lasterror is set.

Some examples using this method:

AppleScriptMBS.Error as AppleScriptErrorMBS

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Returns an AppleScriptError object for details error information.
Example
dim a as AppleScriptMBS
dim e as AppleScriptErrorMBS
a=new AppleScriptMBS

a.Compile "tell application ""Finder"""+chr(13)+"open file ""test"""+chr(13)+"end tell"+chr(13)

// You may check for errors here.

a.Execute

e=a.Error

// in a breakpoint you can see an error message here.

Returns nil on any error.
The returned object may be empty.
The error information is changed whenever you compile or execute.

AppleScriptMBS.Execute

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Runs the current script.
Example
dim a as new AppleScriptMBS

a.Compile "beep"
a.Execute

Lasterror is set.
If you use this method, please make sure your own Application's HandleAppleEvent method doesn't block executing by returning true for unknown events.

AppleScriptMBS.ExecuteEvent(eventname as string, parameters() as string)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.4 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Executes an event inside the current script with the given parameters.
Example
dim a as new AppleScriptMBS
dim lines(-1) as string

lines.append "on test(a,b,c)"
lines.append "display dialog a"
lines.append "display dialog b"
lines.append "display dialog c"
lines.append "end test"

a.Compile join(lines,EndOfLine.Macintosh)
a.Execute

dim s(2) as string
s(0)="Hello"
s(1)="World"
s(2)="!"

a.ExecuteEvent("test",s)

The eventname must be the name of the event in pure ASCII or MacRoman encoding (and lowercase!).
The parameters can be unicode. The array is based on 0, so string in parameters(0) is the first parameter.

Lasterror is set.

Some examples using this method:

AppleScriptMBS.Result as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The result of the executed script as a string.
Example
dim a as new AppleScriptMBS

a.Compile "return ""hello"""
a.Execute

MsgBox a.Result

Returns "" on any error.

If unicode is enabled, this string is unicode (UTF16).

AppleScriptMBS.ResultAsStringArray as string()

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The result of the executed script as a string array.
Example
dim a as new AppleScriptMBS

a.Compile "return {""Hello"", ""World""}"
a.Execute

MsgBox join(a.ResultAsStringArray,EndOfLine) // Shows Hello World in two lines

a.Compile "tell application ""Mail"""+EndOfLine.Macintosh+"return name of every mailbox"+EndOfLine.Macintosh+"end tell"
a.Execute

MsgBox join(a.ResultAsStringArray,EndOfLine) // shows your Mailboxes

On any error the array is empty.
If unicode is enabled, this strings are unicode (UTF16).

AppleScriptMBS.ResultDisplayString as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 9.6 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The result of the executed script as a string.
Example
dim a as new AppleScriptMBS

a.Compile "return ""hello"""
a.Execute

MsgBox a.ResultDisplayString

This is the same as Result, but a flag is set to tell AppleScript that the string is for display to a human. So it may not be good for input to the AppleScript compiler.

Returns "" on any error.
If unicode is enabled, this string is unicode (UTF16).

AppleScriptMBS.ScriptProperty(index as Integer) as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The name of the property with the given index.
Example
dim s as String
dim a as AppleScriptMBS
dim i,c,cc as Integer
dim z,t as String

s=s+"property hello : ""Hallo Leute"""+chr(13)
s=s+"property just : ""Just a test"""+chr(13)
s=s+"display dialog hello"+chr(13)
s=s+"return just"+chr(13)

MsgBox "The script:"+chr(13)+s
a=new AppleScriptMBS

a.Compile s

c=a.CountScriptProperties

cc=c-1
for i=0 to cc
z=a.ScriptProperty(i)
if z<>"" then
if i=0 then
t=z
elseif i=cc then
t=t+" and "+z
else
t=t+", "+z
end if
end if
next

MsgBox str(c)+" properties in the script: "+t

t=a.ScriptProperty(0)
z=a.ScriptPropertyValue(t)
MsgBox "Value of the property named "+t+" is: "+z

a.ScriptPropertyValue(t)="Hello World!"

z=a.ScriptPropertyValue(t)
MsgBox "The new value of the property named "+t+" is: "+z

a.Execute

MsgBox "The result is: "+a.Result

Index is from 0 to CountScriptProperties-1.
Lasterror is set.

Some examples using this method:

AppleScriptMBS.ScriptPropertyValue(name as string) as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
property Apple Script MBS MacClassic Plugin 3.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
The value of a script property.
Example
Sub NewEmailinMail(subject as string, body as string, sender as string, receiver as string)
// make a new email in Apple Mail using AppleScript

dim lines(-1) as string


lines.append ""
lines.append "property MySubject : ""My Subject"""
lines.append "property MyBody : ""My Body"""
lines.append "property MyReceiver : ""test@test.test"""
lines.append "property MySender : ""test@test.test"""
lines.append ""
lines.append "tell application ""Mail"""
lines.append " activate"
lines.append " set NewMail to make new outgoing message with properties {visible:true, subject:MySubject, content:MyBody}"
lines.append " "
lines.append " tell NewMail"
lines.append " make new to recipient at beginning of to recipients with properties {address:MyReceiver}"
lines.append " end tell"
lines.append " "
lines.append " set the sender of NewMail to MySender"
lines.append "end tell"

dim a as new AppleScriptMBS

a.Compile Join(lines,EndOfLine.Macintosh)

// Change encoding
subject = ConvertEncoding(subject, encodings.MacRoman)
body = ConvertEncoding(body, encodings.MacRoman)
sender = ConvertEncoding(sender, encodings.MacRoman)
receiver = ConvertEncoding(receiver, encodings.MacRoman)

// replace line endoings
subject = ReplaceLineEndings(subject, EndOfLine.Macintosh)
body = ReplaceLineEndings(body, EndOfLine.Macintosh)
sender = ReplaceLineEndings(sender, EndOfLine.Macintosh)
receiver = ReplaceLineEndings(receiver, EndOfLine.Macintosh)

// set properties in Script
a.ScriptPropertyValue("MySubject")=subject
a.ScriptPropertyValue("MyBody")=body
a.ScriptPropertyValue("MySender")=sender
a.ScriptPropertyValue("MyReceiver")=receiver

a.Execute
End Sub

If name or value are UTF16 encoding, than the plugin passes UTF16. Else it passes the bytes and AppleScript may assume MacRoman encoding.
(Read and Write computed property)

Some examples using this property:

AppleScriptMBS.Source as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.1 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Decompiles the current script and returns the source code.
Example
dim a as new AppleScriptMBS

a.compile "beep"

MsgBox a.Source

AppleScriptMBS.SourceTextStyle as string

Type Topic Plugin Version macOS Windows Linux iOS Targets
method Apple Script MBS MacClassic Plugin 3.3 ✅ Yes ❌ No ❌ No ❌ No Desktop, Console & Web
Decompiles the current script and returns the source code text style.
Example
dim a as new AppleScriptMBS

a.compile "beep"

EditField1.SetTextAndStyle a.Source, a.SourceTextStyle

Doesn't work for unicode text.

Some examples using this method:

The items on this page are in the following plugins: MBS MacClassic Plugin.


The biggest plugin in space...