Substitute Original File for Alias in Finder Selection for Loop

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

2 Likes

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

--------------------------------------------------------
1 Like

:clap:t3::clap:t3::clap:t3: 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!!!

1 Like