Interpolate into an AppleScript called through Cmd

The background: the NativeFileDialog.jl unfortunately makes problems on MacOS 15 (i.e. current version). I’m trying to put together a workaround.

This one actually works:

cmd1 = `osascript -e 'POSIX path of (choose file with prompt "Pick a file:")' 2\> /dev/null`; 
# sending log noise to /dev/null

r1 = readchomp(cmd1) ;

(on log noise on MacOS 15 )

This one works too:

cmd3 = `osascript -e 'set strPath to POSIX file "/Users/eben60/Downloads/2023/"
POSIX path of (choose file with prompt "Pick a file:" default location strPath)' 2\> /dev/null`

r3 = readchomp(cmd3) ;

Now, I’d like to replace the hardwired default location by a programmable one. Like this:

default = "Users/eben60/Downloads/2023/"

cmd5 = `osascript -e 'set strPath to POSIX file "$default"
POSIX path of (choose file with prompt "Pick a file:" default location strPath)' 2\> /dev/null`

r5 = read(cmd5, String) ;

That doesn’t work. $default" will not be expanded at this position. And none of the workarounds I could think of (e.g. putting the quotation marks into the default variable) worked. Any ideas?

Build the script as a string, then pass that string to osascript.

script = """ set strPath to POSIX file "$default"
POSIX path of (choose file with prompt "Pick a file:" default location strPath)
"""
read(`osascript -e $script`, String)
3 Likes