Get "Original" Path Property of Selected File

Does anyone know how to obtain this path programatically?

CleanShot 2024-11-30 at 15.30.00@2x

I have thousands of aliases that point to files that no longer exist (they've been moved to a new drive), so I need to grab this path in order to recreate the alias with the updated original file location.

It's not as simple as creating new aliases from scratch, as they've been organised in a way that isn't reflected in the original file structure, and it would be a real headache to reorganise them from scratch.

I have three ideas.

OCR could read the path, but it wouldn't work very reliably, because of how spaces can't be detected at the end of each line.

AppleScript may have a property in the Finder app that allows you to get the "Original" property. This should be perfectly reliable.

The Find Image action could perform a "triple click" on the area above the "Select" button and then you could type Ctrl-C into the Finder app and you could thereby "get" the string. I think this should be reliable too.

1 Like

Thanks for the reply. UI interaction isn't really practical as there are thousands of aliases. I've been investigating the AppleScript route but so far no luck. Similarly, mdls seems to return everything except the original path.

Hello Neil (@noisneil):wave:

Here is an ASObjC snippet that normally should work and give you the original File‘s Path.


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

Greetings from Germany :de:

Tobias

1 Like

I'm reading up on the macOS command "mdfind" which may work too. But I'll leave it to you experts to investigate.

Here‘s another nifty thing written by Chris (@ccstone) in 2019

Greetings from Germany :de:

Tobias

Thanks for the suggestion. Unfortunately it doesn't return anything. Perhaps this returns the original file path when the original file exists, but since once doesn't in my case...

I've managed to get pretty close with this shell script that directly parses the alias binary content:

Local__AliasPath="$KMVAR_Local__AliasPath"; PATH_COMPONENTS=$(dd if="$Local__AliasPath" bs=1 2>/dev/null | strings -n 4 | grep -A10 "^Volumes$" | grep -v "^file:" | grep -v "^Macintosh" | grep -v "^dnib" | grep -v "^[0-9A-F]\{8\}-" | grep -E "/|^[A-Za-z]|^[0-9].*"); echo "Original path: /$(echo "$PATH_COMPONENTS" | tr '\n' '/' | sed 's/\/$//')"

However, it's very unreliable and, more often than not, will skip parts of the path. I feel like it must surely be possible to access the path, as it's visible in the info pane, but maybe there's more to it.

Hey Neil

I’m by far no expert … but maybe there is a bigger chance when trying to get the information out of the object via ASObjC … if it might ever be possible to…

But if your original Files are elsewhere you’re out of luck anyway…

Maybe you’ll get a better answer if you’re describing your problem on the LateNight Software Forum or on the MacScripter Forum.

Greetings from Germany :de:

Tobias

1 Like

Thanks. I think that's a good idea. :+1:t3:

Ok so I've got a solution for my particular problem:

As this is a new drive, with no other aliases depending on its drive name, I was able to rename it to the name of the drive the aliases were expecting, and they all work again. I'd love to be able to change the drive name to something more appropriate, but the fact that I have my instrument catalogue back in working order is a big relief.

Even though this crisis has been averted, I'm going to leave the thread open, as I'm still very interested to know if someone knows of a way to access that path, just for curiosity's sake.

Hey Neil

Glad that it’s working again for now …

If I were you I would ensure to build something based on that structure you have given the bits of code I shared above, which also includes a full reference to everything - then rebuild everything from scratch using automation with the Name of your likening for the drive.

If you do this you’ll have something that will be able to fix common problems related to Aliases and Symlinks by applying it all from scratch when any number of Aliases or Symlinks are not working.

I have a similar setup for my Aliases and Symlinks which for example gets checked automatically with a Macro once a week at night when I sleep.

It’s pretty darn useful to have something like this if you have to deal with thousands of these Aliases or Symlinks.

Greetings from Germany :de:

Tobias

1 Like

Your suggestion to try other forums may turn out to be a fruitful.

1 Like

You‘re welcome.

I’ll always try my very best to make good suggestions…

Have saved those suggestions from your other thread on MacScripter for my own reference… I’m so glad that I was able to help you out making that suggestion.

Greetings from Germany :de:

Tobias

1 Like

A very helpful soul over on macscripter.net has helped get me started.

For some reason I've been unable to get the alias replacement script itself to use Local__Alias instead of the Finder's selection, so that I can use the For Each action.

The macro below works for a single alias. If anyone knows why I can set the variable selectedAlias like this, please let me know.

“This_doesn't_work"
set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set selectedAlias to getvariable "Local__Alias" instance inst
end tell

Get Original Path from Alias.kmmacros (27 KB)

Macro screenshot