Skip to content

Instantly share code, notes, and snippets.

@hetima
Created August 3, 2011 11:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hetima/1122416 to your computer and use it in GitHub Desktop.
Save hetima/1122416 to your computer and use it in GitHub Desktop.
set FileType And Creator on i386 x86_64
//i386 x86_64用
//needs CoreServices.framework
BOOL setFileTypeAndCreator(NSString* asFilePath, const char* typeStr, const char* creatorStr);
UInt32 OSTypeFromString(const char* inStr);
UInt32 OSTypeFromString(const char* inStr)
{
UInt32 outType;
char *buff;
if(strlen(inStr)!=4){
outType=0;
}else{
buff=(char*)&outType;
*(buff)=*(inStr+3);
*(buff+1)=*(inStr+2);
*(buff+2)=*(inStr+1);
*(buff+3)=*(inStr);
}
return outType;
}
BOOL setFileTypeAndCreator(NSString* asFilePath, const char* typeStr, const char* creatorStr){
BOOL result=NO;
UInt32 fType=OSTypeFromString(typeStr);
UInt32 fCreator=OSTypeFromString(creatorStr);
FSRef fRef;
FSCatalogInfo catInfo;
FInfo *finderInfo = (FInfo *)&(catInfo.finderInfo);
CFURLRef url;
//LOG(@"%s,%s=%d,%d",typeStr,creatorStr,fType,fCreator);
url=CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)asFilePath, kCFURLPOSIXPathStyle, false);
if(CFURLGetFSRef(url, &fRef)){
if (FSGetCatalogInfo(&fRef, kFSCatInfoFinderInfo, &catInfo, NULL, NULL, NULL) == noErr){
finderInfo->fdCreator = fCreator;
finderInfo->fdType = fType;
//if(fCreator==0)finderInfo->fdFlags &= ~kHasBeenInited;
if (FSSetCatalogInfo(&fRef, kFSCatInfoFinderInfo, &catInfo)==noErr) {
result=YES;
}
}
CFRelease(url);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment