0

I have a need to convert from a full path starting with /Users/username/file.ext to a tilde path ~/file.ext. I see lots of ways built into NSString and NSURL that go from tilde to full but not the other way around. While I could go the route of using NSHomeDirectory() and replacing within the path string, I was hoping for a way baked into Cocoa for handling this should things change.

The reason why I need this is because I am pushing Adobe JSX scripts from a Cocoa app. The Cocoa app gathers all the data needed for the script and pushes the data as arguments to the script. The File class appears to require tilde-based paths (but not the Folder class which is odd), but I cannot find anything definitive in their own documentation and am going by my own observations. Full paths fail while tilde paths do not.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Philip Regan
  • 5,005
  • 2
  • 25
  • 39

1 Answers1

0

I know this question is old, but here is the answer:

let path:NSString // a file path
let pathWithTilde = path.abbreviatingWithTildeInPath

https://developer.apple.com/documentation/foundation/nsstring/1407943-abbreviatingwithtildeinpath

This method is available since macOS 10.0.

mbt
  • 155
  • 10