SCRIPT: How To Determine If Finder Item is Alias or SymLink



Continuing the discussion from Open file in finder - filtering file type:

Since @peternlewis has said that the KM Action Get File Attribute action will use the target of the link when choosing an Alias or SymLink file, I have written this script to determine if the Finder's selection is either.

Script Returns: either “Link: Alias” OR “Link: SymLink” OR the file class.

UPDATED: 2019-03-22 13:58 GMT-5

Ver 3.0

  • Rewrote using System Events (thanks to @CJK)
  • Added Error Handler
    • Will now return an error message if no KM Variable is provided and no item is selected in Finder.

property ptyScriptName : "Determine if File is Alias or SymLink"
property ptyScriptVer : "3.0" --  Rewrite with SE and Err Handler
property ptyScriptDate : "2019-03-22"
property ptyScriptAuthor : "JMichaelTX"

  • Determine if Finder Item Is an Alias or SymLink
    • Gets Item from either KM Variable
    • Item selected in Finder (if NO data from KM)

RETURNS:  Item Kind
  • "Link: Alias"      -- IF item is an alias file
  • "Link: SymLink"    -- IF item is a SymLink file
  • Kind of Item        -- For all other items (files/folders)  

  1.  macOS 10.11.6+
  2.  Mac Applications
      • Keyboard Maestro 8.2+

  3.  Keyboard Maestro Variables
      • GET (input) -- optionable
        •  Local__FilePath  -- Full POSIX Path of Item
REF:  The following were used in some way in the writing of this script.

  1.  2019-03-21, CJK, Keyboard Maestro Discourse
      SCRIPT: How To Determine If Finder Item is Alias or SymLink
  ### Requires Keyboard Maestro 8.0.3+ ###
  set kmInst to system attribute "KMINSTANCE"
  tell application "Keyboard Maestro Engine"
    set kmFilePath to getvariable "Local__FilePath" instance kmInst
  end tell
  if (kmFilePath ≠ "") then -- USE KM VARIABLE
    set filePath to kmFilePath
    tell application "Finder"
      set itemList to get selection
        set fileHFS to item 1 of itemList as text
      on error
        error "Please select an item in Finder"
      end try
    end tell -- Finder
    set filePath to POSIX path of fileHFS
  end if
  --- GET FILE TYPE ---
  tell application "System Events"
    tell the item named filePath
      if (its class = file) and (its kind = "Alias") then
        set fileType to its type identifier
        if (fileType = "") then
          set fileType to "Link: Alias"
        else if (fileType = "public.symlink") then
          set fileType to "Link: SymLink"
        end if
      else -- If NOT Alias or Symlink
        set fileType to its class
      end if
    end tell -- item named filePath
  end tell -- System Events
  set scriptResults to fileType
  --~~~~~~~~~~~~~ END TRY ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
on error errMsg number errNum
  if errNum = -128 then ## User Canceled
    set errMsg to "[USER_CANCELED]"
  end if
  set scriptResults to "[ERROR]" & return & errMsg & return & return ¬
    & "SCRIPT: " & ptyScriptName & "   Ver: " & ptyScriptVer & return ¬
    & "Error Number: " & errNum
end try
--~~~~~~~~~~~~~~~~END ON ERROR ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

return scriptResults
-->Link: Alias
-->Link: SymLink

You can put this script in a KM Execute an AppleScript action to return the results to a KM variable.

Here's a complete Macro that uses the script.

MACRO:   Determine if Finder Item is Alias or SymLink

~~~ VER: 1.0    2019-03-22 ~~~


Determine if Finder Item is Alias or SymLink.kmmacros (13 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.



Just posted an update.


Here's an alternative AppleScript that takes a more direct approach:

tell application "Finder" to tell (get its selection) to ¬
	if {} ≠ it then set f to its first item as text

set fp to f's POSIX path

tell application "System Events" to tell the item named fp
	if its class = file and its kind = "Alias" then ¬
		return its type identifier
	return its class
end tell

Output: Uniform type identifiers are used to report symlinks ("public.symlink") and alias files (""), and everything else is reported as a file or folder.

If one wishes to specify a file path rather than use Finder's selection, then the relevant half of the script is from where it's labelled --②, and simply needs the variable fp to be given a plain posix path, e.g. set fp to system attribute "KMVAR_filepath" or whatever.


Just made a major update that includes a macro.

Thanks for sharing your script which makes good use of System Events. I have made use of your technique in my revised script.

While I like a "more direct approach", I like to balance that with a script that is easy for all to read, understand, and modify if one sees fit. I also like to declare key variables so that they can be easily inspected in Script Debugger.