MacOS – Quick way to create a symlink

findermacos

Dragging a folder in Finder while pressing Command + Option ( + ) creates an alias. But is there a quick and similar way to create a symlink this way?

Best Answer

macOS does not have a native drag and drop method to create a symlink in the same manner as creating an alias in Finder. By default, symlinks are created by using ln from the command line in Terminal.


In Terminal:

$ ln
usage: ln [-Ffhinsv] source_file [target_file]
       ln [-Ffhinsv] source_file ... target_dir
       link source_file target_file
$

For additional details, use: man ln


For a homegrown solution using Automator and AppleScript, the following is a bare-bones example of an Automator Service (QuickAction in macOS Mojave) that when assigned a keyboard shortcut, e.g. ⌘S in System Preferences > Keyboard > Shortcuts > Services, will create a symlink of the selected items in Finder at the selected destination folder that is brought up by pressing e.g. ⌘S:

This Service (QuickAction) will be available on the Services menu in Finder or from right-click Context menu, and or the assigned keyboard shortcut once an item or items are selected in Finder.

Bare-bones example AppleScript code:

on run {input, parameters}

    if input is equal to {} then return

    activate

    set posixPath to POSIX path of (choose folder with prompt ¬
        "Select destination folder for Symlink..." default location ¬
        (path to desktop folder) with invisibles)

    repeat with thisItem in input
        set thisItem to POSIX path of (thisItem as alias)
        try
            do shell script "ln -s " & quoted form of thisItem & ¬
                space & quoted form of posixPath
        end try
    end repeat

end run

The example AppleScript code assumes you have write privileges at the selected destination folder, other then the selected items source folder, and as coded only creates the symlink if it doesn't already exist. Changes can be made to the code to accommodate other options.

Make Symbolic Link Automator Service


Note: The example AppleScript code is just that and, other then a single try statement, does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.