Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

I'd like to create a desktop shortcut to a specific folder, buried deep within ~/Library/. Library is Hidden by default in Lion, and I'd like to keep it that way, for a variety of reasons. Is there a one-step, command line action I can use, to create a desktop shortcut to a given path? I'd like to avoid solutions that involve unhiding the Library, creating the Alias using Finder, and rehiding it. I know how to do that, but for my purposes, a single line that can be pasted into Terminal and be done with it would be preferable.

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Try this on Terminal:

cd ~/Desktop
ln -s ~/Library/path/to/folder
share|improve this answer
1  
I think you meant ln -s ~/Library/path/to/folder folder. One minor disadvantage to this method (ie symlinking) is that the link will be broken if the "original" (ie the target) is moved or renamed. –  Kelvin May 20 '12 at 2:32
    
The second argument folder is not required. If you omit it, ln creates a link named the same as the original folder. –  Will V May 20 '12 at 3:31
    
Ah, you're right. I got an error before, but I must've mistyped something. –  Kelvin May 20 '12 at 3:48
    
I know what it was - you can't have a trailing slash! –  Kelvin May 20 '12 at 3:50

It's possible to do it in one line of Terminal. Let's say you want to alias to the file "/Users/me/Library/Preferences/org.herf.Flux.plist".

osascript -e 'tell application "Finder"' -e 'make new alias to file (posix file "/Users/me/Library/Preferences/org.herf.Flux.plist") at desktop' -e 'end tell'

You should replace to file with to folder if you have a folder.

Here's a shell script that allows you pass in a file or folder path to create the alias:

#!/bin/bash

if [[ -f "$1" ]]; then
  type="file"
else
  if [[ -d "$1" ]]; then 
    type="folder"
  else
    echo "Invalid path or unsupported type"
    exit 1
  fi
fi

osascript <<END_SCRIPT
tell application "Finder"
   make new alias to $type (posix file "$1") at desktop
end tell
END_SCRIPT

If you name this script make-alias.sh, chmod u+x make-alias.sh and put it in /usr/local/bin, you can run e.g. make-alias.sh ~/Library/Preferences.

share|improve this answer
    
Will ~/Library/Preferences/org.herf.Flux.plist" work, or does the username need to be explicitly included in the Terminal command? –  LessPop_MoreFizz May 20 '12 at 1:51
    
I just tried using ~ and it doesn't work with the one-line osascript command. I suggest using the script file instead, because the ~ is converted automatically. –  Kelvin May 20 '12 at 1:56
    
Hmm. Seems to break on filenames with spaces such as /Library/Application Support/ –  LessPop_MoreFizz May 20 '12 at 2:13
    
If you're using the bash script, you should put the filename in single quotes if it has any spaces or special characters. However, this will prevent ~ from being expanded. The best thing is not to use quotes, and to tab complete the filename so bash will properly "escape" the special characters. E.g. type ~/Library/Application then press tab. If Application Support was the only match, the shell should have inserted a backslash before the space. You can also manually use a backslash to escape. –  Kelvin May 20 '12 at 2:26
    
Note that the spaces/special chars problem would exist in any solution - the shell can't know that you meant to pass 1 parameter rather than 2 separate ones. –  Kelvin May 20 '12 at 2:35

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.