This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
DIAppController.m
75 lines (69 loc) · 2.94 KB
/
DIAppController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//
// DIAppController.m
// Delete Immediately
//
// Created by Jacob Bandes-Storch on 8/30/09.
//
#import "DIAppController.h"
@implementation DIAppController
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[NSApp setServicesProvider:self];
}
- (void)deleteImmediately:(NSPasteboard *)pasteboard
userData:(NSString *)userData
error:(NSString **)error {
NSArray *items = nil;
NSArray *urlItems = [pasteboard readObjectsForClasses:[NSArray arrayWithObject:[NSURL class]]
options:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:NSPasteboardURLReadingFileURLsOnlyKey]];
NSMutableArray *new = [NSMutableArray array];
for (NSURL *url in urlItems) {
[new addObject:[url path]];
}
items = new;
if (items && [items count] > 0) {
NSString *messageText;
NSString *infoText = NSLocalizedString(@"INFO_SINGLE", @"Deletion info (singular)");
if ([items count] > 1) {
infoText = NSLocalizedString(@"INFO_MULTIPLE", @"Deletion info (plural)");
messageText = [NSString stringWithFormat:
NSLocalizedString(@"CONFIRM_DELETE_MULTIPLE", @"Confirm multiple file deletion"), [items count]];
} else {
messageText = [NSString stringWithFormat:
NSLocalizedString(@"CONFIRM_DELETE_SINGLE", @"Confirm single file deletion (with name)"),
[[NSFileManager defaultManager] displayNameAtPath:[items objectAtIndex:0]]];
}
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"DIWarnBeforeDelete"]) {
NSAlert *alert = [NSAlert alertWithMessageText:messageText
defaultButton:NSLocalizedString(@"CONFIRM_DELETE", @"Confirm delete button")
alternateButton:NSLocalizedString(@"DENY_DELETE", @"Deny deletion button")
otherButton:nil
informativeTextWithFormat:infoText];
// [alert setShowsSuppressionButton:YES];
// [[alert suppressionButton] setTitle:@"Don’t show this warning again"];
[NSApp activateIgnoringOtherApps:YES];
if ([alert runModal] != NSAlertDefaultReturn) {
return; // Don't delete
}
// Suppression checkbox has no effect if the delete is cancelled
// if ([[alert suppressionButton] state] == NSOnState) {
// [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"DIWarnBeforeDelete"];
// }
}
for (NSString *path in items) {
NSError *error;
if (![[NSFileManager defaultManager] removeItemAtPath:path error:&error]) {
[NSApp activateIgnoringOtherApps:YES];
NSLog(@"Error deleting file at path %@! Error: %@", path, error);
[[NSAlert alertWithError:error] runModal];
}
// This doesn't really seem to work. Oh well.
// According to the docs, we're not "normally" supposed to invoke it manually,
// but I'm taking that to mean nothing will break outright, and it might work sometimes.
[NSApp deactivate];
}
} else {
NSLog(@"Data not available for public.file-url or NSFilenamesPboardType! Available types: %@", [pasteboard types]);
}
}
@end