返回的错误的文件夹路径的命令在系统事件告诉块

标准添加脚本定义了路径command; you tell it which folder you want, it returns an alias pointing to that folder.

例如,你可以说:

p1到路径设置为应用程序支持

And it will return an alias to the “Application Support” folder in your top-level Library folderAliases use HFS-style paths, so the result (p1在这种情况下)是这样的:

第谷高清:库:应用程序支持:

有几个打你可以通过特殊的文件夹名称path to,包括文档”文件夹,favorites文件夹,桌面, and so forthConsult the Standard Additions scripting dictionary for the full list.

Because of the layered system folder layout on Mac OS X, there are more than one instance of some of these foldersFor example, with the “Application Support” folder, there’s one in the top-level “Library” folder, and there’s another one inside the “Library” folder in each user’s home folder.

这些不同的层文件系统领域in Mac OS X’s parlance.1你可以通过path to命令一个parameter, which allows you to specify which domain you want正如上面,如果你省略了parameter, it defaults to the Local domain (i.ethe top-level “Library” folder shared by all users).

To get the current user’s “Application Support” folder, ask for the User domain:

p1到路径设置为应用程序支持从用户域

这将返回类似:

第谷高清:用户:格鲁伯:图书馆:应用程序支持:

所以,有鉴于此,猜猜这是什么回报:

tell application "System Events"
    set p2 to path to application support from user domain
end tell

The reasonable guess, of course, is that it would return the same thing唉,不相反,在上面的代码片段中,p2设置为:

第谷高清:库:应用程序支持:

即通过将路径命令在一个tell application "System Events"块,它不再荣誉从用户域参数,而always returns the folder from the Local domain

这是相当阴险,因为在一个tell application "System Events"block is exactly where you’re likely to want to get the path to a folder, because you can’t do much with an alias to a folder except within such a blockAppleScript, by itself, doesn’t let you do anything with a folder.

For example, let’s say you want to get a list of every item within the user’s “Application Support” folder你不能这样说:

set p to path to application support from user domain
set my_list to every item of p

因为你会得到一个错误每件部分You need to ask an application that knows how to do something with a folderThe Finder can do this, but the recommended way to script this is to use System Events instead但你不能说:

tell application "System Events"
    set p to path to application support from user domain
    set my_list to every item of p
end tell

因为一旦你移动路径command into the System Events tell block, you get the wrong answerInstead, you have to remember this, and write it like this, with the路径statement outside the tell block:

set p to path to application support from user domain
tell application "System Events"
    set my_list to every item of p
end tell

The Finder, however, does not suffer from the same bug as System Events.2这是预期:

tell application "Finder"
    set p to path to application support from user domain
    set my_list to every item of p
end tell

The reason to prefer System Events over Finder for this sort of thing is that, ostensibly, Finder is technically “optional” — users of third-party file managers like路径查找器might not be using the Finder然而,系统事件总是可用的。

This sort of bug is precisely why so many people despise AppleScript. Not only does it make no sense, but something like this can be maddeningly difficult to track down想象一下,如果你开始路径statement outside the System Events tell block, and everything is working as you expectThen, while cleaning up the code, you move the路径statement into the tell block, to keep it next to the other lines of code that pertain to the folder you’re getting the path to.

Now your script is broken — but you may not notice immediately, because it doesn’t break with a compilation or runtime errorInstead, just by moving the路径statement inside the System Events tell block, it silently starts returning a different (and wrong) “Application Support” folder.

如果你在家计分:雷达4255244


  1. Cf”唯一的Mac OS X扩展文件夹“关于Mac OS X的文件系统领域的更多细节。

  2. 我认为这是一个错误在系统事件If this isn’t a bug, but instead expected behavior, I’d love the explanation还需注意,这种行为在10.3.9和10.4.2相同。