1

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
1
  • That's a "Finder" error. The Finder does not know that command and thus it errors. Take that command out of your Finder tell block. It's a regular applescript command not a Finder command. Commented Jun 25, 2010 at 7:04

1 Answer 1

2
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
Sign up to request clarification or add additional context in comments.

1 Comment

How would I create file objects for all the filenames. I need it in a list object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.