Re: Replace : with / in Applescript
Re: Replace : with / in Applescript
- Subject: Re: Replace : with / in Applescript
- From: Christopher Nebel <email@hidden>
- Date: Tue, 15 Jul 2008 10:47:27 -0700
On Jul 15, 2008, at 9:00 AM, Jay Louvion wrote:
Applescript returns a path to a file to me in the following format:
HardDrive:Library:Directory1:Directory2: etc.
I need to convert the returned string into a more terminal friendly
string such as:
/Library/Directory1/Directory2/
Is there an easy way to eliminate the hard drive name from the
beginning of the string and replace the colons with forward slashes?
Well, I would go for the all-time classic:
If “HardDrive:Library:Directory1:Directory2:” is a variable called
the TheFolder for example, simply coerce using quoted form of POSIX
path of TheFolder
A few additional points:
1. The "quoted form of" part is only appropriate if you're going to
use the result to build up a "do shell script" command or something
similar. (See <http://developer.apple.com/technotes/tn2002/
tn2065.html>.) If you just want the path all by itself, omit it.
2. A point of terminology: this is not a "coercion" in the AppleScript
sense, it's a property access. (Two of them, if you count the "quoted
form" part.) "Coercion" may happen implicitly by using values with
certain operators, or explicitly by using the "as" operator. "of" is
neither of these.
3. If you want to go the other direction, use the "POSIX file" class,
for example:
POSIX file "/" --> file "HardDrive:"
4. What you're really dealing with here is converting HFS paths (colon-
style) to POSIX paths (slash-style). Despite appearances, this is NOT
as simple as replacing ":" with "/" and adding/removing the startup
disk name -- there's all kinds of weirdness involving file system
mount points you have to know in order to get the correct result, so
if you try to do it yourself by munging the strings, it's just going
to be wrong sometimes. Use "POSIX path" and "POSIX file": they're
simpler, and they know how to do the right thing.
--Chris Nebel
AppleScript Engineering
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden