开发者

Set list to shell script output in AppleScript

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 fr开发者_如何学编程om 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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜