Re: File path info
Re: File path info
- Subject: Re: File path info
- From: Richard 23 <email@hidden>
- Date: Tue, 2 Mar 2004 08:16:52 -0800
As you may recall, Duane Mitchell said something like:
>
I'm an AppleScript newbie and have a basic question on file specs.
>
>
I'd like to know how I can get the name of the folder that a file is
>
in from the file spec: alias "MacHD:Folder1:File".
ok
>
Also, if there is any other useful info that I could get from this
>
basic file spec that would be appreciated.
not a whole lot unless you use "info for" or the "Finder" depending
on what info you need. Let's focus on what can be done with a file
spec, alias or path string without outside assistance.
It's been awhile since I've posted but am assuming we still have a
problem with scripts containing special AppleScript characters.
So "==>" represents a continuation character (option-l).
Also you need to replace "tids" with "text item delimiters".
I got so tired of typing that term that I included the "tids"
synonym in the aete (dictionary) of a pseudo scripting addition
so I don't have to anymore.
While I was at it I defined other useful terms that would otherwise
appear as raw codes, like "text data" and "style data" for "ktxt"
and "ksty". But that's another story.
These handlers are written generally without making assumptions
about the path delimiter which is defined in "Path_Delim."
-- ---------------------------------------------------------
-- Preprocessed by Convert Script 1.0d5
-- ---------------------------------------------------------
property Path_Delim : ":"
-- -------------------------------------------------------------------
-- extracts file or folder name from path string or alias
-- -------------------------------------------------------------------
on GetName(thePath)
set {AppleScript's tids} to {{Path_Delim}}
set thePath to thePath as string
if result ends with Path_Delim then set ==>
thePath to result's text 1 thru -2
set {AppleScript's tids} to {{""}, thePath's last text item}
result's end
end GetName
-- -------------------------------------------------------------------
-- split path or alias to { parent folder path, file or folder name }
-- -------------------------------------------------------------------
on SplitPath(thePath)
set {AppleScript's tids} to {{Path_Delim}}
set thePath to thePath as string
if result ends with Path_Delim then set ==>
thePath to result's text 1 thru -2
tell thePath to set {AppleScript's tids} to {{""}, text 1 ==>
thru text item -2 & Path_Delim, last text item}
rest of result
end SplitPath
-- ---------------------------------------------------------
Both handlers ensure that the result includes the file or folder
name (rather than an empty string) by appending a path delimiter
if necessary and then processing the string as a folder path.
Coercion to string is performed in case an alias was passed.
I keep a variation of this in a folder of common handler libraries
and paste it into scripts all the time to avoid re-inventing the
wheel every time.
Now back to your original example.
>
I'd like to know how I can get the name of the folder that a
>
file is in from the file spec: alias "MacHD:Folder1:File".
assuming theFile contains your fss value,
set {folderPath, fileName} to SplitPath(theFile)
set folderName to GetName(folderPath)
A more specific handler for getting the folder name in one step
could easily be derived from the others if commonly required.
R23
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.