【问题标题】:Make alias script failing使别名脚本失败
【发布时间】:2016-07-24 03:17:39
【问题描述】:

这里很少使用 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

我已经尝试过使用和不使用“新”。我已经尝试在文件名前面使用“POSIX 文件”并在文件名后面使用“作为 POSIX 文件”作为强制。我尝试过使用“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" 确认目标路径不存在。

以防万一,我运行的是 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 单行,因此文件路径将是内联的,而不是变量。但是我首先转到命令行,然后转到脚本编辑器,因为我无法让它工作,并且调用此代码 sn-p 的每个方法都会产生相同的错误消息。所以希望当/如果我让脚本工作时,我将能够从 Python 的 osascript 调用等效代码。 :}

【问题讨论】:

    标签: macos path applescript alias


    【解决方案1】:

    AppleScript represents file paths differently than POSIX does 以来,您使用POSIX file 肯定走在正确的轨道上(本质上是冒号而不是正斜杠)。

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

    但是,POSIX file 的问题在于它返回文件引用而不是 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
    

    这可能有点啰嗦,但我希望它有所帮助!

    【讨论】:

    • @larryy 太好了!很高兴我能帮上忙。
    • 更正: 1. 冒号分隔的 HFS 路径是旧的 OSX 前遗留问题。 AS 支持向后兼容(因为 AS 开发人员很懒惰),并且还支持标准 POSIX 路径。 2. Finder 需要对象说明符(例如file "test.txt" of folder "Documents" of home),但在大多数用例中会接受HFS 路径字符串(Finder 也是旧的pre-OSX 后遗症)或@ 987654338@/POSIX file 说明符并转换它们; getset 是例外。 3. HFS 路径存在根本缺陷——它们无法区分同名卷——因此请始终尽可能使用 POSIX 路径。
    • 4.正如您所说,make 命令的at 参数需要对将在其中创建新对象的文件夹对象的引用。如果您希望为新别名对象指定一个特定名称,请使用with parameter。示例:tell application "Finder" to make new alias at (POSIX file "/Users/jsmith/Desktop") to (POSIX file "/Users/jsmith/Documents") with properties {name:"My Docs"}
    • 实际上,Finder 别名的正确 AppleScript 类是 alias file
    • @vadian:你说得对,应该是make new alias file at...。 (碰巧的是,Finder 对此类拼写错误非常宽容,并且愿意接受 alias 作为这种特定上下文中的同义词。OTOH,在其他情况下它不会将它们视为可互换的- 在这一点上,这种马虎抓到你 - 所以在所有场合都保持 100% 准确和 100% 准确仍然是一个非常好的习惯。)
    猜你喜欢
    • 2016-12-29
    • 2020-06-12
    • 1970-01-01
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多