使用 applescript 列出子文件夹

发布于 2024-10-29 13:46:16 字数 466 浏览 4 评论 0原文

这是我的第一个苹果脚本。我以为我会做一些简单的事情,比如使用路径导航到文件夹并列出子文件夹...不幸的是,我无法弄清楚:-)

这是我迄今为止尝试过的:

第一次尝试:

tell application "Finder"
    set the_folder to POSIX path of "Users:MyName:Doc"
    log the_folder
    set folder_list to every item of folder the_folder
    log folder_list 
end tell

这个产生错误: “Finder 出现错误:无法获取文件夹“/Users/MyName/Doc”。

有人可以吗: 1. 向我解释我做错了什么。 2. 提供一个可行的例子。

提前致谢。

顺便说一句,该文件夹确实存在于我的机器上......

This is my first applescript. I thought I'd do something simple like navigating to a folder using a path and listing the subfolders...Unfortunately, I can't figure it out :-)

Here is what I've tried so far:

The first try:

tell application "Finder"
    set the_folder to POSIX path of "Users:MyName:Doc"
    log the_folder
    set folder_list to every item of folder the_folder
    log folder_list 
end tell

This produces an error:
"Finder got an error: Can't get folder "/Users/MyName/Doc".

Could someone please:
1. Explain to me what I'm doing wrong.
2. Provide an example that works.

Thanks in advance.

btw the folder does exist on my machine...

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

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

发布评论

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

评论(2

故事与诗 2024-11-05 13:46:16

更新:哎呀!看来我给你的信息是错误的,所以我会给你正确的信息。

命令 POSIX path of 需要完整的别名引用。我的意思是提供完整的文件引用(即 :Users::somefolder:)。如果您引用的是文件夹,请确保以冒号结束引用(即 Macintosh HD:Users:)。改进的版本如下所示:

tell application "Finder"
  set the_folder to (POSIX path of ("<your_disk_name>:Users:<your_user_name>:Doc:") as alias) as alias
  set folder_list to every item of the_folder
end tell

可选

POSIX 路径(即 /Users//somefolder)强制返回 别名 >,需要两次转换。

转换 1:第一步是将引用转换为文件引用。为此,请将单词 as POSIX file 放在引用后面,如下所示:

"/Users/<your_user_name>/somefolder" as POSIX file

此代码生成以下形式的文件引用: file ":Users::som​​efolder:"

转换 2:在引用末尾添加第二个强制转换,as alias...

"/Users/<your_user_name>/somefolder" as POSIX file as alias

此代码生成一个实际的别名引用: alias ":Users::somefolder:

如果您有任何疑问,请询问。:)

UPDATE: Oops! It appears that I have given you the wrong information so I will give you the correct information.

The command POSIX path of requires a complete alias reference. By that I mean supplying the full file reference (i.e. <your_disk_name>:Users:<your_user_name>:somefolder:). Make sure that if you're referring to a folder that you end the reference with a colon (i.e. Macintosh HD:Users:). An improved version would look like this:

tell application "Finder"
  set the_folder to (POSIX path of ("<your_disk_name>:Users:<your_user_name>:Doc:") as alias) as alias
  set folder_list to every item of the_folder
end tell

OPTIONAL

To coerce a POSIX path (i.e. /Users/<your_user_name>/somefolder) back into an alias, two conversions are needed.

Conversion 1: The first step is to convert the reference into a file reference. To do this, place the words as POSIX file after the reference, like so:

"/Users/<your_user_name>/somefolder" as POSIX file

This code procudes a file reference in this form: file "<your_disk_name>:Users:<your_user_name>:somefolder:"

Conversion 2: Add a second coercion, as alias, to the end of the reference...

"/Users/<your_user_name>/somefolder" as POSIX file as alias

This code produces an actual alias reference: alias "<your_disk_name>:Users:<your_user_name>:somefolder:

If you have any questions, just ask. :)

稚气少女 2024-11-05 13:46:16

Posix 路径是您在命令行中使用的路径,并以“/”分隔。 Applescript 路径以“:”分隔,因此只需使用它们即可。尝试这个脚本看看路径应该是什么样子......

set folderPath to (choose folder) as text

Posix paths are paths you use at the command line and are "/" delimited. Applescript paths are ":" separated so just use those. Try this script to see what the path should look like...

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