//************************************************************************************** // Filename: EmptyCM.cp // Part of Contextual Menu Workshop by Abracode Inc. // http://free.abracode.com/cmworkshop/ // Copyright © 2002-2003 Abracode, Inc. All rights reserved. // // Description: FSRef-based file sample CM // //************************************************************************************** #include "CFAbstractCMPlugin.h" #include "CMUtils.h" #include "MoreFilesExtras.h" #include "CFObjDel.h" //#include "StAllocThrowDisable.h" #include "DebugSettings.h" // Strings enum { kCommandStrings = 130, // STR# id kMenuStrIndx = 1 }; OSStatus FSRefCheckFileOrFolder(const FSRef *inRef, void *ioData); OSStatus FSRefProcessFileOrFolder(const FSRef *inRef, void *ioData); // --------------------------------------------------------------------------- // CMPluginExamineContext // --------------------------------------------------------------------------- // Have a look at the selection and decides whether to display any commands OSStatus CMPluginExamineContext( void *thisInstance, const AEDesc *inContext, AEDescList *outCommandPairs ) { // StAllocThrowDisable disableNewThrows;//for MSL operator new try { TRACE_STR( "\pFileSampleCM->CMPluginExamineContext" ); AbstractCMPluginType *theThis = (AbstractCMPluginType *)thisInstance; if(inContext == NULL) return errAENotAEDesc; if(inContext->descriptorType == typeNull) return errAENotAEDesc; UInt32 theFlags = kProcBreakOnFirst; if( CMUtils::ProcessObjectList( inContext, theFlags, FSRefCheckFileOrFolder ) ) { if( (theFlags & kListOutMultipleObjects) != 0) CMUtils::AddCommandToAEDescList("\pMultiple Files or Folders selected", kCMImplCommand, outCommandPairs); else CMUtils::AddCommandToAEDescList("\pFile or Folder selected", kCMImplCommand, outCommandPairs); /* StBundleResOpen resOpen( theThis->bundleRef ); if( resOpen.IsValid() ) { CMUtils::AddResCommand( outCommandPairs, kCommandStrings, kMenuStrIndx, kCMImplCommand ); } */ } } catch(...) { } return noErr; } // --------------------------------------------------------------------------- // CMPluginHandleSelection // --------------------------------------------------------------------------- // Carry out the command that the user selected. The commandID indicates // which command was selected OSStatus CMPluginHandleSelection( void *thisInstance, AEDesc *inContext, SInt32 inCommandID ) { // StAllocThrowDisable disableNewThrows;//for MSL operator new try { TRACE_STR( "\pFileSampleCM->CMPluginHandleSelection" ); AbstractCMPluginType *theThis = (AbstractCMPluginType *)thisInstance; if(inCommandID == kCMImplCommand) { UInt32 theFlags = kListClear; CMUtils::ProcessObjectList( inContext, theFlags, FSRefProcessFileOrFolder ); } } catch(...) { } return noErr; } // --------------------------------------------------------------------------- // CMPluginPostMenuCleanup // --------------------------------------------------------------------------- void CMPluginPostMenuCleanup( void *thisInstance ) { // StAllocThrowDisable disableNewThrows;//for MSL operator new try { TRACE_STR( "\pFileSampleCM->CMPluginPostMenuCleanup" ); AbstractCMPluginType *theThis = (AbstractCMPluginType *)thisInstance; } catch(...) { } } #pragma mark - OSStatus FSRefCheckFileOrFolder(const FSRef *inRef, void *ioData) { #pragma unused (ioData) if(inRef == NULL) return paramErr; //we are interested in real files or folders //sometimes we get FSRef not valid (InternetExplorer) FSCatalogInfo theInfo; return ::FSGetCatalogInfo(inRef, kFSCatInfoNodeFlags, &theInfo, NULL, NULL, NULL); } OSStatus FSRefProcessFileOrFolder(const FSRef *inRef, void *ioData) { #pragma unused (ioData) TRACE_STR( "\pFileSampleCM->ProcessFileOrFolder" ); if(inRef == NULL) return paramErr; OSStatus err = noErr; #if _DEBUG_ CFURLRef urlRef = ::CFURLCreateFromFSRef( kCFAllocatorDefault, inRef ); if(urlRef != NULL) { CFObjDel urlDel(urlRef); CFStringRef strRef = ::CFURLGetString( urlRef ); if(strRef != NULL) { Str255 theString; if( ::CFStringGetPascalString(strRef, theString, sizeof(Str255), kCFStringEncodingMacRoman) ) { DEBUG_STR( theString ); } else { DEBUG_STR( "\pFileSampleCM. CFStringGetPascalString failed" ); } } else { DEBUG_STR( "\pFileSampleCM. CFURLGetString failed" ); } } else { DEBUG_STR( "\pFileSampleCM. CFURLCreateFromFSRef failed" ); } #endif //_DEBUG_ FSCatalogInfo theInfo; err = ::FSGetCatalogInfo(inRef, kFSCatInfoNodeFlags, &theInfo, NULL, NULL, NULL); if ( err == noErr ) { //do something useful to the file/folder TRACE_STR( "\pFileSampleCM. Executing now!" ); } else { DEBUG_STR( "\pFileSampleCM. FSGetCatalogInfo failed" ); } /* careful with the following. Finder chokes when you send several events in a row if(err == noErr) {//tell Finder to update the item look CMUtils::FSRefUpdateFinderObject(inRef); } */ return err; }