2

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.

marc-medley
  • 8,931
  • 5
  • 60
  • 66

1 Answers1

2

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.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154