Re: How to launch Finder with given directory from Carbon app
Re: How to launch Finder with given directory from Carbon app
- Subject: Re: How to launch Finder with given directory from Carbon app
- From: Philippe Casgrain <email@hidden>
- Date: Wed, 11 Feb 2009 09:19:38 -0500
On 2009-02-11, at 03:36 , Dieter Oberkofler wrote:
How would I launch the Finder with a given directory from a Carbon
app?
(In the Windows API this would be done by using ShellExecute(...,
"explore", "/mydirectory", ...). What is the counterpart in the Carbon
API?)
Send an 'open' AppleEvent to the Finder with the folder to open as a
parameter. You can also use AppleScript, which amounts to the same
thing, but you can store a string in your resources instead of
compiled code:
'tell application "Finder" to open "/mydirectory"'
Here is a function, SimpleRunAppleScript, which takes a string
representing an AppleScript and runs it.
(Note: "TMCStringUTF8" is our own UTF-8 string class. Substituting
your own, or CFString, is an exercise for the reader).
------------------------------------------------------------------------------
#include <AppleScript.h>
/
*SDOC
***********************************************************************
Name: LowRunAppleScript
Action: 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).
Params: text - script to run
resultData - result from AppleEvent
Returns: OSStatus, noErr if no error
Comments:
***********************************************************************EDOC
*/
static OSStatus LowRunAppleScript(const TMCStringUTF8& theScript,
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,
typeAppleScript);
if (theComponent == NULL)
{
err = paramErr;
goto bail;
}
/* put the script text into an aedesc */
err = theScript.ToAEDesc( &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);
}
}
bail:
AEDisposeDesc(&scriptTextDesc);
if (scriptID != kOSANullScript)
OSADispose(theComponent, scriptID);
if (resultID != kOSANullScript)
OSADispose(theComponent, resultID);
if (theComponent != NULL)
CloseComponent(theComponent);
return err;
}
/
*SDOC
***********************************************************************
Name: SimpleRunAppleScript
Action: compiles and runs the AppleScript in the c-style string
provided
as a parameter. The result returned indicates the success of the
operation.
Params: theScript - script to run
textResult - (optional) text result from AppleEvent (if any)
Returns: OSStatus, noErr if no error
Comments:
***********************************************************************EDOC
*/
OSStatus SimpleRunAppleScript(const TMCStringUTF8& theScript,
TMCStringUTF8* textResult)
{
OSStatus err;
AEDesc resultData;
err = LowRunAppleScript(theScript, &resultData);
if ((noErr == err) && (textResult))
if (typeChar == resultData.descriptorType) // something in the
return value
{
Size theLength;
Ptr theData;
theLength = AEGetDescDataSize(&resultData);
theData = (char *)malloc((unsigned long)theLength+1);
if (theData)
{
if (theLength > 2) // copy the data to the buffer
{
err = AEGetDescData(&resultData, theData, theLength);
theData[theLength-1] = 0; //we don't want the quotes
textResult->FromCPtr(theData+1);
}
free(theData);
}
}
return err;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden