Create subdirectories for autosave

Saving to the Desktop like Screen Shot sounded great at the time,
and I turn of Desktop icons so it didn't bother me, but really it
was a mess and a bit silly considering the number of autosave
clippings that can be produced. So it is time to create year and
month directories to organize them.

Manually saved clippings don't get year and month directories
because they are an explicit user action and the user can handle
them (and might well not want to have to hunt for them).

Surely some users will want year/month/day directories, and some
only year. Perhaps some none. I can't bring myself to make the
settings panel more complicated, so year/month seemed to strike
a balance.
This commit is contained in:
Mark Jerde 2022-01-21 20:38:19 -06:00
parent 000f459774
commit 296d9a8b4d

View file

@ -21,7 +21,8 @@
#endif #endif
@implementation FlycutOperator { @implementation FlycutOperator {
NSDateFormatter *dateFormatter; NSDateFormatter *dateFormatterForFilename;
NSDateFormatter *dateFormatterForDirectory;
} }
- (id)init - (id)init
@ -248,20 +249,32 @@
NSString *pbFullText = [self clippingStringWithCount:index inStore:store]; NSString *pbFullText = [self clippingStringWithCount:index inStore:store];
pbFullText = [pbFullText stringByReplacingOccurrencesOfString:@"\r" withString:@"\r\n"]; pbFullText = [pbFullText stringByReplacingOccurrencesOfString:@"\r" withString:@"\r\n"];
if (!dateFormatter) { if (!dateFormatterForFilename) {
// Date formatters are time-expensive to create, so create once and reuse. // Date formatters are time-expensive to create, so create once and reuse.
dateFormatter = [[NSDateFormatter alloc] init]; dateFormatterForFilename = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd 'at' HH.mm.ss"]; [dateFormatterForFilename setDateFormat:@"yyyy-MM-dd 'at' HH.mm.ss"];
} }
// Get the timestamp string: // Get the timestamp string:
NSDate *currentDate = [NSDate date]; NSDate *currentDate = [NSDate date];
NSString *dateString = [dateFormatter stringFromDate:currentDate]; NSString *dateString = [dateFormatterForFilename stringFromDate:currentDate];
// Make a file name // Make a file name
NSString *fileName = [NSString stringWithFormat:@"%@%@Clipping %@.txt", NSString *fileName = [NSString stringWithFormat:@"%@%@Clipping %@.txt",
prefix, store == favoritesStore ? @"Favorite " : @"", dateString]; prefix, store == favoritesStore ? @"Favorite " : @"", dateString];
// Make a subdirectory, if doing autosave, to avoid a directory with too many files in it as that can make Finder effectively hang on launch if that directory were the desktop.
NSString *subdirectoryString = nil;
if ([prefix isEqualToString:@"Autosave "]) {
if (!dateFormatterForDirectory) {
// Date formatters are time-expensive to create, so create once and reuse.
dateFormatterForDirectory = [[NSDateFormatter alloc] init];
[dateFormatterForDirectory setDateFormat:@"'Autosave Clippings' yyyy/MM"];
}
subdirectoryString = [dateFormatterForDirectory stringFromDate:currentDate];
}
NSString *baseDirectoryString = nil;
NSString *fileNameWithPath = nil; NSString *fileNameWithPath = nil;
#ifdef FLYCUT_MAC #ifdef FLYCUT_MAC
@ -281,28 +294,43 @@
if ([prefix isEqualToString:@"Autosave "]) { if ([prefix isEqualToString:@"Autosave "]) {
NSURL* autoSaveToLocation = [[NSUserDefaults standardUserDefaults] URLForKey:@"autoSaveToLocation"]; NSURL* autoSaveToLocation = [[NSUserDefaults standardUserDefaults] URLForKey:@"autoSaveToLocation"];
if (autoSaveToLocation) { if (autoSaveToLocation) {
fileNameWithPath = [NSString stringWithFormat:@"%@/%@", baseDirectoryString = autoSaveToLocation.path;
autoSaveToLocation.path, fileName];
} }
} else { } else {
NSURL* saveToLocation = [[NSUserDefaults standardUserDefaults] URLForKey:@"saveToLocation"]; NSURL* saveToLocation = [[NSUserDefaults standardUserDefaults] URLForKey:@"saveToLocation"];
if (saveToLocation) { if (saveToLocation) {
fileNameWithPath = [NSString stringWithFormat:@"%@/%@", baseDirectoryString = saveToLocation.path;
saveToLocation.path, fileName];
} }
} }
} }
#endif #endif
if (!fileNameWithPath) { if (!fileNameWithPath) {
// Get the Desktop directory: // An exact place to save wasn't set (like from a Save panel), so build one.
if (!baseDirectoryString) {
// Get the Desktop directory since nothing else was specified.
NSArray *paths = NSSearchPathForDirectoriesInDomains NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDesktopDirectory, NSUserDomainMask, YES); (NSDesktopDirectory, NSUserDomainMask, YES);
NSString *desktopDirectory = [paths objectAtIndex:0]; NSString *desktopDirectory = [paths objectAtIndex:0];
// Make a file name to write the data to using the Desktop directory: baseDirectoryString = desktopDirectory;
}
if (subdirectoryString) {
// Apply a subdirectory / subdirectories.
NSString *directoryPath = [NSString stringWithFormat:@"%@/%@",
baseDirectoryString, subdirectoryString];
[[NSFileManager defaultManager] createDirectoryAtPath:directoryPath
withIntermediateDirectories:YES
attributes:nil
error:nil];
baseDirectoryString = directoryPath;
}
// Make a file name to write the data to using the base directory:
fileNameWithPath = [NSString stringWithFormat:@"%@/%@", fileNameWithPath = [NSString stringWithFormat:@"%@/%@",
desktopDirectory, fileName]; baseDirectoryString, fileName];
} }
// Save content to the file // Save content to the file