使别名脚本失败

Make alias script failing

这里的 AppleScript 用户很少,所以这可能是非常基础的东西,但我似乎无法创建一个非常简单的脚本来创建新的别名文件。这是完整的脚本:

set source_file to "/path/to/test.txt"
set alias_file to "/path/to/test.txt alias"
tell application "Finder" to make new alias at alias_file to source_file

我试过使用和不使用 "new"。我已经尝试过在文件名前面使用 "POSIX file" 并在文件名之后使用 "as POSIX file" 作为强制转换。我试过 "at * to *" 和 "to * at *"。以防万一目的地需要是我试过的包含文件夹。绝对所有变体都会产生相同的错误消息:

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

这并没有告诉我很多信息。

我在这里显然用“/path/to/”替换了实际的文件路径,但我可以保证ls /path/to/test.txt确认源路径有效,ls "/path/to/test.txt alias"确认目标路径不存在。

以防万一,我是 运行ning Mac OS X 10.11.5。确保的 Finder.sdef 条目看起来应该符合我的要求:

make v : Make a new element   make

new type : the class of the new element

at location specifier : the location at which to insert the element

[to specifier] : when creating an alias file, the original
  item to create an alias to or when creating a file viewer window,
  the target of the window

[with properties record] : the initial values for the properties of
  the element → specifier : to the new object(s)

我真正想做的是 运行 从命令行使用 osascript,我真正、真正想做的是从 Python 调用 osascript 单行程序,所以文件路径将是内联的,而不是变量。但我先转到命令行,然后转到脚本编辑器,因为我无法让它工作,并且调用此代码片段的每一种方法都会产生相同的错误消息。所以希望 when/if 我能得到一个可以工作的脚本,我将能够从 Python 调用来自 osascript 的等效代码。 :}

自从 AppleScript represents file paths differently than POSIX does(本质上是冒号而不是正斜杠)以来,您使用 POSIX file 绝对是正确的。

您可以手动将所有路径转换为 ​​AppleScript 路径,但我认为转换它们是更好的解决方案,以保持文件路径的可读性(并让您在阅读源代码时清楚地知道它们确实是文件路径)。

但是,POSIX file 的问题在于它 returns 是一个文件引用,而不是 make new alias 命令正在查找的文本路径。要解决此问题,您所要做的就是将返回的文件引用转换为 text 以使 alias 满意:

set source_file to (POSIX file "/path/to/test.txt") as text
set alias_file to (POSIX file "/path/to/test.txt alias") as text
tell application "Finder" to make new alias at alias_file to source_file

但是还有一个问题:make new alias at x to y 命令期望 y 是一个文件的路径,x 是一个目录的路径,别名应该是放置,但您正在为 xy 传递一个文件路径。目标 ("at") 路径应该只是 /path/to/make new alias 命令将自动命名别名 {original filename} alias。所以,总而言之:

set source_file to (POSIX file "/path/to/test.txt") as text
set alias_dir to (POSIX file "/path/to/") as text
tell application "Finder" to make new alias at alias_dir to source_file

说的可能有点啰嗦,希望对你有所帮助!