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
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)
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.