【问题标题】:How do I create a Macintosh Finder Alias From the Command Line?如何从命令行创建 Macintosh Finder 别名?
【发布时间】:2011-08-16 00:07:19
【问题描述】:

我正在尝试从命令行或 Python 程序创建 Finder Alias。我搜索了网络[1][2][3] 并找到了这个applescript:

$ osascript -e 'tell application "Finder" to make new alias at POSIX file "/Users/vy32/cyber.txt" to POSIX file "/Users/vy32/a/deeper/directory/cyber.txt"'

不幸的是,它给了我这个错误:

29:133: execution error: Finder got an error: AppleEvent handler failed. (-10000)

[1]http://hayne.net/MacDev/Bash/make_alias

[2]https://discussions.apple.com/thread/1041148?start=0&tstart=0

[3]http://hintsforums.macworld.com/showthread.php?t=27642

我做错了什么?

【问题讨论】:

标签: macos applescript


【解决方案1】:

关于您的消息,请尝试查看 Console.app。可能是源文件不存在。当我尝试使 Xcode 4.3+ 应用程序可见时,这对我很有帮助。我找到了工作代码:

$ osascript -e 'tell application "Finder" to make alias file to POSIX file "/file/to/make/link/from" at POSIX file "/folder/where/to/make/link"'

例如:

$ osascript -e 'tell application "Finder" to make alias file to POSIX file "/Applications/Xcode.app/Contents/Applications/OpenGL ES Performance Detective.app" at POSIX file "/Users/mylogin/Applications"'

【讨论】:

  • 这给出了@Leandros 指出的错误,他的解决方案确实有效...
  • @MarkSetchell 您能详细介绍一下您的环境吗?
  • 您好。我在 iMac 上运行 OSX El Capitan,我得到了 Leandros 指出的完全相同的错误。
  • 我明白了。我已经使用 osascript -e 'tell application "Finder" to make alias file to POSIX file "/Applications/Xcode.app/Contents/Applications/Instruments.app" at POSIX file "/Users/mylogin/Applications"'
【解决方案2】:

对于所有因AppleEvent handler failed 错误而苦苦挣扎的人:

make alias不像ln -s那样工作,你不必指定目标文件,你必须指定目标目录,文件名的别名是源文件/文件夹的名称。

例子:

osascript -e 'tell application "Finder" to make alias file to (POSIX file "/Applications/Mail.app") at (POSIX file "/Users/leandros/Desktop")'

【讨论】:

  • 更多说明:第一个参数是原始文件或文件夹。第二个参数是将创建别名的文件夹。该文件夹必须已经存在。你不能命名别名,它只会在第一个参数中给出文件或文件夹的名称(你可以在之后重命名它)。
【解决方案3】:

这种方法可以从命令行工作:

osascript -e 'tell application "Finder" to make alias file to alias "imac:Users:vy32:current:cyber.txt" at "imac:Users:vy32:foobar"'

foobar 是我的 homedir 中的一个目录。

【讨论】:

  • 其他示例对我不起作用,但是这个带有旧式“Mac OS”文件路径的示例可以。谢谢。
【解决方案4】:

Applescript 中的工作命令:

tell application "Finder"
    make new alias at POSIX file "/Path/to/location" to POSIX file "/Path/to/file.ext"
end tell

加盐调味。

【讨论】:

  • @Phillip Regan 它不需要实际的别名引用吗?
  • @fireshadow52:不。我抓取了一个随机文件的路径并使用了我的主文件夹(我只存储操作系统默认值),它就可以正常工作(没有双关语);别名出现在我指定的位置。
  • 我真的很希望能够从命令行执行此操作,但是当我将您的代码更改为 osascript -e 'tell application "Finder" to 后跟您的第二行时,它不起作用.. . ?
  • @vy32 你试过把"改成\"吗?
  • @fireshadow52," 位于 ' 内,因此它无需更改即可通过。
猜你喜欢
  • 2016-10-19
  • 1970-01-01
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 2015-12-22
  • 2011-04-22
  • 1970-01-01
  • 2017-09-05
相关资源
最近更新 更多