views:

165

answers:

3

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.

A: 

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.

Dave DeLong
I'm using another framework called "AppScript", which is a wrapper for Apple Events. My question relates to usage of AppScript.
demonslayer319
@demonslayer319 - you asked how you would interact with Address Book in Objective-C. The Scripting Bridge does just that.
Dave DeLong
Actually, I'm not using Address Book at all in my code. I'm using AppScript for different reasons, but the general usage is the same from app to app, so to have this code (which came in the objc documentation for some random frustrating reason) translated into objc-appscript would be helpful for me.
demonslayer319
Dave: both objc-appscript and Scripting Bridge are ObjC-to-Apple Event bridges. (Appscript is more capable, reliable and better supported; SB is conveniently bundled in the OS. You pays your moneys, etc.)
has
+1  A: 

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.

TechZen
+1  A: 

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];
has