将列表设置为 AppleScript 中的 shell 脚本输出

发布于 2024-09-06 15:59:17 字数 729 浏览 17 评论 0原文

我有一个输出文件名的 shell 脚本(每行一个)。我想将该输出放入 AppleScript 中的列表中。

我该怎么做?

关于如何将这些文件名字符串转换为文件对象的奖励积分。

编辑:

尝试此操作时:

set theFiles to {}
repeat with i from 1 to count of filenames
    set end of theFiles to (POSIX file (item i of filenames))
end repeat

我得到以下信息:

error "Finder got an error: Can’t get POSIX file \"/path/to/file\"." number -1728 from file "Macintosh HD:path:to:file"

编辑2:

事实证明查找器不知道“tell”语句开始后创建的文件。如何更新它或让它知道新文件?

EDIT-3:

这有效(注意添加“我的”):

set theFiles to {}
repeat with i from 1 to count of filenames
    set end of theFiles to (my POSIX file (item i of filenames))
end repeat

I have a shell script that outputs filenames (one per line). I want to put that output into a list in AppleScript.

How can I do this?

Bonus points for how to then turn those filename strings into file objects.

EDIT:

When trying this:

set theFiles to {}
repeat with i from 1 to count of filenames
    set end of theFiles to (POSIX file (item i of filenames))
end repeat

I get this:

error "Finder got an error: Can’t get POSIX file \"/path/to/file\"." number -1728 from file "Macintosh HD:path:to:file"

EDIT-2:

Turns out finder isn't aware of a file that gets created after the "tell" statement starts. How do I update it or make it aware of the new file?

EDIT-3:

This worked (note the addition of "my"):

set theFiles to {}
repeat with i from 1 to count of filenames
    set end of theFiles to (my POSIX file (item i of filenames))
end repeat

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

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

发布评论

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

评论(1

温柔少女心 2024-09-13 15:59:17
set myFilenamesList to paragraphs of (do shell script "path/to/shell/script")
set firstFileObject to POSIX File (item 1 of myFilenamesList)

当您有一个列表时,您可以使用重复循环来迭代该列表并对列表中的项目执行某些操作。例如,如果您想要文件对象的列表,您可以这样做。

set fileObjectsList to {}
repeat with i from 1 to count of myFilenamesList
set end of fileObjectsList to POSIX File (item i of myFilenamesList)
end
return fileObjectsList

当然,这样做没有多大意义,因为一旦您在列表中拥有文件对象,那么您将需要重复该列表以对这些对象执行某些操作...因此您将重复列表2 次,1 次可能就足够了。所以我会做这样的事情......

repeat with i from 1 to count of myFilenamesList
set thisFileObject to POSIX File (item i of myFilenamesList)
-- do something with the file object "thisFileObject"
end
set myFilenamesList to paragraphs of (do shell script "path/to/shell/script")
set firstFileObject to POSIX File (item 1 of myFilenamesList)

When you have a list, you use a repeat loop to iterate over the list and do something with the items in the list. For example if you wanted a list of the file objects you could do this.

set fileObjectsList to {}
repeat with i from 1 to count of myFilenamesList
set end of fileObjectsList to POSIX File (item i of myFilenamesList)
end
return fileObjectsList

Of course it doesn't make much sense to do this because once you have the file objects in a list then you'll need to repeat over that list to do something with those objects... thus you'll be repeating over a list 2 times when 1 time would probably suffice. So I would do something like this instead...

repeat with i from 1 to count of myFilenamesList
set thisFileObject to POSIX File (item i of myFilenamesList)
-- do something with the file object "thisFileObject"
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文