Run AppleScript with NSAppleScriptMBS

Hello @Beatrix_Willius & support MBS Team

I try to execute an AppleScript command and get the boolean result.
But, i have an Exception : error Dictionary (d = Nil)

if Checkbox1.Value = true then
  Var source as string = "tell application \""System Events\"" to tell security preferences to get automatic login"
  // compile, run and show boolean value:
  Var n As new NSAppleScriptMBS(source)
  Var error As Dictionary
  Var d As NSAppleEventDescriptorMBS = n.execute(Error)
  MsgBox str(d.booleanValue)
end

Thanks for help.

You don’t return anything from the script. Therefore, d is nil. Try the following script and you will get a result:

Var source as string = "return ""test"""
// compile, run and show boolean value:
Var n As new NSAppleScriptMBS(source)
Var error As Dictionary
Var d As NSAppleEventDescriptorMBS = n.execute(Error)
MsgBox str(d.stringValue)

Thank you @Beatrix_Willius for your quick response.
It works fine with your example but I have the same mistake with mine!
if d is nil, can it come from the syntax of my source string? (maybe a problem in the quotation marks?) (the original script string is “tell application “System Events” to tell security preferences to get automatic login”) (antislash not visible before each double quote ")
Thanks

Hi Denis, you just need to remove the backslashes in your script and it will work properly.

Also, you can check the error message:

if error <> nil then
  MsgBox error.Value(NSAppleScriptMBS.NSAppleScriptErrorMessage)
end

And it will tell you:

Expected expression, property or key form, etc. but found unknown token.

That unknown token is the backslash in your source script.

Thanks. But i have this error message ! “Not allowed to send Apple events to System Events”
Why ?

It sounds like you denied AppleScript permission for your app when it asked.

You need to go to System Preferences > Security & Privacy > Privacy > Automation, then scroll down until you find your app name and check the box for System Events in order to grant permission.

If your app is codesigned, then you’ll need to include an NSAppleEventsUsageDescription key value pair in the info.plist

If your app is codesigned as a hardened runtime for notarization, then you’ll also need to include AppleEvents entitlements while signing