How Convert Full POSIX Path to a Tilde Path

Hey Folks,

AppleScriptObjC has a pretty convenient method for expanding a tilde-based path to a full POSIX Path:

set thePath to ((current application's NSString's stringWithString:thePath)'s stringByExpandingTildeInPath) as text

But is there a similar method for the reverse?



Yes, stringByAbbreviatingWithTildeInPath

set thePath_withTilde to thePath's stringByAbbreviatingWithTildeInPath()

Hey Pete,

Many thanks.

Your code doesn’t work on my Mojave system without building a construct like I have in post #1.

use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions

set thePath to "/Users/chris/Downloads/"
set thePath to ((current application's NSString's stringWithString:thePath)'s stringByAbbreviatingWithTildeInPath) as text


I’m assuming you’re presenting it out of context, and I’d be interested to see that working context.

And – thank you very much for providing a link to the Apple documentation. That’s very helpful.

Best Regards,

1 Like

Yes, of course. I‘ve made a NSString with a path on my machine. Didn’t include that line, probably to match the one liner in your first post :slight_smile:

1 Like

JXA, for what it’s worth:


or if you prefer:


Hey Rob,

Super. Thanks.

But let’s provide some runnable, testable code for people who don’t know what they’re looking at.

// JavaScript for Automation (JXA)
// Convert a full POSIX Path into a Tilde-Path.

(() => {

   let strPath = '/Users/Your_User_Name/test_directory/';
   let tildePath01 = ObjC.unwrap($(strPath).stringByAbbreviatingWithTildeInPath);
   let tildePath02 = $(strPath).stringByAbbreviatingWithTildeInPath.js;
   return [tildePath01,tildePath02].join('\n');




Looks like a marked spelling of And, and I agree.

(None of those names are variables in that code – the references are never mutated – so the default const probably serves better than let, which introduces the risk of redundant complexities, and is fractionally slower at run-time, in the rare contexts where these things matter)

I hesitated to elaborate the JS much on an AppleScript forum – thank you for taking that on : -)


See, for example, prefer-const - Rules - ESLint - Pluggable JavaScript linter

1 Like