Michael Cho
Michael Cho

Reputation: 746

Get Mac Applescript file object from POSIX path string

I have a list of files stored in a text file called tempfile.txt. eg the contents of the tempfile.txt looks like:

/path/to/my/file1.txt
/path/to/my/file2.txt
/path/to/my/file3.txt

I want to use Applescript to change the label color of these text files in Finder, but having trouble getting the actual file object (instead of the file name and path as a string). Here is what I have so far:

-- some work has already been done to set tempfile to tempfile.txt

set files_to_update to paragraphs of (read tempfile) -- this works fine
repeat with myfile in files_to_update
    set selected_file to POSIX path of myfile -- this works fine
    set label index of selected_file to 1 -- trying to set this file to orange fails with error "A property can't go after this identifier"
end repeat

Any help?

Upvotes: 1

Views: 6829

Answers (2)

Matt Strange
Matt Strange

Reputation: 21

The answer above is correct, but I was tripped up for a bit by something simple, so I thought I'd mention it:

When doing Unix-y things with the path, it's common to get the quoted form of the POSIX path, to avoid issues when there are spaces, etc. in the path. But as alias doesn't understand quoted form, and things go awry. (That is, an error is thrown.)

If you've got a quoted path, strip the quotes when venturing into the world of aliases like so:

set label index of ((text 2 thru -2 of f) as alias) to 1

Upvotes: -1

Lri
Lri

Reputation: 27603

set input to "/Users/username/Desktop/untitled folder/
/Users/username/Desktop/Untitled.txt"

repeat with f in paragraphs of input
    set f to POSIX file f
    tell application "Finder" to set label index of (f as alias) to 1
end repeat

Use POSIX file to get a file object for a text path. (POSIX path of is used to get the text path for a file or an alias.) label index is a property of Finder items.

Upvotes: 4

Related Questions