Resolve original path of Alias file without Finder

This is a weird question. Resolving Alias files is pretty easy:

#!/bin/bash
for FILEPATH in “$@”
do
ORIGINAL=$(/usr/bin/osascript 2>/dev/null << EOF
tell application “Finder”
set thePath to (POSIX file “$FILEPATH”) as alias
set theOriginal to (POSIX path of ((original item of thePath) as text))
end tell
theOriginal
EOF)
if [[ -e $ORIGINAL ]] ; then
echo “${ORIGINAL%/}”
else
echo “alias: ${ORIGINAL%/}: No such file or folder.”
fi
done

However, this needs Finder to be running, which is the case for most people. But not for me. the problem is: I’m not using Finder; I have a third-party file manager, and I’ve disabled (i.e. actually quit) Finder completely. (Possible by editing some plist to add a “Quit” menu item to the Finder.)

So the weird question is, if there’s a way to use another scriptable macOS process to resolve Alias files in this way. (Tried with application “System Events”, but that’s not working.)

Hi,

I don’t know how far the AppleScriptObjc framework can be used in a bash script, this is the Cocoa way

use framework "Foundation"

set theAliasPath to "/Users/myUser/path/to/alias"
set theURL to current application's NSURL's fileURLWithPath: theAliasPath
set {originalURL, resolveError} to current application's NSURL's URLByResolvingAliasFileAtURL:theURL options:0 |error|:(reference)
if resolveError is missing value then
	set originalPath to originalURL's |path|() as text
else
	-- handle error
end if

This actually seems to works”thank you!”, in the Terminal & using a shell script, and with Finder disabled (quit). Didn’t even know that you could use AppleScriptObjC in this way. The only thing I needed to do was to add – ', to add an additional single quote, so the shell script doesn’t swallow itself.

#!/bin/bash

for FILEPATH in “$@”
do

ORIGINAL=$(/usr/bin/osascript 2>/dev/null << EOF
use framework “Foundation”

set theAliasPath to (POSIX path of “$FILEPATH”)
– ’
set theURL to current application’s NSURL’s fileURLWithPath:theAliasPath
set {originalURL, resolveError} to current application’s NSURL’s URLByResolvingAliasFileAtURL:theURL options:0 |error|:(reference)

if resolveError is missing value then
set originalPath to originalURL’s |path|() as text
else
set originalPath to “[original missing]”
end if
originalPath
EOF)

echo “$ORIGINAL”

done

@StefanK: I need to revive this topic, because I’d like to know if it’s possible to use osascript with Foundation to only print the path to the original file that’s embedded in the Alias, independent of whether that original file still exists or not, i.e. don’t go the route of resolveError. (If it’s not an alias file, it’s enough to just print nothing, but not mandatory.) The reason is that I want to do more stuff with the original file’s path in the shell script, e.g. print it, of course, but also extract the basename and use locate to determine, if the user moved it somewhere else, if it points to an unmounted volume etc.