How would I write the following rb-appscript in objc-appscript?

381 Views Asked by At

The documentation in the appscript objc-trunk randomly uses ruby in the section called "Performance Issues".

require "appscript"
include Appscript

desiredEmail = '[email protected]'

p app('Address Book').people[
        its.emails.value.contains(desiredEmail)
        ].name.get

How would this be written in Objective-C? I apologize if this seems like an overly basic question, I have 0 experience with Ruby.

Thanks.

3

There are 3 best solutions below

0
On BEST ANSWER

If you run the ruby script and use ASTranslate it should translate the raw appscript commands to Objc-appscript.

Edit01:

I think it will look something like this. I haven't run the tool to make the glue code so I'm guessing about the way the app name shows up.

#import "AddressBookGlue.h" //Dont know the precise name 

AddressBookApplication *abApp=[[AddressBookApplication alloc] initWithName: @"Address Book.app"];

NSString *desiredEmail=@"[email protected]"

NSString *returnedName= [[[[[[abApp people] emails] value] contains:desiredEmail] name] get]; 

Basically, it follows the same rules that Objectic-c uses when converting from a dot syntax: anywhere there is a dot in the original syntax, expect a bracket in Objective-C.

I might add that if you're going to be doing a lot of this type scripting, it would be best to spend a day or two learning the basics of ruby or python. It's a lot easier to work with OSA in a dot syntax than a nested syntax. Just looking at all those brackets in the documentation for Objc-appscript makes my eyes water.

4
On

From what I understand, that's printing the name of every person who has an email of "[email protected]".

There's no direct correlation for how to do this in Cocoa. Fortunately for you, Address Book is scriptable, which means you can use the Scripting Bridge framework to interact with it from a Cocoa app.

This page has a really great explanation on how to simply interact with Mail.app via ScriptingBridge: http://robnapier.net/blog/scripting-bridge-265

Hopefully that should give you enough information to get going in the right direction.

0
On

Apologies for the incompleteness of the objc-appscript manual, which was originally ported from rb-appscript as you can tell. (FWIW, I should have some time to work on appscript this spring.)

Translating the Ruby code back to AppleScript first is probably the easiest approach:

tell application "Address Book"
   get name of every person where value of its email contains "[email protected]"
end tell

Running it through ASTranslate gives this:

#import "ABGlue/ABGlue.h"
ABApplication *addressBook = [ABApplication applicationWithName: @"Address Book"];
ABReference *ref = [[[addressBook people] byTest: [[[ABIts emails] value] contains: @"[email protected]"]] name];
id result = [ref getItem];