0

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

require "appscript"
include Appscript

desiredEmail = 'sam.brown@foo.com'

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.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
demonslayer319
  • 936
  • 1
  • 10
  • 20

3 Answers3

1

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=@"sam.brown@foo.com"

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
  • 64,370
  • 15
  • 118
  • 145
1

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 "hengist.podd@virgin.net"
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: @"hengist.podd@virgin.net"]] name];
id result = [ref getItem];
has
  • 1,106
  • 7
  • 6
0

From what I understand, that's printing the name of every person who has an email of "sam.brown@foo.com".

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
  • 242,470
  • 58
  • 448
  • 498
  • I'm using another framework called "AppScript", which is a wrapper for Apple Events. My question relates to usage of AppScript. – demonslayer319 Feb 21 '10 at 05:03
  • @demonslayer319 - you asked how you would interact with Address Book in Objective-C. The Scripting Bridge does just that. – Dave DeLong Feb 21 '10 at 16:04
  • 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 Feb 22 '10 at 08:06
  • 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 Feb 25 '10 at 19:59