どのように私は、アプリケーション内のFinderのエイリアスを作成するのですか?

StackOverflow https://stackoverflow.com/questions/1928139

  •  20-09-2019
  •  | 
  •  

質問

CocoaアプリケーションからFinderのエイリアスを作成するために必要なコードは何ですか? OS X 10.5、10.6、10.7との間でそのコードの違いはありますか?

役に立ちましたか?

解決

OS X 10.6は、あなたがNSUrlwriteBookmarkData:toURL:options:error:メソッドを使用することができますので、

<のhref = "からhttp://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html#//apple_ref/occ/clm / NSURL / writeBookmarkData:toURL:オプション:エラー:」のrel = "nofollowを">ドキュメントでます:

  

は、指定されたと指定された場所で、ディスク上のエイリアスファイルを作成します。   ブックマークデータます。

サンプルコード:

NSURL *originalUrl = [NSURL fileURLWithPath:@"/this/is/your/path"];
NSURL *aliasUrl = [NSURL fileURLWithPath:@"/your/alias/path"];
NSData *bookmarkData = [originalUrl bookmarkDataWithOptions: NSURLBookmarkCreationSuitableForBookmarkFile includingResourceValuesForKeys:nil relativeToURL:nil error:NULL];

if(bookmarkData != nil) {
    BOOL success = [NSURL writeBookmarkData:bookmarkData toURL:aliasUrl options:NSURLBookmarkCreationSuitableForBookmarkFile error:NULL];
    if(NO == success) {
        //error
    }
}

しかし、そのように作成されたエイリアスは、以前のOS Xのバージョンと下位互換性がない(事前10.6)

他のヒント

別名プログラムを作成する方法を見てみましょう。

- (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);
}

また、AppleScriptを使用することができます。

osascript -e 'tell application "Finder" to make alias file to POSIX file "/Applications/myapp.app" at POSIX file "/Applications/"'

Swift3を使用して、指定されたブックマークデータと指定された位置にディスク上のエイリアスファイルを作成します。

サンプルコード:

let url = URL(string: "originalURL")
let aliasUrl = URL(string: "aliasURL")
do{
      let data = try url.bookmarkData(options: .suitableForBookmarkFile, includingResourceValuesForKeys: nil, relativeTo: nil)
            try URL.writeBookmarkData(data, to: aliasUrl)
  }catch{}

あなたは-[NSFileManager linkItemAtPath:toPath:error:]をしたいです。アフィクは、それとその関連方法は、すべてのバージョン間の好適な手段です。

scroll top