Symlink creation in the Finder AppleScript

This script can be run from the script menuextra, or also as an app or droplet if saved as a app. Has not been tested for use beyond OS X 10.6.8 (not sure which useless, silly nickname this version has – still cats at this point, though – I’ve never seen the point in memorizing that garbage).

What it does is ask for files, or as an app use files dropped onto it, as sources to create symbolic link targets at a folder of the users choosing.

Including a non-generic (or less generic) icon since it’s nice to have one when these sorts of droplets are placed in the toolbar of Finder windows. To place in the app package, right click (control click) on the application in the Finder and select “Show package contents” from the pop-up menu. In that window navigate to Contents:Resources, duplicate the original icon file (droplet.icns), and then place the unzipped version of the below icon file into the Resources folder.

icon image

droplet.icns
FYI, I did not do a 512 pixel version. [UPDATE] New sexier icon, created with Affinity Photo (and Icon Composer of course).
[UPDATE] New sexier script. Will either prompt for source files or run on items selected in the Finder. Also logs symlinks created.

(*
http://strawhousepig.net/

Logs date, time, and path of symlinks created into ~/Library/Logs/symlinker.log
Not that I've ever hit a return button without being certain of where I was at.
This log does not have a rotation mechanism. Could be added to the periodicals, though.

There is a 'behavior' setting below.

It should be noted that dropping a large amount of files and attempting to link into their
source directory will result in an equally large amount of error dialogs unless user cancels.
You could move the target check to before the repeat, but then you need another
repeat that does the same as in the repeat already here, so *meh*.

Modified from- https://www.macworld.com/article/1058177/symboliclinks.html

*)
-- This setting decides the initial behavior of this script. Set to either "auto" or "prompt"
-- auto - One less step, but your Finder skills need to be up top. Runs immediately on items selected in the Finder where ever it has focus. It is also the only way this script (in its current form) will symlink a directory.
-- prompt - Safer due to having the extra step of choosing in script which files to run on.

property symlinker_behavior : "auto"

on run
  if symlinker_behavior is "prompt" then
    tell application "Finder"
      try
        set cwd to folder of window 1
      on error
        set cwd to home
      end try
    end tell
    open {choose file with prompt ¬
      "Choose a source file to create a symbolic link:" default location cwd as alias with invisibles and multiple selections allowed}
  else if symlinker_behavior is "auto" then
    tell application "Finder" to set _files to (selection as list)
    open (_files)
  else
    display alert "Erroneous setting in main.scrpt." as warning
  end if
end run
on open the_files
  set dest_dir to choose folder with prompt "Select a target directory." with invisibles
  repeat with source in the_files
    try
      set source_path to POSIX path of (source as alias)
      set target_path to POSIX path of dest_dir & name of (info for (source as alias))
      if target_path is not source_path then
        if source_path ends with "/" then set source_path to text 1 thru -2 of source_path
        do shell script "ln -s " & quoted form of source_path & " " & quoted form of target_path
        do shell script "echo `date "+%Y-%m-%d %H:%M:%S"`' " & target_path & "' >> ~/Library/Logs/symlinker.log"
      else
        display dialog "Cannot place links to source in source directory." with icon 0
      end if
    on error theErr
      if theErr is not "User canceled." then display dialog theErr
    end try
  end repeat
end open

Leave a Reply

Your email address will not be published. Required fields are marked *