Cocoa アプリケーションから Finder エイリアスを作成するために必要なコードは何ですか? OS X 10.5、10.6、および 10.7 の間でそのコードに違いはありますか?
2579 次
4 に答える
4
How to Create an Alias Programmatically を見てください。
- (void)makeAliasToFolder:(NSString *)destFolder inFolder:(NSString *)parentFolder withName:(NSString *)name
{
// Create a resource file for the alias.
FSRef parentRef;
CFURLGetFSRef((CFURLRef)[NSURL fileURLWithPath:parentFolder], &parentRef);
HFSUniStr255 aliasName;
FSGetHFSUniStrFromString((CFStringRef)name, &aliasName);
FSRef aliasRef;
FSCreateResFile(&parentRef, aliasName.length, aliasName.unicode, 0, NULL, &aliasRef, NULL);
// Construct alias data to write to resource fork.
FSRef targetRef;
CFURLGetFSRef((CFURLRef)[NSURL fileURLWithPath:destFolder], &targetRef);
AliasHandle aliasHandle = NULL;
FSNewAlias(NULL, &targetRef, &aliasHandle);
// Add the alias data to the resource fork and close it.
ResFileRefNum fileReference = FSOpenResFile(&aliasRef, fsRdWrPerm);
UseResFile(fileReference);
AddResource((Handle)aliasHandle, 'alis', 0, NULL);
CloseResFile(fileReference);
// Update finder info.
FSCatalogInfo catalogInfo;
FSGetCatalogInfo(&aliasRef, kFSCatInfoFinderInfo, &catalogInfo, NULL, NULL, NULL);
FileInfo *theFileInfo = (FileInfo*)(&catalogInfo.finderInfo);
theFileInfo->finderFlags |= kIsAlias; // Set the alias bit.
theFileInfo->finderFlags &= ~kHasBeenInited; // Clear the inited bit to tell Finder to recheck the file.
theFileInfo->fileType = kContainerFolderAliasType;
FSSetCatalogInfo(&aliasRef, kFSCatInfoFinderInfo, &catalogInfo);
}
アップルスクリプトも使えます
osascript -e 'tell application "Finder" to make alias file to POSIX file "/Applications/myapp.app" at POSIX file "/Applications/"'
于 2012-06-19T06:55:35.570 に答える
-1
あなたがしたい-[NSFileManager linkItemAtPath:toPath:error:]。AFIK、それとそれに関連するメソッドは、すべてのバージョンで推奨される手段です。
于 2009-12-18T14:07:20.970 に答える