Re: [cocoa scripting] getting script class attribute name
Re: [cocoa scripting] getting script class attribute name
- Subject: Re: [cocoa scripting] getting script class attribute name
- From: Dustin Voss <email@hidden>
- Date: Mon, 5 Sep 2005 17:26:09 -0700
On 5 Sep 2005, at 2:46 PM, Bill Cheeseman wrote:
on 2005-09-05 1:44 PM, Ken Victor at email@hidden wrote:
given that i have the key and apple event code for an attribute of a
class, is it possible to get the name of the attribute? ie, the name
the user would use in a script?
i want the name in order to be able to provide better error messages
and i'd prefer to get this at runtime rather than having to build
constants into my app.
I ran into this same issue recently and concluded that you must
code the
AppleScript names into your app. You could probably avoid this if
you were
willing to parse your sdef file via XML. I decided it was easier to
code the
names.
If you did want to parse the sdef file, you can use
OSACopyScriptingDefinition() to get it. This function is documented at:
http://developer.apple.com/documentation/Carbon/Reference/
Open_Scripti_Architecture/Open_Script_Arch/chapter_1.2_section_12.html
With the advent of NSXML, the parsing is trivial. You can use XPath
expressions to get the values you need:
http://developer.apple.com/documentation/Cocoa/Conceptual/
NSXML_Concepts/Articles/QueryingXML.html
For example, this should find the "frontmost" property:
// Get the bundle.
FSRef appRef;
CFDataRef sdefData;
CFURLRef appURL = CFBundleCopyBundleURL (CFBundleGetMainBundle());
CFURLGetFSRef (appURL, &appRef);
CFRelease (appURL);
// Get the SDEF.
NSError *err;
OSACopyScriptingDefinition (*appRef, kOSAModeNull, &sdefData);
NSXMLDocument *sdefDoc =
[[NSXMLDocument alloc] initWithData:(NSData *)sdefData options:0
error:&err];
NSXMLElement *sdefRoot = [sdefDoc rootElement];
CFRelease (sdefData);
// Get the property name.
NSArray *nodes = [sdefRoot nodesForXPath: @"//property[code=\"pisf\"]/
@name" error:&err];
NSString *name = [[nodes objectAtIndex:0] stringValue];
// Do something with 'name'.
// Clean up.
[sdefDoc release];
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden