1

I am trying to use Apple Pages (from iWork) with Objective-C's Scripting Bridge.

This is the working AppleScript:

tell application "Pages"
    set name of item 1 of contents of (get selection) to "myLittleTextBox"
end tell

How can I achieve the same thing in Objective-C with Scripting Bridge?

I tried the tip under Cocoa Scripting Bridge and <contents> element but no luck ...

The strange thing is that reading the properties is no problem:

PagesApplication *myPages = [SBApplication applicationWithBundleIdentifier:@"com.apple.iWork.Pages"];
NSLog(@"myPages.selection.properties:%@",myPages.selection.properties);

... but I've had no luck setting or accessing the objects in the selection.

Of course, I could send the AppleScript via NSAppleScript but hey, that would be too easy. ;)

Community
  • 1
  • 1

2 Answers2

1

Well I can get the property item. The returned property object is an Array.

id  selObject = pages.selection.properties;


NSString*  theName = [[selObject objectAtIndex:0]objectForKey:@"name"];
NSLog(@"theName = %@",theName);

And to set it:

 id selObject2 =  pages.selection.get;


[selObject2 setValue:@"myPage"  forKey:@"name"];
markhunte
  • 6,805
  • 2
  • 25
  • 44
  • you made my day! That is working!! What I dont understand if I debug the selObject2 its a __NSArrayM and it seems it has nothing in it... I would never came up with the idear of calling a setValue forKey on a __NSArrayM Object! – Peter Vogel May 21 '13 at 10:52
  • It did throw me when I checked out the class to get an idea of what was being returned. Have a look at https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/SBObject_Class/SBObject/SBObject.html and https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ScriptingBridgeConcepts/UsingScriptingBridge/UsingScriptingBridge.html Which will give you some insight into it. (not a lot but some :-) ) – markhunte May 21 '13 at 17:45
0

Never mind NSAppleScript; AppleScriptObjC has been a standard feature since 10.6. ASOC allows your ObjC code to treat AppleScript script objects just like regular Cocoa classes and objects, and AppleScript is easier and more reliable than Scripting Bridge for communicating with "AppleScriptable" applications. See this post for more information.

Community
  • 1
  • 1
foo
  • 3,171
  • 17
  • 18