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.
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');
})();
(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 : -)