Me gustaría crear un acceso directo en el escritorio a una carpeta específica, enterrada en lo más profundo de ~/Library/
. La biblioteca está oculta de forma predeterminada en Lion, y me gustaría mantenerla así, por una variedad de razones. ¿Hay una acción de línea de comando de un solo paso que pueda usar para crear un acceso directo en el escritorio a una ruta determinada? Me gustaría evitar soluciones que impliquen mostrar la Biblioteca, crear el Alias usando Finder y volver a ocultarlo. Sé cómo hacerlo, pero para mis propósitos, sería preferible una sola línea que se pueda pegar en la Terminal y terminar con ella.
Prueba esto en la Terminal:
cd ~/Desktop
ln -s ~/Library/path/to/folder
Es posible hacerlo en una línea de Terminal. Digamos que desea crear un alias para el archivo "/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'
Debe reemplazar to file
con to folder
si tiene una carpeta.
Aquí hay un script de shell que le permite pasar una ruta de archivo o carpeta para crear el 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
Si nombra este script y lo coloca make-alias.sh
, puede ejecutar, por ejemplo, .chmod u+x make-alias.sh
/usr/local/bin
make-alias.sh ~/Library/Preferences
~/Library/Preferences/org.herf.Flux.plist"
Funcionará o el nombre de usuario debe incluirse explícitamente en el comando Terminal?~
y no funciona con el osascript
comando de una línea. Sugiero usar el archivo de script en su lugar, porque ~
se convierte automáticamente./Library/Application Support/
~
que se amplíe. Lo mejor es no usar comillas, y completar con tabulaciones el nombre del archivo para que bash "escape" correctamente los caracteres especiales. Por ejemplo, escriba ~/Library/Application
y luego presione tabulador. Si Application Support
era la única coincidencia, el shell debería haber insertado una barra invertida antes del espacio. También puede usar manualmente una barra invertida para escapar.osascript -e "tell app \"Finder\" to make new alias to (posix file \"$1\") as alias at desktop"
.En caso de que necesite dirigir el enlace a una carpeta específica (o darle un nombre específico), puede usar set name of result to "…"
como en
#!/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
Para las personas que deseen una solución de python, aquí hay una función que envuelve applescript y luego llama a 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
"""
# https://apple.stackexchange.com/questions/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)
Kelvin
ln -s ~/Library/path/to/folder folder
. Una desventaja menor de este método (es decir, el enlace simbólico) es que el enlace se romperá si el "original" (es decir, el objetivo) se mueve o cambia de nombre.Boj
folder
no es necesario. Si lo omite,ln
crea un enlace con el mismo nombre que la carpeta original.Kelvin
Kelvin
bfontaine
igal
motociclista