I need to convert a list containing POSIX paths into a list of HFS paths. Is there an Applescript command to do this?
I know there is a command to go the other way. And the “POSIX path of” command seems to leave off the device name (i.e., “Macintosh HD”), simply doing a search-and-replace of : for / or vice-versa, as needed. But I need the whole path.
Pass the POSIX path string to a ‘POSIX file’ specifier to get a file URL value, then coerce that to Unicode text:
set lst to {"/", "/usr/bin", "/Applications/TextEdit.app"}
repeat with pathRef in lst
set pathRef's contents to (POSIX file pathRef) as Unicode text
end repeat
lst --> {"Mac HD:", "Mac HD:usr:bin", "Mac HD:Applications:TextEdit.app"}
If you want a list of file URL values, delete the ‘as Unicode text’. If you want a list of alias values (assuming all paths exist), replace it with ‘as alias’.
‘POSIX path’ is a property of AppleScript’s alias, file URL and file specification types that returns the equivalent POSIX path . It also kinda works on strings, though it shouldn’t really and is liable to produce garbage for those (it certainly won’t turn a POSIX path string into anything good, as you’ve found).
Your script works just fine in AppleScript, but when I try to use it in an XCode droplet, it just fails, giving me error -1700 (and when I’m trying to modify something it goes to -1728)…
Is there a way to solve that?
on application_openFiles_(sender, fileList)
awakeFromNib()
-- Droplet Function (drag any file or folders on the Application's Icon to launch it
repeat with i from 1 to (the count (fileList as list))
try
set item i of fileList to (POSIX file (item i of fileList)) as alias
on error number errnum
display dialog errnum -- RETURNS -1700 error
end try
end repeat
on application_openFiles_(sender, fileList)
awakeFromNib()
repeat with i from 1 to (the count (fileList as list))
try
set item i of fileList to (((item i of fileList as text) as POSIX file) as alias)
on error number errnum
display dialog errnum
end try
end repeat
end application_openFiles_