Retired Document
Important: This sample code may not represent best practices for current development. The project may use deprecated symbols and illustrate technologies and techniques that are no longer recommended.
TestMoreAppleEvents/TestMoreAppleEvents.c
/* |
File: TestMoreAppleEvents.c |
Contains: Source for application to test MoreFinderEvents, MoreAppleEvents. |
DRI: George Warner |
Copyright: Copyright (c) 2000-2001 by Apple Computer, Inc., All Rights Reserved. |
Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc. |
("Apple") in consideration of your agreement to the following terms, and your |
use, installation, modification or redistribution of this Apple software |
constitutes acceptance of these terms. If you do not agree with these terms, |
please do not use, install, modify or redistribute this Apple software. |
In consideration of your agreement to abide by the following terms, and subject |
to these terms, Apple grants you a personal, non-exclusive license, under AppleÕs |
copyrights in this original Apple software (the "Apple Software"), to use, |
reproduce, modify and redistribute the Apple Software, with or without |
modifications, in source and/or binary forms; provided that if you redistribute |
the Apple Software in its entirety and without modifications, you must retain |
this notice and the following text and disclaimers in all such redistributions of |
the Apple Software. Neither the name, trademarks, service marks or logos of |
Apple Computer, Inc. may be used to endorse or promote products derived from the |
Apple Software without specific prior written permission from Apple. Except as |
expressly stated in this notice, no other rights or licenses, express or implied, |
are granted by Apple herein, including but not limited to any patent rights that |
may be infringed by your derivative works or by other works in which the Apple |
Software may be incorporated. |
The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO |
WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED |
WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
COMBINATION WITH YOUR PRODUCTS. |
IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR |
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION |
OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT |
(INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN |
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Change History (most recent first): |
*/ |
//================================================================== |
#ifndef TRUE |
#define FALSE 0 |
#define TRUE !FALSE |
#endif |
#define PRINT_APPLEEVENTS FALSE // set this true to AEPrint AppleEvents before sending them. |
//================================================================== |
// includes |
//================================================================== |
#include <Fonts.h> |
#include <Script.h> |
#include <stdio.h> |
#include <SIOUX.h> |
#include <AEHelpers.h> |
#include <TextUtils.h> |
#include <FinderRegistry.h> |
#include "MoreAEObjects.h" |
#include "MoreFinderEvents.h" |
#include "MoreAppleEvents.h" |
//================================================================== |
// compiler directives |
//================================================================== |
#define TEST_MOREFEGETSELECTION TRUE |
#define TEST_MOREFEGETEVERYITEMONDESKTOP FALSE |
#if (!TEST_MOREFEGETSELECTION && !TEST_MOREFEGETEVERYITEMONDESKTOP) |
#error set one of the above to true |
#endif |
#define TEST_GET_KIND FALSE |
#define TEST_GET_COMMENT FALSE |
#define TEST_SET_COMMENT FALSE |
#define TEST_OPEN_INFO_WINDOW FALSE |
#define TEST_MFE_OPEN_FILE FALSE |
#define TEST_MFE_DUPLICATE FALSE |
#define TEST_MFE_MOVE FALSE |
#define TEST_MFE_GETALIASASOBJECT TRUE |
#if (!TEST_GET_KIND && !TEST_GET_COMMENT && !TEST_SET_COMMENT && !TEST_OPEN_INFO_WINDOW && !TEST_MFE_OPEN_FILE && !TEST_MFE_DUPLICATE && !TEST_MFE_MOVE && !TEST_MFE_GETALIASASOBJECT) |
#error set one of the above to true |
#endif |
#define USE_CFSTRINGS TRUE |
//================================================================== |
static long CopyP2CStr(register unsigned char *pPStr,register char *pCStr) |
{ |
register short i,len = *pPStr++; |
for (i = 1;i <= len; i++) |
*pCStr++ = *pPStr++; |
*pCStr = 0; |
return len; |
} |
//------------------------------------------------------------------ |
static long BuildPathVector(FSSpec *pFSSpecPtr,char *path) |
{ |
char* p = path; |
long len,retLen = 0; |
if (pFSSpecPtr->parID != fsRtParID) |
{ |
FSSpec parentFSSpec; |
if (FSMakeFSSpec(pFSSpecPtr->vRefNum,pFSSpecPtr->parID,"\p",&parentFSSpec) != noErr) |
return 0; |
len = BuildPathVector(&parentFSSpec,p); |
p += len; |
*p++ = ':'; |
retLen += len + 1; |
} |
len = CopyP2CStr(pFSSpecPtr->name,p); |
p += len; |
retLen += len; |
*p = 0; // terminate C string |
return retLen; |
} |
//------------------------------------------------------------------ |
static long GetFullPath(FSSpec *pFSSpecPtr,char *path,OSErr *pOSErr) |
{ |
if (nil != pOSErr) |
*pOSErr = noErr; |
return BuildPathVector(pFSSpecPtr,path); |
} |
//------------------------------------------------------------------ |
static pascal Boolean MyAEIdleProc(EventRecord *pEventRecord, long *sleepTime, RgnHandle *mouseRgn) |
{ |
*sleepTime = 10; |
*mouseRgn = nil; |
if (pEventRecord != nil) |
{ |
switch (pEventRecord->what) |
{ |
KeyDown: |
if (((pEventRecord->modifiers & cmdKey) != 0) && |
((pEventRecord->message & charCodeMask) == '.')) |
return true; |
} |
} |
SIOUXHandleOneEvent(pEventRecord); |
return false; |
} |
//------------------------------------------------------------------ |
extern int dprintf(const char *format, ...); |
//------------------------------------------------------------------ |
int main(void) |
{ |
Str255 tStr255; |
AEDescList objectList; |
AEIdleUPP tAEIdleUPP = NewAEIdleUPP(MyAEIdleProc); |
OSErr anErr; |
// Set the SIOUX window defaults |
SIOUXSettings.autocloseonquit = FALSE; |
SIOUXSettings.asktosaveonclose = FALSE; |
SIOUXSettings.showstatusline = FALSE; |
SIOUXSettings.columns = 132; |
SIOUXSettings.rows = 24; |
SIOUXSettings.fontsize = 10; |
GetFNum("\pMonaco",&SIOUXSettings.fontid); |
SIOUXSettings.standalone = TRUE; |
printf("\nWelcome to MAE_TestApp!\n"); |
#if TEST_MOREFEGETSELECTION |
anErr = MoreFEGetSelection(tAEIdleUPP,&objectList); |
#elif TEST_MOREFEGETEVERYITEMONDESKTOP |
anErr = MoreFEGetEveryItemOnDesktop(tAEIdleUPP,&objectList); |
#endif |
if (noErr == anErr) |
{ |
long numDocs, index; |
AEKeyword keyword; |
#if TEST_MOREFEGETSELECTION |
//printf("\nMoreFEGetSelection good!"); |
#elif TEST_MOREFEGETEVERYITEMONDESKTOP |
//printf("\nMoreFEGetEveryItemOnDesktop good!"); |
#endif |
anErr = AECountItems (&objectList, &numDocs); |
if (noErr == anErr) |
{ |
//printf("\nAECountItems good! (%ld)",numDocs); |
for (index = 1; index <= numDocs; index++) |
{ |
AliasHandle tAliasHandle = nil; |
FSRef tFSRef; |
FSSpec tFSSpec; |
AEDesc tAEDesc; |
anErr = AEGetNthDesc(&objectList,index,typeWildCard,&keyword,&tAEDesc); |
if (noErr != anErr) |
{ |
printf("\n¥AEGetNthDesc(typeWildCard) err: %d.",anErr); |
continue; |
} |
else |
{ |
//printf("\nAEGetNthDesc(typeWildCard) good, (actual: '%4.4s').",&tAEDesc.descriptorType); |
if (typeObjectSpecifier == tAEDesc.descriptorType) |
{ |
anErr = MoreFEGetObjectAsAlias(&tAEDesc,&tAliasHandle,tAEIdleUPP); |
if (noErr == anErr) |
{ |
Boolean wasChanged; |
anErr = FSResolveAliasWithMountFlags(NULL,tAliasHandle, |
&tFSRef,&wasChanged,kResolveAliasFileNoUI); |
if (noErr != anErr) |
{ |
printf("\n¥ResolveAlias[WithMountFlags] err: %d.",anErr); |
continue; |
} |
} |
else |
{ |
printf("\n¥MoreFEGetObjectAsAlias err: %d.",anErr); |
continue; |
} |
} |
else |
{ |
if (typeFSRef != tAEDesc.descriptorType) |
{ |
anErr = AECoerceDesc(&tAEDesc,typeFSRef,&tAEDesc); |
if (noErr != anErr) |
{ |
printf("\n¥AECoerceDesc err: %d.",anErr); |
continue; |
} |
} |
anErr = AEGetDescData(&tAEDesc,&tFSRef,sizeof(FSRef)); |
if (noErr != anErr) |
{ |
printf("\n¥AEGetDescData err: %d.",anErr); |
continue; |
} |
anErr = FSNewAlias( nil, &tFSRef, &tAliasHandle); |
if (noErr != anErr) |
{ |
printf("\n¥FSNewAlias err: %d.",anErr); |
continue; |
} |
} |
} |
// get the FSRef as a FSSpec |
anErr = FSGetCatalogInfo(&tFSRef,kFSCatInfoNone,NULL,NULL,&tFSSpec,NULL); |
if (noErr != anErr) continue; |
GetFullPath(&tFSSpec,(char *) &tStr255,&anErr); |
printf("\n Nth: '%s'",tStr255); |
#if TEST_GET_KIND |
#if USE_CFSTRINGS |
{ |
CFStringRef tCFString = nil; |
CFIndex length = 0; |
CFIndex dataSize = 0; |
anErr = MoreFEGetKindCFString(&tFSRef,&tCFString,tAEIdleUPP); |
if (noErr == anErr) |
{ |
char* dataPtr = nil; |
if (nil != tCFString) |
{ |
length = CFStringGetLength(tCFString); |
dataSize = sizeof(UniChar) * (length + 1); |
dataPtr = (char*) NewPtrClear(dataSize); |
if (nil != dataPtr) |
{ |
if (!CFStringGetCString(tCFString,dataPtr,dataSize,kCFStringEncodingASCII)) |
{ |
if (!CFStringGetCString(tCFString,dataPtr,dataSize,kCFStringEncodingUTF8)) |
{ |
DisposePtr(dataPtr); |
dataPtr = NULL; |
length = dataSize = 0; |
} |
} |
} |
} |
if (length > 0) |
printf(" = {kind = \"%s\"}.",dataPtr); |
else |
printf(" = {kind = \"<none!>\"}."); |
if (nil != dataPtr) |
DisposePtr(dataPtr); |
} else |
printf("\n¥MoreFEGetKindCFString err: %d.",anErr); |
} |
#else |
{ |
Size actualSize; |
anErr = MoreFEGetKind(&tFSSpec,tStr255,tAEIdleUPP); |
actualSize = tStr255[0]; |
CopyPascalStringToC(tStr255,(Ptr) tStr255); |
if (noErr == anErr) |
{ |
if (actualSize > 0) |
{ |
printf("{kind = \"%s\"}.",tStr255); |
} |
else |
printf("{kind = \"<none!>\"}."); |
} else |
printf("\n¥MoreFEGetKind err: %d.",anErr); |
} |
#endif USE_CFSTRINGS |
#endif TEST_GET_KIND |
#if TEST_GET_COMMENT |
#if USE_CFSTRINGS |
{ |
CFStringRef tCFString = nil; |
CFIndex length = 0; |
CFIndex dataSize = 0; |
anErr = MoreFEGetCommentCFString(&tFSRef,&tCFString,tAEIdleUPP); |
if (noErr == anErr) |
{ |
char* dataPtr = nil; |
if (nil != tCFString) |
{ |
length = CFStringGetLength(tCFString); |
dataSize = sizeof(UniChar) * (length + 1); |
dataPtr = (char*) NewPtrClear(dataSize); |
if (nil != dataPtr) |
{ |
if (!CFStringGetCString(tCFString,dataPtr,dataSize,kCFStringEncodingASCII)) |
{ |
if (!CFStringGetCString(tCFString,dataPtr,dataSize,kCFStringEncodingUTF8)) |
{ |
DisposePtr(dataPtr); |
dataPtr = NULL; |
length = dataSize = 0; |
} |
} |
} |
} |
if (length > 0) |
printf(" = \"%s\".",dataPtr); |
else |
printf(" = \"<no comment!>\"."); |
#if TEST_SET_COMMENT |
if (length > 0) |
anErr = MoreFESetCommentCFString(&tFSRef,CFSTR(""),tAEIdleUPP); |
else |
anErr = MoreFESetCommentCFString(&tFSRef,CFSTR("What the CF-ork?"),tAEIdleUPP); |
if (noErr == anErr) |
printf("\nMoreFESetComment good!"); |
else |
printf("\n¥MoreFESetComment err: %d.",anErr); |
#endif TEST_SET_COMMENT |
if (nil != dataPtr) |
DisposePtr(dataPtr); |
} else |
printf("\n¥MoreFEGetComment err: %d.",anErr); |
} |
#else |
{ |
Size actualSize; |
anErr = MoreFEGetComment(&tFSSpec,tStr255,tAEIdleUPP); |
actualSize = tStr255[0]; |
CopyPascalStringToC(tStr255,(Ptr) tStr255); |
if (noErr == anErr) |
{ |
if (actualSize > 0) |
{ |
printf(" = \"%s\".",tStr255); |
} |
else |
printf(" = \"<no comment!>\"."); |
#if TEST_SET_COMMENT |
if (tStr255[0]) |
anErr = MoreFESetComment(&tFSSpec,"\p",tAEIdleUPP); |
else |
anErr = MoreFESetComment(&tFSSpec,"\pWhat the fork?",tAEIdleUPP); |
if (noErr == anErr) |
printf("\nMoreFESetComment good!"); |
else |
printf("\n¥MoreFESetComment err: %d.",anErr); |
#endif TEST_SET_COMMENT |
} else |
printf("\n¥MoreFEGetComment err: %d.",anErr); |
} |
#endif USE_CFSTRINGS |
#endif TEST_GET_COMMENT |
#if TEST_OPEN_INFO_WINDOW |
anErr = MoreFEOpenInfoWindow(&tFSSpec,tAEIdleUPP); |
if (noErr == anErr) |
printf("\nMoreFEOpenInfoWindow good!"); |
else |
printf("\n¥MoreFEOpenInfoWindow err: %d.",anErr); |
#endif TEST_OPEN_INFO_WINDOW |
#if TEST_MFE_OPEN_FILE |
anErr = MoreFEOpenFile(&tFSSpec); |
if (noErr == anErr) |
printf("\nMoreFEOpenFile good!"); |
else |
printf("\n¥MoreFEOpenFile err: %d.",anErr); |
#endif TEST_MFE_OPEN_FILE |
#if (TEST_MFE_DUPLICATE || TEST_MFE_MOVE) |
{ |
FSSpec folderFSSpec = {0,0,"\p"}; |
anErr = FSMakeFSSpec(0,0,"\p",&folderFSSpec); |
if (noErr != anErr) |
printf("\n¥FSMakeFSSpec err: %d.",anErr); |
else |
{ |
AEDesc resultObjDesc = {typeNull,nil}; |
#if TEST_MFE_DUPLICATE |
anErr = MoreFEDuplicate(&tFSSpec,&folderFSSpec,false,&resultObjDesc,tAEIdleUPP); |
if (noErr != anErr) |
printf("\n¥MoreFEDuplicate err: %d.",anErr); |
#elif TEST_MFE_MOVE |
anErr = MoreFEMove(&tFSSpec,&folderFSSpec,false,&resultObjDesc,tAEIdleUPP); |
if (noErr != anErr) |
printf("\n¥MoreFEMove err: %d.",anErr); |
#else |
#error "ether TEST_MFE_DUPLICATE or TEST_MFE_MOVE should be true." |
#endif |
#if PRINT_APPLEEVENTS // set this true to AEPrint the result |
else |
{ |
Handle strHdl; |
anErr = AEPrintDescToHandle(&resultObjDesc,&strHdl); |
if (noErr == anErr) |
{ |
char nul = '\0'; |
PtrAndHand(&nul,strHdl,1); |
printf("\n¥MoreFEDuplicate:resultObjDesc=%s.",*strHdl); fflush(stdout); |
DisposeHandle(strHdl); |
} |
} |
#endif |
} |
} |
#endif (TEST_MFE_DUPLICATE || TEST_MFE_MOVE) |
#if TEST_MFE_GETALIASASOBJECT |
if (nil == tAliasHandle) |
{ |
printf("\n¥MoreFEGetAliasAsObject requires valid AliasHandle."); |
continue; |
} |
else |
{ |
anErr = MoreFEGetAliasAsObject(tAliasHandle,&tAEDesc,tAEIdleUPP); |
if (noErr != anErr) |
printf("\n¥MoreFEGetAliasAsObject err: %d.",anErr); |
#if 1 //PRINT_APPLEEVENTS // set this true to AEPrint the result |
else |
{ |
Handle strHdl; |
anErr = AEPrintDescToHandle(&tAEDesc,&strHdl); |
if (noErr == anErr) |
{ |
char nul = '\0'; |
PtrAndHand(&nul,strHdl,1); |
printf("\n¥MoreFEGetAliasAsObject:tAEDesc=%s.",*strHdl); fflush(stdout); |
DisposeHandle(strHdl); |
} |
} |
#endif |
} |
#endif TEST_MFE_GETALIASASOBJECT |
if (nil != tAliasHandle) |
DisposeHandle((Handle) tAliasHandle); |
} // for (index = 1; index <= numDocs; index++) |
anErr = noErr; |
} |
else |
printf("\n¥AECountItems err: %d.",anErr); |
(void) MoreAEDisposeDesc(&objectList); |
} |
else |
printf("\n¥MoreFEGetSelection err: %d.",anErr); |
DisposeAEIdleUPP(tAEIdleUPP); |
return 0; |
} |
Copyright © 2003 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2003-01-14