-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQSFileTagsPlugInAction.m
210 lines (180 loc) · 8.76 KB
/
QSFileTagsPlugInAction.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
//
// QSFileTagsPlugInAction.m
// QSFileTagsPlugIn
//
// Created by Nicholas Jitkoff on 5/3/05.
// Copyright __MyCompanyName__ 2005. All rights reserved.
//
#import "QSMDTagsQueryManager.h"
#import "QSFileTagsPlugInAction.h"
#define kQSFileTagsPlugInAction @"QSFileTagsPlugInAction"
@implementation QSFileTagsPlugInAction
#pragma mark - Quicksilver Actions
- (QSObject *)addTagsForFile:(QSObject *)dObject tags:(QSObject *)iObject {
NSArray *newTags = [[iObject stringValue] componentsSeparatedByString:@" "];
newTags = [[QSMDTagsQueryManager sharedInstance] performSelector:@selector(stringByAddingTagPrefix:) onObjectsInArray:newTags returnValues:YES];
[self tagFiles:[dObject validPaths] add:newTags remove:nil set:nil];
return nil;
}
- (QSObject *)removeTagsForFile:(QSObject *)dObject tags:(QSObject *)iObject {
NSArray *newTags = [[iObject stringValue] componentsSeparatedByString:@" "];
newTags = [[QSMDTagsQueryManager sharedInstance] performSelector:@selector(stringByAddingTagPrefix:) onObjectsInArray:newTags returnValues:YES];
[self tagFiles:[dObject validPaths] add:nil remove:newTags set:nil];
return nil;
}
- (QSObject *)setTagsForFile:(QSObject *)dObject tags:(QSObject *)iObject {
NSArray *newTags = [[iObject stringValue] componentsSeparatedByString:@" "];
newTags = [[QSMDTagsQueryManager sharedInstance] performSelector:@selector(stringByAddingTagPrefix:) onObjectsInArray:newTags returnValues:YES];
[self tagFiles:[dObject validPaths] add:nil remove:nil set:newTags];
return nil;
}
- (QSObject *)showWindowForTag:(QSObject *)dObject {
// NSString *string = [dObject stringValue];
return nil;
}
- (QSObject *)showTags:(QSObject *)dObject {
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSString *comment = [ws commentForFile:[dObject singleFilePath]];
NSArray *tags = [self tagsFromString:comment];
NSMutableArray *tagObjects = [self performSelector:@selector(objectForTag:) onObjectsInArray:tags returnValues:YES];
NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:tagObjects, kQSResultArrayKey, nil];
[[NSNotificationCenter defaultCenter] postNotificationName:@"QSSourceArrayCreated" object:self userInfo:userInfo];
return nil;
}
- (QSObject *)showTaggedFilesInFinder:(QSObject *)dObject {
NSString *string = [[QSMDTagsQueryManager sharedInstance] stringByAddingTagPrefix:[dObject stringValue]];
NSArray *slices = [NSArray arrayWithObject:[NSDictionary dictionaryWithObjectsAndKeys:@"Othr", @"FXSliceKind", @"kMDItemFinderComment", @"FXAttribute", string, @"Value", @"S:**", @"Operator", nil]];
NSString *name = [NSString stringWithFormat:@" {%@} ", string];
NSString *query = [NSString stringWithFormat:@"(kMDItemFinderComment = '%@'cdw) ", string];
// NSLog(@"SPURL: %@ - %@", scheme, query);
[self runQuery:query withName:name slices:slices];
return nil;
}
- (QSObject *)setCommentForFile:(QSObject *)dObject to:(QSObject *)iObject{
NSString *newComment, *path, *oldComment;
NSArray *tags;
for (QSObject *targetFile in [dObject splitObjects]) {
path = [targetFile objectForType:QSFilePathType];
oldComment = [[NSWorkspace sharedWorkspace] commentForFile:path];
tags = [self tagsFromString:oldComment];
newComment = [self string:[iObject stringValue] byAddingTags:nil removingTags:nil settingTags:tags];
[[NSWorkspace sharedWorkspace] setComment:newComment forFile:path];
}
return nil;
}
#pragma mark - Quicksilver Validation
- (NSArray *)validIndirectObjectsForAction:(NSString *)action directObject:(QSObject *)dObject {
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
NSString *comment = [ws commentForFile:[dObject singleFilePath]];
QSObject *textObject = nil;
if ([action isEqualToString:@"QSAddFileTagsAction"]) {
textObject = [QSObject textProxyObjectWithDefaultValue:@""];
} else if ([action isEqualToString:@"QSSetFileCommentAction"]) {
NSString *comment = [self commentFromString:[[NSWorkspace sharedWorkspace] commentForFile:[dObject singleFilePath]]];
return [NSArray arrayWithObject:[QSObject textProxyObjectWithDefaultValue:comment?comment:@""]];
} else {
NSArray *tags = [self tagsFromString:comment];
tags = [[QSMDTagsQueryManager sharedInstance] performSelector:@selector(stringByRemovingTagPrefix:) onObjectsInArray:tags returnValues:YES];
textObject = [QSObject textProxyObjectWithDefaultValue:[tags componentsJoinedByString:@" "]];
}
return [NSArray arrayWithObject:textObject]; //[QSLibarrayForType:NSFilenamesPboardType];
}
#pragma mark - Helper Methods
- (NSArray *)tagsFromString:(NSString *)string {
NSArray *tags = [string componentsSeparatedByString:@" "];
NSMutableSet *realTags = [NSMutableSet set];
for (NSString *tag in tags) {
if ([[QSMDTagsQueryManager sharedInstance] stringByRemovingTagPrefix:tag]) {
[realTags addObject:tag];
}
}
return [realTags allObjects];
}
- (NSString *)commentFromString:(NSString *)string
{
return [self string:string byAddingTags:nil removingTags:[self tagsFromString:string] settingTags:nil];
}
- (NSString *)string:(NSString *)string byAddingTags:(NSArray *)add removingTags:(NSArray *)remove settingTags:(NSArray *)setTags {
NSMutableArray *words = [NSMutableArray array];
NSMutableSet *tags;
// process the existing Spotlight comment
if ([string length]) {
NSArray *array = [string componentsSeparatedByString:@" "];
// split words from tags in the existing comment (preserving order and duplicates)
for (NSString *component in array) {
if (![[QSMDTagsQueryManager sharedInstance] stringByRemovingTagPrefix:component]) {
[words addObject:component];
}
}
}
if ([setTags count]) {
// overwrite exisitng tags
tags = [NSMutableSet setWithArray:setTags];
} else {
// build a list of tags
tags = [NSMutableSet setWithArray:[self tagsFromString:string]];
NSSet *removeTags = [NSSet setWithArray:remove];
NSSet *addTags = [NSSet setWithArray:add];
[tags minusSet:removeTags];
[tags unionSet:addTags];
}
[words addObjectsFromArray:[tags allObjects]];
NSString *result = [words componentsJoinedByString:@" "];
return result;
}
- (void)tagFiles:(NSArray *)paths add:(NSArray *)add remove:(NSArray *)remove set:(NSArray *)set {
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
for (NSString *path in paths) {
NSString *comment = [ws commentForFile:path];
comment = [self string:comment byAddingTags:add removingTags:remove settingTags:set];
// NSLog(@"newcomm %@", comment);
[ws setComment:comment forFile:path];
}
return;
}
- (QSObject *)objectForTag:(NSString *)tag {
return [QSObject objectWithType:QSFileTagType value:tag name:[[QSMDTagsQueryManager sharedInstance] stringByRemovingTagPrefix:tag]];
}
- (void)runQuery:(NSString *)query withName:(NSString *)name slices:(NSArray *)slices {
NSString *trueQuery = query;
if ([trueQuery rangeOfString:@"kMD"] .location == NSNotFound) {
trueQuery = [NSString stringWithFormat:@"((kMDItemFSName = '%@*'cd) || kMDItemTextContent = '%@*'cd) && (kMDItemContentType != com.apple.mail.emlx) && (kMDItemContentType != public.vcard) ", query, query];
}
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO] forKey:@"ToolbarVisible"]
forKey:@"ViewOptions"];
[dict setObject:[NSNumber numberWithInt:0]
forKey:@"CompatibleVersion"];
[dict setObject:trueQuery
forKey:@"RawQuery"];
if ([NSApplication isLeopard]) {
// Saved searches seem to now require an array of paths to search. Current code uses root as the path.
[dict setObject:[NSDictionary dictionaryWithObjectsAndKeys:
//query, @"AnyAttributeContains",
[NSArray arrayWithObjects:@"/", nil], @"FXScopeArrayOfPaths",
slices, @"FXCriteriaSlices",
nil]
forKey:@"SearchCriteria"];
} else {
[dict setObject:[NSDictionary dictionaryWithObjectsAndKeys:
//query, @"AnyAttributeContains",
slices,
@"FXCriteriaSlices",
nil]
forKey:@"SearchCriteria"];
}
NSMutableString *filename = [name mutableCopy];
if (!filename) {
filename = [query mutableCopy];
[filename replaceOccurrencesOfString:@"/" withString:@"_" options:0 range:NSMakeRange(0, [filename length])];
if ([filename length] > 242)
filename = [[filename substringToIndex:242] mutableCopy];
}
[filename appendString:@".savedSearch"];
filename = (NSMutableString*)[NSTemporaryDirectory() stringByAppendingPathComponent:filename];
[dict writeToFile:filename atomically:NO];
[[NSFileManager defaultManager] setAttributes:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:
NSFileExtensionHidden] ofItemAtPath:filename error:nil];
[[NSWorkspace sharedWorkspace] openFile:filename];
}
@end