터미널에서 폴더로 바탕 화면 바로 가기 / 별칭을 만들 수 있습니까? 싶습니다. 주어진 경로에

에 깊이 묻혀있는 특정 폴더에 대한 바탕 화면 바로 가기를 만들고 싶습니다 ~/Library/. Lion은 도서관이 기본적으로 숨겨져 있으며 여러 가지 이유로 보관하고 싶습니다. 주어진 경로에 대한 바탕 화면 바로 가기를 만들기 위해 사용할 수있는 한 단계의 명령 줄 작업이 있습니까? 라이브러리 숨기기를 해제하고 Finder를 사용하여 별칭을 만든 다음 다시 숨기는 솔루션을 피하고 싶습니다. 나는 그것을하는 방법을 알고 있지만, 내 목적을 위해 터미널에 붙여 넣을 수있는 단일 행이 바람직합니다.



답변

터미널에서 이것을 시도하십시오 :

cd ~/Desktop
ln -s ~/Library/path/to/folder

답변

한 줄의 터미널에서 할 수 있습니다. “/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'

당신은 교체해야 to fileto folder폴더를해야합니다.

별명을 작성하기 위해 파일 또는 폴더 경로를 전달할 수있는 쉘 스크립트는 다음과 같습니다.

#!/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

이 스크립트의 이름을 경우 make-alias.sh, chmod u+x make-alias.sh그것을 넣어 /usr/local/bin, 당신은 예를 실행할 수 있습니다 make-alias.sh ~/Library/Preferences.


답변

특정 폴더에서 링크를 대상으로 지정하거나 특정 이름을 지정해야하는 경우 다음 set name of result to "…"과 같이 사용할 수 있습니다 .

#!/bin/bash

if [[ $# -ne 2 ]]; then
    echo "mkalias: specify 'from' and 'to' paths" >&2
    exit 1
fi

from="$(realpath $1)"
todir="$(dirname $(realpath $2))"
toname="$(basename $(realpath $2))"
if [[ -f "$from" ]]; then
    type="file"
elif [[ -d "$from" ]]; then
    type="folder"
else
    echo "mkalias: invalid path or unsupported type: '$from'" >&2
    exit 1
fi

osascript <<EOF
tell application "Finder"
   make new alias to $type (posix file "$from") at (posix file "$todir")
   set name of result to "$toname"
end tell
EOF

답변

#!/bin/bash

get_abs() {
  # $1 : relative filename
  echo "$(cd "$(dirname "$1")" && pwd)/$(basename "$1")"
}


if [[ $# -ne 2 ]]; then
    echo "mkalias: specify 'from' and 'to' paths" >&2
    exit 1
fi

from=$(eval get_abs $1)  
todir=$(dirname $(eval get_abs $2))
toname=$(basename $(eval get_abs $2))
if [[ -f "$from" ]]; then
    type="file"
elif [[ -d "$from" ]]; then
    type="folder"
else
    echo "mkalias: invalid path or unsupported type: '$from'" >&2
    exit 1
fi

osascript <<EOF
tell application "Finder"
   make new alias to $type (posix file "$from") at (posix file "$todir")
   set name of result to "$toname"
end tell
EOF

답변

파이썬 솔루션을 원하는 사람들을 위해 애플 스크립트를 래핑 한 다음 subprocess.call을 호출하는 함수가 있습니다.

def applescript_finder_alias(theFrom, theTo):
    """
    (theFrom, theTo)
    create a short/alias
    theFrom, theTo: relative or abs path, both folder or both file
    """
    # /apple/51709
    applescript = '''
    tell application "Finder"
       make new alias to %(theType)s (posix file "%(theFrom)s") at (posix file "%(todir)s")
       set name of result to "%(toname)s"
    end tell
    '''
    def myesp(cmdString):
        import os, inspect, tempfile, subprocess
        caller = inspect.currentframe().f_back
        cmd =  cmdString % caller.f_locals

        fd, path = tempfile.mkstemp(suffix='.applescript')
        try:
            with os.fdopen(fd, 'w') as tmp:
                tmp.write(cmd.replace('"','\"').replace("'","\'")+'\n\n')
            subprocess.call('osascript ' + path, shell=True, executable="/bin/bash")
        finally:
            os.remove(path)
        return None
    import os
    theFrom = os.path.abspath(theFrom)
    theTo = os.path.abspath(theTo)
    if os.path.isfile(theFrom): 
        theType = 'file'
    else:
        theType = 'folder'
    todir = os.path.dirname(theTo)
    toname = os.path.basename(theTo)
    myesp(applescript)