NSSavePanel *panel = [NSSavePanel savePanel];
panel.allowedFileTypes = @[ @"enjoyable" ];
NJMapping *mapping = _currentMapping;
- panel.nameFieldStringValue = mapping.name;
+ panel.nameFieldStringValue = [mapping.name stringByFixingPathComponent];
NSWindow *window = NSApplication.sharedApplication.keyWindow;
[panel beginSheetModalForWindow:window
completionHandler:^(NSInteger result) {
[pboard setString:@(rowIndexes.firstIndex).stringValue forType:PB_ROW];
[pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType];
return YES;
+ } else if (rowIndexes.count == 1 && rowIndexes.firstIndex == 0) {
+ [pboard declareTypes:@[NSFilesPromisePboardType] owner:nil];
+ [pboard setPropertyList:@[@"enjoyable"] forType:NSFilesPromisePboardType];
+ return YES;
} else {
return NO;
}
#import <Foundation/Foundation.h>
+@interface NSCharacterSet (FixFilename)
+
++ (NSCharacterSet *)invalidPathComponentCharacterSet;
+ // A character set containing the characters that are invalid to
+ // use in path components on common filesystems.
+
+@end
+
@interface NSString (FixFilename)
- (NSString *)stringByFixingPathComponent;
// Does various operations to make this string suitable for use as
- // a single path component of a normal filename. Removes / and :
- // characters and prepends a _ to avoid leading .s or an empty
- // name.
+ // a single path component of a normal filename. Removes
+ // characters that are invalid. Strips whitespace from the
+ // beginning and end. If the first character is a . or a -, a _ is
+ // added to the front.
@end
#import "NSString+FixFilename.h"
+@implementation NSCharacterSet (FixFilename)
+
++ (NSCharacterSet *)invalidPathComponentCharacterSet {
+ return [NSCharacterSet characterSetWithCharactersInString:@"\"\\/:*?<>|"];
+}
+
+@end
+
@implementation NSString (FixFilename)
- (NSString *)stringByFixingPathComponent {
- static NSCharacterSet *invalid;
- if (!invalid)
- invalid = [NSCharacterSet characterSetWithCharactersInString:@"/:"];
+ NSCharacterSet *invalid = NSCharacterSet.invalidPathComponentCharacterSet;
+ NSCharacterSet *whitespace = NSCharacterSet.whitespaceAndNewlineCharacterSet;
NSArray *parts = [self componentsSeparatedByCharactersInSet:invalid];
- NSString *newName = [parts componentsJoinedByString:@""];
- if (!newName.length)
+ NSString *name = [parts componentsJoinedByString:@"_"];
+ name = [name stringByTrimmingCharactersInSet:whitespace];
+ if (!name.length)
return @"_";
- if ([newName characterAtIndex:0] == '.')
- newName = [@"_" stringByAppendingString:newName];
- return newName;
+ unichar first = [name characterAtIndex:0];
+ if (first == '.' || first == '-')
+ name = [@"_" stringByAppendingString:name];
+ return name;
}
@end