ComBelkadanUtilsFilePathFormatter
Language: Objective-C, Author: Belkadan Software
License: MIT/X11
Formats an absolute file path into its display name. When the user edits the field the entire path is shown. By default, also tries to autocomplete a path entered by the user, and only allows paths starting with the root folder (/) or home folder (~).
The full name of this class is ComBelkadanUtils_FilePathFormatter; for convenience it is #define'd in the header file to FilePathFormatter. YOU MUST USE THE FULL NAME IN INTERFACE BUILDER.
ComBelkadanUtilsFilePathFormatter source preview
/*******************************************************************************
Copyright (c) 2004-2007 Jordy Rose
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior authorization.
*******************************************************************************/
#import "ComBelkadanUtilsFilePathFormatter.h"
@implementation FilePathFormatter
- (id)init
{
if (self = [super init]) {
shouldCompleteFilenames = YES;
}
return self;
}
- (BOOL)shouldCompleteFilenames
{
return shouldCompleteFilenames;
}
- (void)setShouldCompleteFilenames:(BOOL)newShouldComplete
{
shouldCompleteFilenames = newShouldComplete;
}
- (NSString *)stringForObjectValue:(id)path
{
if (![path isKindOfClass:[NSString class]]) return [path description];
return [[NSFileManager defaultManager] displayNameAtPath:path];
}
- (NSString *)editingStringForObjectValue:(id)obj
{
if (![obj isKindOfClass:[NSString class]]) return [obj description];
else return obj;
}
- (BOOL)getObjectValue:(id *)obj forString:(NSString *)str errorDescription:(NSString **)error
{
*obj = [str stringByStandardizingPath];
return YES;
}
- (BOOL)isPartialStringValid:(NSString **)partialStringPtr
proposedSelectedRange:(NSRangePointer)proposedSelRangePtr
originalString:(NSString *)origString
originalSelectedRange:(NSRange)origSelRange
errorDescription:(NSString **)error
{
if ([self shouldCompleteFilenames]) {
if (!([*partialStringPtr length] == 0 || [*partialStringPtr hasPrefix:@"~"] || [*partialStringPtr hasPrefix:@"/"])) {
return NO;
} else if (([origString length] - origSelRange.length) < ([*partialStringPtr length] - proposedSelRangePtr->length)) {
NSString *newString;
NSArray *matches;
int count = [*partialStringPtr completePathIntoString:&newString caseSensitive:NO matchesIntoArray:&matches filterTypes:nil];
if (count > 0) {
*proposedSelRangePtr = NSMakeRange([*partialStringPtr length], 0);
*partialStringPtr = [matches objectAtIndex:0];
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSEnumerator *matchEnum = [matches objectEnumerator];
do {
*partialStringPtr = [matchEnum nextObject];
} while (*partialStringPtr != nil && [workspace isInvisibleFileAtPath:*partialStringPtr]);
if (*partialStringPtr != nil) {
proposedSelRangePtr->length = [*partialStringPtr length] - proposedSelRangePtr->location;
NSRange firstPathSeparator = [*partialStringPtr rangeOfString:@"/" options:0 range:*proposedSelRangePtr];
if (firstPathSeparator.location != NSNotFound) {
*partialStringPtr = [*partialStringPtr substringToIndex:firstPathSeparator.location];
proposedSelRangePtr->length = [*partialStringPtr length] - proposedSelRangePtr->location;
}
return NO;
}
}
}
}
return YES;
}
@end
ComBelkadanUtilsFilePathFormatter header preview
/*******************************************************************************
Copyright (c) 2004-2007 Jordy Rose
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Except as contained in this notice, the name(s) of the above copyright holders
shall not be used in advertising or otherwise to promote the sale, use or other
dealings in this Software without prior authorization.
*******************************************************************************/
#import <Cocoa/Cocoa.h>
#import "NSWorkspace+BSInvisibility.h"
#define FilePathFormatter ComBelkadanUtils_FilePathFormatter
@interface FilePathFormatter : NSFormatter {
BOOL shouldCompleteFilenames;
}
@end
Download Archive
Dependencies:
Compatible with:
- Mac OS X 10.3
- Mac OS X 10.4 PPC
- Mac OS X 10.4 Intel

This is a great piece of code; I found it really handy. However, it has a typo that prevents it from functioning correctly: on line 92 of ComBelkadanUtilsFilePathFormatter.m is says "isInvisibleAtPath" when it should say "isInvisibleFileAtPath". Fixing this made it work perfectly.
Fixed, thanks!