My app receives both HFS and POSIX NSString file paths back from AppleScript.

On the AS Implementors list I was advised to coerce all the AS data to say HFS 
before it is converted to an NSString.

This will be my goal but in the short term I will need to detect HFS paths and 
convert them to POSIX.
 
What is the best way to *reliably* detect the HFS path type and convert it to 
POSIX?

My current approach is shown below but there has to be a better more direct way.

        // filePath is POSIX or HFS
        NSString *filePath = // some path

        //
        // creating a POSIX NSURL with HFS path creates a URL instance, 
        // though with a corrupt path, hence the comparison
        //
        NSURL *posixURL = [NSURL nd_URLWithFileSystemPathPOSIXStyle:filePath];
        if (![[posixURL path] isEqualToString:filePath]) {      // fails for 
HFS style path
                NSURL *hfsURL = [NSURL 
nd_URLWithFileSystemPathHFSStyle:filePath];
                filePath =[hfsURL nd_fileSystemPathPOSIXStyle];
        }

where an NSURL category exists defining the following

+ (NSURL *)nd_URLWithFileSystemPathPOSIXStyle:(NSString *)aPOSIXString
{
        CFURLRef theURL = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, 
(CFStringRef)aPOSIXString, kCFURLPOSIXPathStyle, [aPOSIXString hasSuffix:@"/"] 
);
        CFMakeCollectable( theURL );
        return [(NSURL *)theURL autorelease];
}

+ (NSURL *)nd_URLWithFileSystemPathHFSStyle:(NSString *)aHFSString
{
        CFURLRef theURL = CFURLCreateWithFileSystemPath( kCFAllocatorDefault, 
(CFStringRef)aHFSString, kCFURLHFSPathStyle, [aHFSString hasSuffix:@":"] );
        CFMakeCollectable( theURL );
        return [(NSURL *)theURL autorelease];
}

- (NSString *)nd_fileSystemPathPOSIXStyle
{
        CFStringRef     theString = CFURLCopyFileSystemPath((CFURLRef)self, 
kCFURLPOSIXPathStyle);
        CFMakeCollectable( theString );
        return [(NSString *)theString autorelease];
}
Regards

Jonathan Mitchell

Developer
http://www.mugginsoft.com






_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to