Tuesday, December 12, 2017

#1 2017-04-05 01:02:30 am

JayBrown
Member
Registered: 2016-02-22
Posts: 4

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.)

Last edited by JayBrown (2017-04-05 01:04:19 am)


Filed under: Finder, alias

Offline

 

#2 2017-04-05 03:07:24 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11482
Website

Re: Resolve original path of Alias file without Finder

Hi,

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

Applescript:

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


regards

Stefan

Offline

 

#3 2017-04-05 07:42:44 am

JayBrown
Member
Registered: 2016-02-22
Posts: 4

Re: Resolve original path of Alias file without Finder

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

Last edited by JayBrown (2017-04-05 07:43:13 am)

Offline

 

#4 2017-08-13 04:26:13 pm

JayBrown
Member
Registered: 2016-02-22
Posts: 4

Re: Resolve original path of Alias file without Finder

@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.

Last edited by JayBrown (2017-08-15 09:46:17 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)