3

I am trying to instantiate a SB class using Swift but it doesn't seem to work:

if let messageClass = (mail as! SBApplication).class(forScriptingClass:"outgoing message") {
        let message = (messageClass as! SBObject.Type).init(properties: ["subject": "message subjects"]) as MailOutgoingMessage
        mail.outgoingMessages!().add(message)
        print("Subject: \(message.subject)")
        print("Outgoing messages: \(mail.outgoingMessages!().count)")
    }

All I get in the output is:

Subject: nil
Outgoing messages: 0

I know that I should cast the message to a MailOutgoingMessage.type and not SBObject.type but I couldn't access the init method otherwise.

Someone has experience using Scripting Bridge with Swift? Clues?

altares
  • 115
  • 8

1 Answers1

2

Scripting Bridge is nasty, obfuscated and broken. If you have to use SB, Tony Ingraldi wrote some helper tools for Swift users. TBH, I recommend using AppleScript-ObjC over SB simply because AppleScript at least works.

For a native Swift-AE bridge, see SwiftAutomation. I'm still working on the documentation, but the code itself descends from appscript which is the only AppleScript alternative of the last 20 years that actually works right, so is just about beta-quality now.

Swift (and AppleScript) users who'd like to see Apple adopt SwiftAutomation in 10.3 are strongly encouraged to submit duplicate Radar tickets to bugreport.apple.com requesting it, e.g. here's one already copied to OpenRadar if you want to copy-paste it as-is (include the original rdar:// to help the Radar team resolve it quicker): openradar.appspot.com/29332915

foo
  • 239
  • 1
  • 2
  • Thank you. I didn't know about SwiftAutomation. I'm already using Tony Ingraldis' toolbox but I just wished to understand the SB syntax directly because I'm still learning Swift. – altares Dec 01 '16 at 17:15