Re: Path of a folder represented in AppleScript (HFS) path style
Re: Path of a folder represented in AppleScript (HFS) path style
- Subject: Re: Path of a folder represented in AppleScript (HFS) path style
- From: Bill Monk <email@hidden>
- Date: Wed, 13 Jul 2005 08:52:08 -0500
As has been noted, they're called HFS paths rather than Applescript
paths, which probably explains why your archive search didn't turn up
anything useful.
I was slightly surprised that NSString's NSStringPathExtensions
didn't offer this, and very surprised at how difficult it was to
figure out how to do this simple thing.
It was difficult because if you do a full-text search for "HFS Path"
in the Xcode Documentation window, you get NO hits. NONE. So you
never see kCFURLHFSPathStyle, which would lead you directly to a
solution like the one offered below.
#import <Foundation/Foundation.h>
// Add a category method to NSString to convert POSIX paths
// to colon-delimted HFS paths.
@interface NSString (HFSPathUtils)
- (NSString *)HFSPathFromPOSIXPath;
- (BOOL)pascalString:(StringPtr)outPString maxLen:(long)bufferSize;
@end
@implementation NSString (HFSPathUtils)
//
// Convert a slash-delimited POSIX path to a colon-delimited HFS path.
// Note the HFS path will be represented as Unicode characters
// in an NSString. If intending to use the HFS path with Carbon,
// then use the pascalString category method to obtain the ASCII
// length-prefixed pascal string Carbon requires.
//
- (NSString *)HFSPathFromPOSIXPath {
// thanks to stone.com for the pointer to
CFURLCreateWithFileSystemPath()
CFURLRef url;
CFStringRef hfsPath = NULL;
BOOL isDirectoryPath = [self hasSuffix:@"/"];
// Note that for the usual case of absolute paths,
isDirectoryPath is
// completely ignored by CFURLCreateWithFileSystemPath.
// isDirectoryPath is only considered for relative paths.
// This code has not really been tested relative paths...
url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
(CFStringRef)self,
kCFURLPOSIXPathStyle,
isDirectoryPath);
if (NULL != url) {
// Convert URL to a colon-delimited HFS path
// represented as Unicode characters in an NSString.
hfsPath = CFURLCopyFileSystemPath(url, kCFURLHFSPathStyle)
if (NULL != hfsPath) {
[(NSString *)hfsPath autorelease];
}
CFRelease(url);
}
return hfsPath;
}
//
// fill the caller's buffer with a length-prefixed pascal-style ASCII
string
// converted from the Uncode characters in an NSString.
//
- (BOOL)pascalString:(StringPtr)outPStringPtr maxLen:(long)bufferSize
{
BOOL convertedOK = NO;
if ( outPStringPtr != NULL ) {
convertedOK = CFStringGetPascalString( (CFStringRef)self,
outPStringPtr,
bufferSize,
CFStringGetSystemEncoding() );
}
return convertedOK;
}
@end
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden