Dealing with macOS aliases on the command-line

If you’ve been using a Mac for a while, you’ve probably come up against Aliases. They’re similar in many ways to the Unix Symbolic Link, but have their own set of advantages and disadvantages. The main advantage is you can move the original file to a new location on the same disk, and the alias will still find it. The main disadvantage (for me) is that the OS X Unix layer doesn’t know what to do with them.

We use aliases in Papers for various reasons, and I needed a way to handle them from the node.js world. For that reason, I wrote a small module called Alias Resolver. I recently updated the module to support the creation of aliases (mainly because it is useful for testing). As a side-effect of this, the module is now usable for creating and resolving aliases on the command-line.

To create an alias in the shell, you may have used some funky-looking AppleScript to tell the Finder to do it for you. Now, it’s much simpler (assuming you have Homebrew or Node.js), as Alias Resolver comes with the create-alias and resolve-alias commands.

  $ brew install node
  $ npm install -g alias-resolver
  $ create-alias /bin/ls /tmp/ls-alias
  $ file /tmp/ls-alias
  /tmp/ls-alias: MacOS Alias file
  $ cat /tmp/ls-alias
  bookmark88<'Atbinv8A    file:///
                                  Macintosh H(A&$FE16297F-ACA3-31D2-88EF-3B67691353C9/0dnib(Xx@h (      0 4"<%
  $ resolve-alias /tmp/ls-alias
  /bin/ls