路径command; you tell it which folder you want, it returns an alias pointing to that folder.
And it will return an alias to the “Application Support” folder in your top-level Library folderAliases use HFS-style paths, so the result (
桌面, 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你可以通过
从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:
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唉,不相反,在上面的代码片段中,
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.