Welcome to MacForumz.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

[newbie Q] how to get the path of the folder of a file?

 
   Macintosh computer (Home) -> Apple Scripts RSS
Next:  Need Applescript to Modify Finder Flags  
Author Message
Tom Djajadiningrat

External


Since: Jan 05, 2004
Posts: 2



(Msg. 1) Posted: Mon Jan 05, 2004 3:14 am
Post subject: [newbie Q] how to get the path of the folder of a file?
Archived from groups: alt>comp>lang>applescript (more info?)

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.


Any help is greatly appreciated.


Tom

 >> Stay informed about: [newbie Q] how to get the path of the folder of a file? 
Back to top
Login to vote
Jerry Kindall

External


Since: Jul 09, 2003
Posts: 926



(Msg. 2) Posted: Mon Jan 05, 2004 3:14 am
Post subject: Re: [newbie Q] how to get the path of the folder of a file? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
newbie: path relative to the script - tell application "Finder" activate select file "myfile.pdf" of folder "test" of startup disk open selection end tell I am trying to change a full path to the document to a relative path to the document. The script and th...

getting a file name from a path name - Woe is me.... I would have thought I could at least do a simple script like this. I want to extract the file name from a list of files. However, I get an error and the script never goes to the handler. The error is "Can't continue processFiles&qu...

Folder Actions (newbie) - Hello all, Please, exuse the newbie question. I have a client that wants a 'watched' folder that will be used for FTP uploads. They want the contents of this folder (PDF files) to be automatically printed and moved to a 'Printed' Folder. I have..

fax scripting and attaching a file using a direct path - Hi all, i'm having issues attaching a faxable file to 4-sight. I'm pretty sure my issues involve the AddFile function. I can fax it directly when i use a this: on AddFile(m) <font color=purple> ; local f</font> <font color=purple&...

Relative file reference without full path? - Suppose I have an alias to a file, and a name of a file within that folder. Is there a way to get an alias to the file without using any full paths? (I want to avoid full paths so that the script won't break if there are two volumes of the same name,..
   Macintosh computer (Home) -> Apple Scripts All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]