Question

Quel est le code nécessaire pour créer un alias du Finder à partir d'une application Cocoa? Y a-t-il des différences pour ce code entre OS X 10.5, 10.6 et 10.7?

Était-ce utile?

La solution

Depuis OS X 10.6, vous pouvez utiliser la méthode de NSUrl de writeBookmarkData:toURL:options:error:

De documentation :

  

crée un fichier alias sur le disque à un emplacement spécifié avec spécifié   les données de signets.

Exemple de code:

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
    }
}

Cependant, les alias créés de cette façon ne sont pas rétrocompatible aux versions OS X antérieure (avant 10.6)

Autres conseils

Jetez un oeil à Comment créer un 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);
}

Vous pouvez également utiliser AppleScript

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

crée un fichier alias sur le disque à un emplacement spécifié avec les données de signet spécifiées en utilisant swift3.

Exemple de code:

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{}

Vous voulez -[NSFileManager linkItemAtPath:toPath:error:]. Afik, et ses méthodes connexes sont les moyens préférés dans toutes les versions.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top