marc-medley
marc-medley

Reputation: 9832

How to get original path when alias fails to resolve

How can the original target path be programatically retrieved when the alias fails to resolve?

do {
    let resolutionOptions: URL.BookmarkResolutionOptions = [
        .withoutUI, .withoutMounting
    ]
    let _ = try URL(resolvingAliasFileAt: fileURL, options: resolutionOptions)
}
catch _ {
    // since non-resolvable, then retrieve & print original target string 
}

The existing StackOverflow question "Getting alias path of file in swift" does not cover original target path retrieval for the situation of a non-resolvable alias.

The information would seem to be available somehow because the Finder GUI Get Info will still show the Original: /Some/Path even if the original is not found or available.

Also, mdls metadata listing did not provide the original target path.

Upvotes: 2

Views: 157

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90521

I think you can load the bookmark data using URL.bookmarkData(withContentsOf:), then use resourceValues(forKeys:fromBookmarkData:) with [.pathKey] as the keys. Then, query the path of the returned URLResourcesKey object.

Upvotes: 2

Related Questions