Using a For-Loop on the Finder selection I would like to identify a file A that is a link to an original file B, and substitute B for A in the for loop. Any ideas? What I really want to do is have a file attribute "original" such that when I have a file that is an alias (content kind = Alias) I can get an attribute "original" from the alias which is the name of the original file. I'm really surprised this attribute does not exist already.
Hey Ike,
Keyboard Maestro can detect whether a file is an alias, but as far as I know it cannot locate the original item of an alias.
Here’s an AppleScript that should do what you want.
--------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2016/0/29 05:38
# dMod: 2016/06/30 22:45
# Appl: Finder
# Task: Replace Alias Files with their original
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Replace, @Alias, @File, @Original
--------------------------------------------------------------------------------
try
tell application "Finder"
set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
repeat with theItem in finderSelectionList
if class of theItem = alias then
set parentFolder to parent of theItem as alias
with timeout of 3 seconds
set originalItemOfAlias to original item of theItem as alias
delete theItem
# Using a try-block on the off-chance the item is already in that folder.
try
move originalItemOfAlias to parentFolder
end try
end timeout
end if
end repeat
end tell
on error e number n
set e to e & return & return & "Num: " & n
if n ≠ -128 then
try
tell application (path to frontmost application as text) to set ddButton to button returned of ¬
(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
default button "OK" giving up after 30)
if ddButton = "Copy Error Message" then set the clipboard to e
end try
end if
end try
--------------------------------------------------------------------------------
-Chris
Thank you. I¹ll try this in the near future.
I¹ll ask Peter to see about adding a getter for the original to KM.
Ike Nassi
It’s possible, but given Apple’s ever reduced support for aliases, and the fact that resolving an alias can lock up the application for several minutes, I doubt direct support will ever happen.
I've updated the script above to (hopefully) prevent such a hang. Unfortunately I don't have a network handy, so I can't test to be certain.
I've also added some more error-checking.
-Chris
I'm never sure if it's okay to revive very old threads, so now is a good time to tell me if it's not so I know for future...
That said, I thought I'd try to figure out how to use the AppleScript @ccstone put forward here to replace an alias with its original file, but keep the alias name. A handy option if, like me you've categorised a lot of aliases and prefer the new naming system.
I got a bit stuck when trying to select the new file that replaced the alias in order to rename it. I managed to use yet another of @ccstone's macros to sort by date added to retrieve the path of the most recent file, which would logically be the one that just replaced the alias. Reveal file didn't work for some reason (doubtless user error) so I went with the ol' Go To Finder function.
Unfortunately, when I try this on a folder, the Finder switches to a list view of its contents, and the folder itself isn't available to be renamed. Close, but no cigar.
Replace alias with original (keep alias name).kmmacros (8.7 KB)
Hey Neil,
Sure it is – as long as new posts are genuinely relevant to that thread.
You don't need to select it – you just need to be able to reference it.
-Chris
--------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2016/0/29 05:38
# dMod: 2021/08/19 18:46
# Appl: Finder
# Task: Replace Alias Files with their original – Keeping the name of the Alias File.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Replace, @Alias, @File, @Original
# Vers: 1.00
--------------------------------------------------------
try
tell application "Finder"
set finderSelectionList to selection as alias list
if length of finderSelectionList = 0 then error "No files were selected in the Finder!"
repeat with theItem in finderSelectionList
if class of theItem = alias then
set nameOfTheItem to theItem's name
set parentFolder to parent of theItem as alias
with timeout of 3 seconds
set originalItemOfAlias to original item of theItem as alias
delete theItem
# Using a try-block on the off-chance the item is already in that folder.
try
set movedItemRef to move originalItemOfAlias to parentFolder
set name of movedItemRef to nameOfTheItem
end try
end timeout
end if
end repeat
end tell
on error e number n
set e to e & return & return & "Num: " & n
if n ≠ -128 then
try
tell application (path to frontmost application as text) to set ddButton to button returned of ¬
(display dialog e with title "ERROR!" buttons {"Copy Error Message", "Cancel", "OK"} ¬
default button "OK" giving up after 30)
if ddButton = "Copy Error Message" then set the clipboard to e
end try
end if
end try
--------------------------------------------------------
Now that is elegant. I honestly don't know how you do this sort of stuff. Years of practice, obviously! Worked perfectly straight away. Yet more hours of my life saved. Thankyou Chris!!!
Thanks for sharing this!
I have a few questions:
1 - When I was testing with a few text files, I noticed that they change to a blank icon so I can't preview them, even though I can open them with textEdit.
The alias:
The original file:
The end result:
2 - I noticed that this indeed replaces the alias by moving the original file, but that's not always desired. For example I may want to just replace the alias with the original file, but not necessarily move the original file from its original location, in case I have other aliases somewhere pointing to that file.
I was trying to change the AS from "move" to "copy" but when I do that, the word "copy" becomes black instead of blue, so I'm not sure if that's how it's done?
Also, I was thinking that the "delete theItem" section could be removed from the script and at the end we could have a Prompt asking if we want to Copy or Move the file:
Give this a little test on some non-crucial files:
Replace Alias with Original.kmmacros (36 KB)
It is designed to also work for multiple aliases at once.
It works, thanks.
A few things:
-
When I decide to keep the alias name, there's an issue, because an alias doesn't contain the extension, so when it copies, it becomes a file with no extension (so no QuickLook) like this:
-
I made some changes to avoid having 2 dialogs showing and since I will rarely want to delete the original and/or change the name of the alias, the defaults are set to keep the original and keep the original file name. Here it is:
===Replace Alias with Original Macro (v11.0.1)
Like a charm!
Thanks!
I just edited it to use a slightly more logical checkbox configuration.
Yeah, now each person can make those adjustments.
Since I rarely change the alias name, other than removing the word "alias" when it adds it, having both checkboxes unchecked makes more sense, even though having the first one checked wouldn't make a difference either