Driving Cocoa app via AE from command line tool.
Driving Cocoa app via AE from command line tool.
- Subject: Driving Cocoa app via AE from command line tool.
- From: Allan Hoeltje <email@hidden>
- Date: Mon, 25 Jul 2005 19:26:39 -0700
I am having a real hard time deciphering the Apple Script documentation. I
have made my Cocoa app scriptable with the appropriate .scriptSuite and
.scriptTerinology files. Here is the relevant snippet from the suite file
that describes the "theySay" attribute of my app:
<dict>
<key>Name</key>
<string>MyApp</string>
<key>AppleEventCode</key>
<string>myap</string>
<key>Classes</key>
<dict>
<key>MyApplication</key>
<dict>
<key>AppleEventCode</key>
<string>capp</string>
<key>Superclass</key>
<string>NSCoreSuite.NSApplication</string>
<key>Attributes</key>
<dict>
<key>theySay</key>
<dict>
<key>AppleEventCode</key>
<string>tsay</string>
<key>Type</key>
<string>NSString</string>
</dict>
</dict>
</dict>
</dict>
</dict>
Here is a script that successfully tells my app to display Hello World:
tell application "MyApp"
activate
set theySay to "Hello World!"
end tell
I want a C command line tool to do exactly what the above script does. I've
got the activate part working just fine but can't seem to get the "theySay"
attribute set. The following code was lifted almost verbatim from the
AppleScript Language Guide but does not seem to work:
// gMyAppAEDesc is already created and was used to launch the app
char * s = "Hello World!";
AppleEvent contactAE;
contactAE.descriptorType = typeNull;
contactAE.dataHandle = NULL;
OSStatus status = AECreateAppleEvent(
'capp', // AEEventClass
'tsay', // AEEventID
&gMyAppAEDesc, // AEAddressDesc *
kAutoGenerateReturnID, // AEReturnID
kAnyTransactionID, // AETransactionID
&contactAE ); // AppleEvent *
CFStringRef stringRef = CFStringCreateWithCString(
kCFAllocatorDefault,
(const char *)s,
kCFStringEncodingUTF8 );
CFIndex length = CFStringGetLength( stringRef );
CFIndex maxBytes = CFStringGetMaximumSizeForEncoding(
length, kCFStringEncodingUTF8 );
UInt8 * textBuf = malloc( maxBytes );
CFIndex actualBytes;
CFStringGetBytes( stringRef, CFRangeMake( 0, length ),
kCFStringEncodingUTF8,
0, true, (UInt8 *) textBuf, maxBytes, &actualBytes );
status = AEPutAttributePtr(
&contactAE, // AppleEvent *
keyDirectObject, // AEKeyword
typeUTF8Text, // DescType
textBuf, // const void *
actualBytes ); // Size
AppleEvent dummyReply;
dummyReply.descriptorType = typeNull;
dummyReply.dataHandle = NULL;
status = AESendMessage(
&contactAE, // const AppleEvent * event
&dummyReply, // AppleEvent * reply - can be NULL
kAENoReply, // AESendMode sendMode
kAEDefaultTimeout ); // long timeOutInTicks
My real code checks all of the status returns and they all are noErr. Not
only is this not working but seems to me to be VERY convoluted. Are Apple
events the best/blessed way for a command line tool to talk with a Cocoa
app?
Thanks!
-Allan
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden