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.

2 Likes

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

thank you. it’s taken me quite a while to find this simple solution

This is simpler as it doesn’t require using both system events and the finder.

set tsvFile to (choose file)
tell application "System Events"
	set containingFolder to path of (container of tsvFile)
end tell

I’m not sure about the cause and effect of it but it seems that container of x returns a property rather than a file object, even though it sort looks like one. Furthermore, container has its own properties, one of which is path. So the above command gets the path property of the container property of the alias object.

If you wanted to use the Finder alone, then it was be even simpler.

set tsvFile to (choose file)
tell application "Finder"
	container of tsvFile as text
end tell

If you look at each of the scripts when compiled, you should see that the word ‘container’ in the Finder example is blue while in the System Events example it is purple. Blue seems to mean ‘application keyword’ while purple seems to mean ‘property’. This is likely why the two scenarios are handled differently.

Referring back to your initial post, ‘path to’ only works with a set of predetermined folders, applictions and scripts, resources. Since ‘tsvFile’ is not a constant, the command generated a ‘constant’ error. You can look up the details here in the language guide but some examples would be ‘desktop’ or ‘documents folder’. Also, you shouldn’t use the word ‘parent’ as a variable as it has a predetermined meaning. Note that its colour is purple and not green, like ‘tsvFile’ is.

1 Like

The correct and easy way was already answered by Mockman

set tsvFile to choose file

for hfs path, do this…

tsvfile as text

for posix path do this…

POSIX path of tsvFile

No need to use a tell block to involve Finder or System Events