How to: retrieve full path of file alias

Goal:
Given file alias: retrieve full path without filename.

Searched through dozens of posts, got plenty of code suggestions, but all end in errors.

Here’s one of the many things I’ve tried:

set tsvFile to (choose file)
set parent to (path to tsvFile as text)

And here’s the error I get
“Can’t make alias … into type constant”

Any help is appreciated.

set tsvFile to (choose file)

tsvFile as string -- HFS

POSIX path of (tsvFile as string) -- POSIX

tell application "System Events"

container of tsvFile -- enclosing folder

end tell

I couldn’t figure out how to coerce your ‘System Events’ example result into a string.

Did a further search using keyword ‘container of’ and found this:

tell application "Finder" to set containingFolder to container ¬
	of tsvFile as string

I’m able to use the result to build file paths with ‘containingFolder’ as base dir.

1 Like

A comment and an alternative…

You can actually coerce the result of the choose file command in one line. You could also coerce it to a file specification with as «class furl», or to a posix path as seen further down.

set tsvFile to (choose file) as text

I wouldn’t recommend this method unless you plan on using the result with a shell script but you can also use dirname:

do shell script "dirname " & quoted form of POSIX path of (choose file)
2 Likes

Thanks, good to know. But, this messes up my subsequent code to get the container’s path.

I’m sure there’s a simple workaround, but I’m getting buried in minutae :exploding_head:, and will stick with my current solution.

1 Like