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"
(*
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PURPOSE/METHOD:
• Determine if Finder Item Is an Alias or SymLink
• Gets Item from either KM Variable
OR
• 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)
REQUIRED:
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
https://forum.keyboardmaestro.com/t/script-how-to-determine-if-finder-item-is-alias-or-symlink/13289/3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*)
try
--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- CHECK FOR DATA FOM KM ---
### 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
else -- USE FINDER SELECTION
tell application "Finder"
set itemList to get selection
try
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 = "com.apple.alias-file") 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
-->folder
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 ~~~
DOWNLOAD:
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.