Tools for Working with AppleScript

Hey Folks,

Now and then on the forum you've heard me refer to a relative alias in connection to AppleScript.

This is very much like a $HOME-based (or tilde-based) Unix path, except there are many more available anchor-points than just the user's home folder.

Here is a tool to create these for you if/when you need one or more for an AppleScript.

-Chris


PUTTING RELATIVE-ALIASES ON THE CLIPBOARD


Keyboard Maestro Macro:

Finder ⇢ Create Relative Alias(s) of Selected Item(s) v1.00.kmmacros (8.9 KB)


Compiled AppleScript:

Finder Selection – Create relative alias(s) and Place on the Clipboard.scpt.zip (14.1 KB)

------------------------------------------------------------------------------
# Auth: Christopher Stone <scriptmeister@thestoneforge.com>
# dCre: 2012/09/22 00:36
# dMod: 2014/09/23 21:40
# Appl: Finder
# Task: Create relative alias(es) to selected Finder item(s) and copy to the Clipboard.
#     : If nothing is selected then the insertion location is used.
# Deps: Uses only OSX components.
# Tags: @Applescript, @Finder, @Alias, @Path, @Reference, @Relative
------------------------------------------------------------------------------
--» PROPERTIES
------------------------------------------------------------------------------
property pathToSpec : {application support, applications folder, desktop, desktop pictures folder, documents folder, downloads folder, favorites folder, Folder Action scripts, fonts, help, home folder, internet plugins, keychain folder, library folder, modem scripts, movies folder, music folder, pictures folder, preferences, printer descriptions, public folder, scripting additions folder, scripts folder, shared documents, shared libraries, sites folder, startup disk, startup items, system folder, system preferences, temporary items, trash, users folder, utilities folder, workflows folder}
------------------------------------------------------------------------------
global expandedPathTo
------------------------------------------------------------------------------

try
   
   set clipboardList to {}
   set expandedPathTo to {}
   
   repeat with i in pathToSpec
      set end of expandedPathTo to path to (get contents of i) from user domain
   end repeat
   
   set AppleScript's text item delimiters to return
   set expandedPathTo to paragraphs of (expandedPathTo as text)
   
   tell application "Finder"
      set fileList to selection as alias list
      if length of fileList = 0 then
         set fileList to target of front window as alias as list
      end if
   end tell
   
   if fileList = {} then error "No items were selected in the Finder!"
   
   set AppleScript's text item delimiters to ":"
   
   repeat with i in fileList
      set end of clipboardList to makeRelativeAlias(i)
   end repeat
   
   set AppleScript's text item delimiters to return
   
   set the clipboard to (clipboardList as text)
   
on error e number n
   stdErr(e, n, true, true) of me
end try

------------------------------------------------------------------------------
--» HANLDERS
------------------------------------------------------------------------------
on makeRelativeAlias(_item)
   global expandedPathTo, pathToSpec
   set _item to _item as text
   set _len to (length of (text items of _item))
   repeat with i from _len to 1 by -1
      set _path to ((text items 1 thru i of _item) as text) & ":"
      if _path is in expandedPathTo then
         repeat with idx from 1 to (length of expandedPathTo)
            if (item idx of expandedPathTo) = _path then
               set refFldr to "path to " & (item idx of pathToSpec) as text
               set diskLocalRef to ("alias " & ("(" & "(" & refFldr & " as text) & ") & "\"" & (text items (i + 1) thru -1 of _item) as text) & "\")"
               try
                  run script diskLocalRef
               on error
                  set refFldr to refFldr & " from user domain"
                  set diskLocalRef to ("alias " & ("(" & "(" & refFldr & " as text) & ") & "\"" & (text items (i + 1) thru -1 of _item) as text) & "\")"
                  try
                     run script diskLocalRef
                  on error e
                     error e
                  end try
               end try
               
               return diskLocalRef
               
            end if
         end repeat
      end if
   end repeat
end makeRelativeAlias
------------------------------------------------------------------------------
on stdErr(e, n, beepFlag, ddFlag)
   set e to e & return & return & "Num: " & n
   if beepFlag = true then
      beep
   end if
   if ddFlag = true then
      tell application (path to frontmost application as text)
         set dDlg to display dialog e with title "ERROR!" buttons {"Cancel", "Copy", "OK"} default button "OK"
      end tell
      if button returned of dDlg = "Copy" then set the clipboard to e
   else
      return e
   end if
end stdErr
------------------------------------------------------------------------------
4 Likes