In article
<jpdjajadiningrat-E83F98.00142305012004 DeleteThis @dreader2.news.tiscali.nl>, Tom
Djajadiningrat <jpdjajadiningrat DeleteThis @yahoo.com> wrote:
> This is an embarrassingly simple question that I cannot solve
> nonetheless... :-(
>
> Say I've got a path to a file:
>
> HD:myFiles:theImportantFolder:theImportantFile
>
>
> How do I then get the path of the folder that contains that file? That
> is, how do I get:
>
> HD:myFiles:theImportantFolder
>
> I tried things like:
>
> set myFolderPath to the folder of myFilePath
>
> but that does not seem to work.
One reason that doesn't work is because myFilePath is a string, not a
file. Strings don't have containing folders. They're strings, after
all.
The other reason is that the term Finder uses for the folder that
contains a given file is "container," not "folder." (Finder is
probably the best application to ask questions like this, since it's
always running and knows lots about files.) In Finder, "folder" is a
class representing (no surprise here) folders and is used for child
folders of a given container -- "folders of desktop folder" would refer
to all the folders on your desktop, for example.
So what you want is something like this:
tell application "Finder" to get container of file myFilePath
By saying "file myFilePath" you are saying "the file whose path is the
string myFilePath" -- this is an object that can have a container, and
"container of" retrieves it. This is a folder object, not a string.
If you want it as a path, use something like "get (container of file
myFilePath) as string."
Of course, this won't work if the file doesn't exist. Files that don't
exist aren't in a folder and thus don't have a container. In that case
you would have to restort to string manipulation, and simply cut off
everything past the last colon. I'll leave that as an exercise.
--
Jerry Kindall, Seattle, WA <http://www.jerrykindall.com/>
Send only plain text messages under 32K to the Reply-To address.
This mailbox is filtered aggressively to thwart spam and viruses.<!-- ~MESSAGE_AFTER~ -->
>> Stay informed about: [newbie Q] how to get the path of the folder of a file?