对别名的疑问

发布于 2024-10-01 18:39:56 字数 533 浏览 4 评论 0原文

我在 applescript 文档中读到 - 'alias' 是一个关键字,表明在 编译(即检查语法)时,脚本应记住文件的 ID,并且在执行时不应向 Finder 询问定义路径指定位置处的文件,而是基于 ID。

所以我决定使用这个脚本来玩它 -

set samplePath to alias "Mac:Miraaj:eCBTMood:anim1.png"
tell application "Finder"
    open samplePath
end tell

我编译了这个脚本并将其保存为 AppleScript 应用程序。我通过双击它来执行它,它打开了正确的文件。然后我将文件名更改为anim2.png,并重新执行应用程序。我认为它应该再次打开相同的文件,但它给出了这个错误:

文件 Mac:Miraaj:eCBTMood:anim1.png 为 未找到

如果我做错了什么或错误地解释了“别名”,有人可以建议我吗?

谢谢,

米拉杰

I have read in an applescript document that- ‘alias’ is a keyword indicating that, after
compilation (i.e. checking of the syntax), the script should remember the ID of the file and, upon execution, should not ask the Finder for a file at the location as specified by the defined path, but based on the ID.

So I decided to play with it using this script-

set samplePath to alias "Mac:Miraaj:eCBTMood:anim1.png"
tell application "Finder"
    open samplePath
end tell

I compiled this script and saved it as an AppleScript application. I executed it by double clicking it and it opened the correct file. Then I changed the name of file to anim2.png, and re-executed the application. I thought it should again open the same file but it gave this error:

File Mac:Miraaj:eCBTMood:anim1.png was
not found

Can anyone suggest me if I have done anything wrong or interpreted 'alias' wrongly?

Thanks,

Miraaj

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

极度宠爱 2024-10-08 18:39:56

如果您将别名设置为属性而不是简单的变量,这将起作用。

我复制了您的脚本并复制了您注意到的行为:如果文件被移动或重命名,脚本将无法再找到它。

然后我将脚本修改为:

property samplePath : alias "Mimosa:Users:jerry:Desktop:Lost Cities"
tell application "Finder"
    open samplePath
end tell

现在,脚本应用程序能够在移动文件时找到该文件。当我重命名它时它能够找到该文件。

我认为,区别在于属性在保存时和实例之间保存在应用程序中,但每次都需要从原始路径重新创建简单变量,如果原始路径失败,则该变量将不起作用。

考虑这种变化:

set samplePath to alias "Mimosa:Users:jerry:Desktop:Lost Cities"

on idle
    global samplePath
    tell application "Finder"
        open samplePath
    end tell
    return 30
end idle

它的设置与您的类似,但脚本每三十秒重复一次(将其另存为“保持打开”以获得此行为)。如果关闭文件然后移动它,脚本将在下一次迭代时找到该文件。如果您重命名该文件,它同样会找到它。

这是因为脚本不必重新解释原始文件路径。它使用保存的别名,即使文件移动或重命名,该别名也会自行维护。

一旦它必须返回到您的脚本文本,它就会失败;即使有“属性”变体,如果我再次在 AppleScript 编辑器中打开脚本,链接也会被破坏,因为它会尝试重新解释原来的、现在不正确的路径。

This will work if you set up the alias as a property rather than as a simple variable.

I copied your script and duplicated the behavior you noticed: if the file is moved or renamed, the script can’t find it any more.

I then modified the script to be:

property samplePath : alias "Mimosa:Users:jerry:Desktop:Lost Cities"
tell application "Finder"
    open samplePath
end tell

Now, the script application was able to find the file on moving it. And it was able to find the file when I renamed it.

The difference, I think, is that properties are saved in applications on save and between instances, but simple variables need to be recreated each time from the original path, which won’t work if the original path fails.

Consider this variation:

set samplePath to alias "Mimosa:Users:jerry:Desktop:Lost Cities"

on idle
    global samplePath
    tell application "Finder"
        open samplePath
    end tell
    return 30
end idle

It’s set up like yours, but the script repeats every thirty seconds (save it as “Stay Open” to get this behavior). If you close the file and then move it, the script will find the file at the next iteration. It will likewise find it if you rename the file.

This is because the script doesn’t have to re-interpret the original file path. It’s using the saved alias, which maintains itself even if the file moves or is renamed.

As soon as it has to go back to your script text, it will fail; even with the “property” variation, the link will be broken if I open up the script in AppleScript Editor again, since it then tries to reinterpret the original, now incorrect, path.

江南月 2024-10-08 18:39:56

该文档是错误的,或者至少措辞不当。 Applescript 应用程序不会在运行时之间保存该信息;我不知道有任何应用程序会保存该信息,除非开发人员明确这样做。

如果该位置是硬编码的,并且在脚本调用别名时文件位于正确的位置,那么您可以将该别名移动到任何位置,这样就可以了。同时,重命名文件会导致后续操作失败,这就是为什么我认为文档措辞不佳的原因。最后,一旦你退出,所有这些信息都会消失。

我在脚本中一直使用 alias,但我从未见过它在运行时之间“自动”管理文件位置。我始终必须将文件放在脚本中指定的确切位置。

The documentation is wrong, or at least poorly worded. Applescript applications aren't going to save that information between runtimes; I don't know of any application that would save that information unless the developer explicitly does so.

If the location is hard coded and the file is in the correct location at the time the script calls for the alias then you can move that alias to any location and it will be fine. At the same time, renaming the file will cause subsequent operations to fail, and that's why I think the docs are poorly worded. In the end, once you quit, all of that information goes away.

I use alias all the time in my scripts, but I have never seen it "automagically" manage file locations between runtimes. I always must have files in the exact location as specified in the script.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文