Cocoa Views in Carbon Windows


Cocoa provides views that are either not currently available in HIToolbox or are available without full support. These include views such as WebView, PDFView, QTMovieView, and NSTokenField. In addition, the Cocoa and Carbon control hierarchies are incompatible, so it has been difficult or impossible to have views from both frameworks embedded within the same window.

A new type of HIView called HICocoaView provides a general solution to these problems. You can embed a Cocoa view (any subclass of NSView) inside the HIView control hierarchy of a Carbon window. This is accomplished by associating the Cocoa view with a Carbon wrapper view called HICocoaView, a subclass of HIView. You can use standard HIView functions to manipulate the wrapper view, and you can use Cocoa methods to manipulate the associated Cocoa view. HICocoaView is supported only in compositing windows.

HICocoaView is available in Mac OS X v10.5 and later. For more information, see Carbon-Cocoa Integration Guide.

Blogged with the Flock Browser

carbon and applescript

#include <Carbon/Carbon.h>

static OSStatus SimpleRunAppleScript(const char* theScript);

int main(int argc, char* argv[])

/* example: */

“tell application \”Finder\”\n”
” activate\n”
” select folder \”Documents\” of startup disk\n”
” open selection\n”
“end tell”);

return 0;

#ifdef __APPLE_CC__
#include <Carbon/Carbon.h>
#include <Carbon.h>
#include <Types.h>
#include <OSA.h>
#include <AppleScript.h>
#include <string.h>

/* SimpleRunAppleScript compiles and runs the AppleScript in
the c-style string provided as a parameter. The result returned
indicates the success of the operation. */
static OSStatus SimpleRunAppleScript(const char* theScript) {
return LowRunAppleScript(theScript, strlen(theScript), NULL);

/* LowRunAppleScript compiles and runs an AppleScript
provided as text in the buffer pointed to by text. textLength
bytes will be compiled from this buffer and run as an AppleScript
using all of the default environment and execution settings. If
resultData is not NULL, then the result returned by the execution
command will be returned as typeChar in this descriptor record
(or typeNull if there is no result information). If the function
returns errOSAScriptError, then resultData will be set to a
descriptive error message describing the error (if one is
available). */
static OSStatus LowRunAppleScript(const void* text, long textLength,
AEDesc *resultData) {
ComponentInstance theComponent;
AEDesc scriptTextDesc;
OSStatus err;
OSAID scriptID, resultID;

/* set up locals to a known state */
theComponent = NULL;
AECreateDesc(typeNull, NULL, 0, &scriptTextDesc);
scriptID = kOSANullScript;
resultID = kOSANullScript;

/* open the scripting component */
theComponent = OpenDefaultComponent(kOSAComponentType,
if (theComponent == NULL) { err = paramErr; goto bail; }

/* put the script text into an aedesc */
err = AECreateDesc(typeChar, text, textLength, &scriptTextDesc);
if (err != noErr) goto bail;

/* compile the script */
err = OSACompile(theComponent, &scriptTextDesc,
kOSAModeNull, &scriptID);
if (err != noErr) goto bail;

/* run the script */
err = OSAExecute(theComponent, scriptID, kOSANullScript,
kOSAModeNull, &resultID);

/* collect the results – if any */
if (resultData != NULL) {
AECreateDesc(typeNull, NULL, 0, resultData);
if (err == errOSAScriptError) {
OSAScriptError(theComponent, kOSAErrorMessage,
typeChar, resultData);
} else if (err == noErr && resultID != kOSANullScript) {
OSADisplay(theComponent, resultID, typeChar,
kOSAModeNull, resultData);
if (scriptID != kOSANullScript) OSADispose(theComponent, scriptID);
if (resultID != kOSANullScript) OSADispose(theComponent, resultID);
if (theComponent != NULL) CloseComponent(theComponent);
return err;

Blogged with Flock

Tags: , , , ,