Convert List of Aliases/Paths into POSIX Paths?

I have a list of paths to images that are being used in an HTML table that is AppleScript-generated. Of course all of the paths are colon-delimited and therefor do not work in HTML. So I need to change them to “/” delimited. I know how to do a single file but can’t figure out how to convert an entire list. Any thoughts?

Thanks.

Um, just loop through the list and convert the HFS paths to POSIX paths:

set the_list to {"path:to:file_a", "path:to:file_b", "path:to:file_c"}
repeat with i from 1 to (count the_list)
	set item i of the_list to (POSIX path of item i of the_list)
end repeat
return the_list
-->{"/path/to/file_a", "/path/to/file_b", "/path/to/file_c"}

Jon

of course. thanks so much jonn. i’ve never really had to work with POSIX paths until now and i tend to overcomplicate things when they are new to me. :slight_smile:

Sorry to bug you Jon since the code above certainly works, but when is it best to use the structure you’ve used above, and when is this the way to go (or does it matter)?:

repeat with anItem in the_list
set anItem to (POSIX path of anItem)
end repeat

NovaScotian, it depends on what you’re doing. Look at these examples:

set theList to {1, 2}
repeat with i from 1 to (count theList)
	set item i of theList to (item i of theList) + 1
end repeat
return theList
--> {2, 3}
set theList to {1, 2}
repeat with anItem in theList
	set anItem to anItem + 1
end repeat
return theList
--> {1, 2}

It matters because your code doesn’t actually change the items in the list (just the value of a temporary reference to the items):

set the_list to {"path:to:file_a", "path:to:file_b", "path:to:file_c"}
repeat with anItem in the_list
	set anItem to (POSIX path of anItem)
end repeat
return the_list
-->{"path:to:file_a", "path:to:file_b", "path:to:file_c"}

If you were doing something with each value like:

set the_image_string to ""
set the_list to {"path:to:file_a", "path:to:file_b", "path:to:file_c"}
repeat with anItem in the_list
	set anItem to (POSIX path of anItem)
	set the_image_string to the_image_string & "<img src=\"" & anItem & "\"><br>" & return
end repeat
return the_image_string
-->
(*
"<img src=\"/path/to/file_a\"><br>
<img src=\"/path/to/file_b\"><br>
<img src=\"/path/to/file_c\"><br>
"*)

then your code is fine.

Jon

Efficiency issues with long lists apart, it probably doesn’t matter too much which approach you take. However, the ‘reference’ method (like your above example) differs in one important respect from the ‘index’ method (used by jonn8 earlier).

Take, for example, this simple demonstration of the ‘index’ method - which behaves pretty much as most folks would expect:

set oldList to {"a", "b", "c"}
set newList to {}
repeat with n from 1 to count oldList
	set newList's end to oldList's item n
end repeat
newList contains "b"
--> true

At first glance, the alternative form doesn’t seem to behave correctly:

set oldList to {"a", "b", "c"}
set newList to {}
repeat with i in oldList
	set newList's end to i
end repeat
newList contains "b"
--> false

However, that’s because ‘newList’ in this case is a list of references that have not yet been evaluated:

set oldList to {"a", "b", "c"}
set newList to {}
repeat with i in oldList
	set newList's end to i
end repeat
newList
--> {item 1 of {"a", "b", "c"}, item 2 of {"a", "b", "c"}, item 3 of {"a", "b", "c"}}

One way to evaluate the references is to use the term ‘contents’:

set oldList to {"a", "b", "c"}
set newList to {}
repeat with i in oldList
	set newList's end to i
end repeat
newList's contents
--> {"a", "b", "c"}

So we should now be able to make the earlier example work as expected:

set oldList to {"a", "b", "c"}
set newList to {}
repeat with i in oldList
	set newList's end to i
end repeat
newList's contents contains "b"
--> true

The reason this difference isn’t always apparent is that certain operations, such as coercions and calculations, will automatically evaluate references to return the results:

set oldList to {1, 2, 3}
set newList to {}
repeat with i in oldList
	set newList's end to i * 2
end repeat
newList
--> {2, 4, 6}

This very helpful feature can sometimes lull the unsuspecting into a false sense of security, so that they always expect references to be automatically evaluated - even when no conversion operation is being performed.

Just a point to be aware of… :slight_smile:

I was one of the always folks. Glad I asked. Too much of what I know of AS is learned by observation of others and experiments but lacks an anchorage in fundamentals. Thanks all. I would have had to say:

repeat with anItem in the_list
set anItem to (POSIX path of contents of anItem)
end repeat

Don’t worry if that’s wrong - I’ll play with it.

No worries - that works fine because of the conversion to a POSIX path in each case - so the original reference is evaluated to accomplish that. Similarly, commands like ‘x as string’, etc. also work. It’s only on the rare occasion that the reference method might trip us up. However, to be forewarned and all that… :slight_smile: