-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNSWorkspace_PDCategories.m
677 lines (549 loc) · 17.6 KB
/
NSWorkspace_PDCategories.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
//
// NSWorkspace_PDCategories.m
// SproutedUtilities
//
// Created by Philip Dow on 9/9/06.
// Copyright Sprouted. All rights reserved.
// All inquiries should be directed to developer@journler.com
//
#import <SproutedUtilities/NSWorkspace_PDCategories.h>
#import <SproutedUtilities/NDAlias.h>
#import <SproutedUtilities/NDAlias+AliasFile.h>
// UTType Conformity (parents)
static NSString *kConformsToKey = @"UTTypeConformsTo";
// Finder label colors
/*
static BOOL sFinderColorsInited = NO;
static NSColor* sFinderColors[8] = { nil, nil, nil, nil, nil, nil, nil, nil };
static void initFinderColors(void)
{
sFinderColors[0] = [NSColor clearColor] ;
sFinderColors[1] = [NSColor colorWithCalibratedRed:(168.0/255.0) green:(168.0/255.0) blue:(168.0/255.0) alpha:1.0] ; // gray
sFinderColors[2] = [NSColor colorWithCalibratedRed:(174.0/255.0) green:(216.0/255.0) blue:( 65.0/255.0) alpha:1.0] ; // green
sFinderColors[3] = [NSColor colorWithCalibratedRed:(194.0/255.0) green:(138.0/255.0) blue:(217.0/255.0) alpha:1.0] ; // purple
sFinderColors[4] = [NSColor colorWithCalibratedRed:( 92.0/255.0) green:(160.0/255.0) blue:(255.0/255.0) alpha:1.0] ; // blue
sFinderColors[5] = [NSColor colorWithCalibratedRed:(235.0/255.0) green:(219.0/255.0) blue:( 65.0/255.0) alpha:1.0] ; // yellow
sFinderColors[6] = [NSColor colorWithCalibratedRed:(252.0/255.0) green:( 99.0/255.0) blue:( 89.0/255.0) alpha:1.0] ; // red
sFinderColors[7] = [NSColor colorWithCalibratedRed:(246.0/255.0) green:(170.0/255.0) blue:( 62.0/255.0) alpha:1.0] ; // orange
sFinderColorsInited = YES ;
}
*/
// set label code is from the osxutils project, Copyright (C) 2003-2005 Sveinbjorn Thordarson <sveinbt@hi.is>
// it has been modified to fix inconsistencies.
static OSErr FSpGetPBRec(const FSSpec* fileSpec, CInfoPBRec *infoRec)
{
CInfoPBRec myInfoRec = {0};
OSErr err = noErr;
myInfoRec.hFileInfo.ioNamePtr = (unsigned char *)fileSpec->name;
myInfoRec.hFileInfo.ioVRefNum = fileSpec->vRefNum;
myInfoRec.hFileInfo.ioDirID = fileSpec->parID;
err = PBGetCatInfoSync(&myInfoRec);
if (err == noErr)
*infoRec = myInfoRec;
return err;
}
static void SetLabelInFlags (short *flags, short labelNum)
{
short myFlags = *flags;
//nullify former label
/* Is Orange */
if (myFlags & 2 && myFlags & 8 && myFlags & 4)
{
myFlags -= 2;
myFlags -= 8;
myFlags -= 4;
}
/* Is Red */
if (myFlags & 8 && myFlags & 4)
{
myFlags -= 8;
myFlags -= 4;
}
/* Is Yellow */
if (myFlags & 8 && myFlags & 2)
{
myFlags -= 8;
myFlags -= 2;
}
/* Is Blue */
if (myFlags & 8)
{
myFlags -= 8;
}
/* Is Purple */
if (myFlags & 2 && myFlags & 4)
{
myFlags -= 2;
myFlags -= 4;
}
/* Is Green */
if (myFlags & 4)
{
myFlags -= 4;
}
/* Is Gray */
if (myFlags & 2)
{
myFlags -= 2;
}
//OK, now all the labels should be at off
//create flags with the desired label
switch(labelNum)
{
case 0://None
break;
//case 1:
case 6:
myFlags += 8;
myFlags += 4;
break;
//case 2:
case 7:
myFlags += 2;
myFlags += 8;
myFlags += 4;
break;
//case 3:
case 5:
myFlags += 2;
myFlags += 8;
break;
//case 4:
case 2:
myFlags += 4;
break;
//case 5:
case 4:
myFlags += 8;
break;
//case 6:
case 3:
myFlags += 2;
myFlags += 4;
break;
//case 7:
case 1:
myFlags += 2;
break;
}
//now, to set the desired label
*flags = myFlags;
}
static short GetLabelNumber (short flags)
{
/* Is Orange */ // GREEN
if (flags & 2 && flags & 8 && flags & 4)
return 2;
/* Is Red */ // GREY
if (flags & 8 && flags & 4)
return 1;
/* Is Yellow */ // PURPLE
if (flags & 8 && flags & 2)
return 3;
/* Is Blue */ // YELLOW
if (flags & 8)
return 5;
/* Is Purple */ // RED
if (flags & 2 && flags & 4)
return 6;
/* Is Green */ // BLUE
if (flags & 4)
return 4;
/* Is Gray */ // ORANGE
if (flags & 2)
return 7;
return 0;
}
@implementation NSWorkspace (PDCategories)
- (NSString*) UTIForFile:(NSString*)path
{
FSRef fsRefToItem;
FSPathMakeRef( (const UInt8 *)[path fileSystemRepresentation], &fsRefToItem, NULL );
CFStringRef itemUTI = NULL;
LSCopyItemAttribute( &fsRefToItem, kLSRolesAll, kLSItemContentType, (CFTypeRef *)&itemUTI );
if ( itemUTI == NULL )
{
NSAppleEventDescriptor *ed;
NSAppleScript *script = [[NSAppleScript alloc]
initWithSource:[NSString stringWithFormat:@"do shell script \"/usr/bin/file -b '%@'\"", path]];
ed = [script executeAndReturnError:nil];
if ( ed )
{
// grab the location of the file according to iTunes import
NSString *scriptReturn = [ed stringValue];
if ( [scriptReturn rangeOfString:@"text" options:NSCaseInsensitiveSearch].location != NSNotFound )
itemUTI = CFStringCreateCopy(NULL,kUTTypePlainText);
}
}
if ( itemUTI == NULL )
return nil;
else
return [(NSString*)itemUTI autorelease];
}
- (NSArray*) allParentsAsArrayForUTI:(NSString*)uti
{
if ( uti == nil )
return nil;
// the type declaration
NSDictionary *utiDeclaration = [(NSDictionary*)UTTypeCopyDeclaration((CFStringRef)uti) autorelease];
if ( utiDeclaration == nil )
return nil;
// the parents
id conformsTo = [utiDeclaration objectForKey:kConformsToKey];
if ( conformsTo == nil )
return nil;
// if the parent is singular, it's a string, recast as array
if ( ![conformsTo isKindOfClass:[NSArray class]] )
conformsTo = [NSArray arrayWithObject:conformsTo];
NSString *aParentUTI;
NSEnumerator *enumerator = [conformsTo objectEnumerator];
NSMutableArray *myUTIs = [NSMutableArray array];
// recursively iterate through each parent, deriving the grandparents
while ( aParentUTI = [enumerator nextObject] )
{
// add the current parent to the array
[myUTIs addObject:aParentUTI];
// get the grandparents and append that
NSArray *parentsUp = [self allParentsAsArrayForUTI:aParentUTI];
if ( parentsUp != nil ) [myUTIs addObjectsFromArray:parentsUp];
}
return myUTIs;
}
- (NSString*) allParentsForUTI:(NSString*)uti
{
if ( uti == nil )
return nil;
// the type declaration
NSDictionary *utiDeclaration = [(NSDictionary*)UTTypeCopyDeclaration((CFStringRef)uti) autorelease];
if ( utiDeclaration == nil )
return nil;
// the parents
id conformsTo = [utiDeclaration objectForKey:kConformsToKey];
if ( conformsTo == nil )
return nil;
// if the parent is singular, it's a string, recast as array
if ( ![conformsTo isKindOfClass:[NSArray class]] )
conformsTo = [NSArray arrayWithObject:conformsTo];
NSString *aParentUTI;
NSEnumerator *enumerator = [conformsTo objectEnumerator];
NSMutableString *myUTIs = [NSMutableString string];
// recursively iterate through each parent, deriving the grandparents
while ( aParentUTI = [enumerator nextObject] )
{
// add the current parent to the string
[myUTIs appendString:[NSString stringWithFormat:@"%@,",aParentUTI]];
// get the grandparents and append that
NSString *parentsUp = [self allParentsForUTI:aParentUTI];
if ( parentsUp != nil )
[myUTIs appendString:[NSString stringWithFormat:@"%@,",parentsUp]];
}
// remove the trailing comma
if ( [myUTIs characterAtIndex:[myUTIs length]-1] == ',' )
[myUTIs deleteCharactersInRange:NSMakeRange([myUTIs length]-1,1)];
return myUTIs;
}
#pragma mark -
- (BOOL) file:(NSString*)path conformsToUTI:(NSString*)uti
{
if ( path == nil )
return NO;
NSString *utiAtPath = [self UTIForFile:path];
if ( utiAtPath == nil )
return NO;
return ( UTTypeConformsTo( (CFStringRef)utiAtPath, (CFStringRef)uti ) );
}
- (BOOL) file:(NSString*)path confromsToUTIInArray:(NSArray*)anArray
{
if ( path == nil )
return NO;
NSString *utiAtPath = [self UTIForFile:path];
if ( utiAtPath == nil )
return NO;
BOOL conforms = NO;
NSString *aUTI;
NSEnumerator *enumerator = [anArray objectEnumerator];
while ( aUTI = [enumerator nextObject] )
{
if ( UTTypeConformsTo( (CFStringRef)utiAtPath, (CFStringRef)aUTI ) )
{
conforms = YES;
break;
}
}
return conforms;
}
#pragma mark -
- (NSString*) mdTitleForFile:(NSString*)filename
{
// have a look at the metadata for the file, returning the provided title if one exists
NSString *title = nil;
MDItemRef metaData = MDItemCreate(NULL,(CFStringRef)filename);
if ( metaData != NULL )
{
// grab the title
title = [(NSString*)MDItemCopyAttribute(metaData,kMDItemTitle) autorelease];
// use the md display name
if ( title == nil )
title = [(NSString*)MDItemCopyAttribute(metaData,kMDItemDisplayName) autorelease];
// use the display name at path
if ( title == nil )
title = [[filename lastPathComponent] stringByDeletingPathExtension];
// clean up
CFRelease(metaData);
}
else
{
// use the display name at path
title = [[filename lastPathComponent] stringByDeletingPathExtension];
}
return title;
}
- (NSString*) mdTitleAndComposerForAudioFile:(NSString*)filename
{
// have a look at the metadata for the file,
// author and name, or use display name
NSMutableString *returnString = [[NSMutableString allocWithZone:[self zone]] init];
MDItemRef metaData = MDItemCreate(NULL,(CFStringRef)filename);
if ( metaData != NULL ) {
NSString *title = (NSString*)MDItemCopyAttribute(metaData,kMDItemTitle);
NSArray *authors = (NSArray*)MDItemCopyAttribute(metaData,kMDItemAuthors);
NSString *composer = (NSString*)MDItemCopyAttribute(metaData,kMDItemComposer);
if ( title != nil ) {
if ( authors != nil )
[returnString appendFormat:@"%@ - ", [authors componentsJoinedByString:@", "]];
else if ( composer != nil )
[returnString appendFormat:@"%@ - ", composer];
[returnString appendString:title];
}
else {
// use the display name no path
[returnString appendString:[[filename lastPathComponent] stringByDeletingPathExtension]];
}
// clean up
CFRelease(metaData);
}
else {
// use the display name no path
[returnString appendString:[[filename lastPathComponent] stringByDeletingPathExtension]];
}
return [returnString autorelease];
}
#pragma mark -
- (BOOL) fileIsVCF:(NSString*)filePath
{
NSString *app, *fileType;
[[NSWorkspace sharedWorkspace] getInfoForFile:filePath application:&app type:&fileType];
return ( [fileType isEqual:@"vcf"] );
}
- (BOOL) fileIsClipping:(NSString*)filePath
{
NSString *app, *fileType;
[[NSWorkspace sharedWorkspace] getInfoForFile:filePath application:&app type:&fileType];
return ( [fileType isEqual:@"textClipping"] );
}
#pragma mark -
- (short)finderLabelColorForFile:(NSString*)inPath
{
// 0 = no color, 1 = gray, 2 = green, 3 = purple, 4 = blue, 5 = yellow, 6 = red, 7 = orange
CFURLRef url;
FSRef fsRef;
BOOL ret;
FSCatalogInfo cinfo;
//if (!sFinderColorsInited)
// initFinderColors();
url = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)inPath, kCFURLPOSIXPathStyle, FALSE);
if (!url)
return 0;
ret = CFURLGetFSRef(url, &fsRef);
CFRelease(url);
if (ret && (FSGetCatalogInfo(&fsRef, kFSCatInfoFinderInfo, &cinfo, NULL, NULL, NULL) == noErr))
return (((FileInfo*)&cinfo.finderInfo)->finderFlags & kColor) >> kIsOnDesk;
//return sFinderColors[ (((FileInfo*)&cinfo.finderInfo)->finderFlags & kColor) >> kIsOnDesk ] ;
else
return 0;
}
- (BOOL) setLabel:(short)labelNum forFile:(NSString*)path
{
OSErr err = noErr;
FSRef fileRef;
FSSpec fileSpec;
BOOL isFldr;
//short currentLabel;
FInfo finderInfo;
CInfoPBRec infoRec;
// make sure a file exists at the specified path
if ( path == nil || ![[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isFldr] )
return NO;
//get file reference from path
err = FSPathMakeRef((const UInt8 *)[path fileSystemRepresentation], &fileRef, NULL);
if (err != noErr)
{
NSLog(@"%@ %s - unable to get file reference for path %@", [self className], _cmd, path );
return NO;
}
//retrieve filespec from file ref
err = FSGetCatalogInfo (&fileRef, 0, NULL, NULL, &fileSpec, NULL);
if (err != noErr)
{
NSLog(@"%@ %s - unable to get file spec for path %@", [self className], _cmd, path );
return NO;
}
///////////////////////// IF SPECIFIED FILE IS A FOLDER /////////////////////////
if ( isFldr )
{
//Get HFS record
FSpGetPBRec(&fileSpec, &infoRec);
//get current label
//currentLabel = GetLabelNumber(infoRec.dirInfo.ioDrUsrWds.frFlags);
//set new label into record
SetLabelInFlags((short *)&infoRec.dirInfo.ioDrUsrWds.frFlags, labelNum);
//fill in the requisite fields
infoRec.hFileInfo.ioNamePtr = (unsigned char *)fileSpec.name;
infoRec.hFileInfo.ioVRefNum = fileSpec.vRefNum;
infoRec.hFileInfo.ioDirID = fileSpec.parID;
//set the record
PBSetCatInfoSync(&infoRec);
}
///////////////////////// IF SPECIFIED FILE IS A REGULAR FILE /////////////////////////
else
{
/* get the finder info */
err = FSpGetFInfo (&fileSpec, &finderInfo);
if (err != noErr)
{
NSLog(@"%@ %s - unable to get finder info for path %@", [self className], _cmd, path );
return NO;
}
//retrieve the label number of the file
//currentLabel = GetLabelNumber(finderInfo.fdFlags);
//if it's already set with the desired label we return
//if (currentLabel == labelNum)
// return YES;
//set the appropriate value in the flags field
SetLabelInFlags((short*)&finderInfo.fdFlags, labelNum);
//apply the settings to the file
err = FSpSetFInfo (&fileSpec, &finderInfo);
if (err != noErr)
{
NSLog(@"%@ %s - unable to set finder info for path %@", [self className], _cmd, path );
return NO;
}
}
return YES;
}
#pragma mark -
- (BOOL) moveToTrash:(NSString*)path
{
int tag;
return ( [[NSWorkspace sharedWorkspace]
performFileOperation:NSWorkspaceRecycleOperation
source:[path stringByDeletingLastPathComponent] destination:@""
files:[NSArray arrayWithObject:[path lastPathComponent]] tag:&tag] && tag == 0 );
}
- (NSString*) resolveForAliases:(NSString*)path {
NSString *resolvedPath = nil;
CFURLRef url;
// return a nil string if there is no path
if ( path == nil )
return nil;
url = CFURLCreateWithFileSystemPath(NULL, (CFStringRef)path, kCFURLPOSIXPathStyle, NO );
if (url != NULL)
{
FSRef fsRef;
if (CFURLGetFSRef(url, &fsRef))
{
Boolean targetIsFolder, wasAliased;
if (FSResolveAliasFile (&fsRef, true /*resolveAliasChains*/,
&targetIsFolder, &wasAliased) == noErr )
{
if ( wasAliased )
{
CFURLRef resolvedUrl = CFURLCreateFromFSRef(NULL, &fsRef);
if (resolvedUrl != NULL)
{
resolvedPath = (NSString*)
CFURLCopyFileSystemPath(resolvedUrl,
kCFURLPOSIXPathStyle);
CFRelease(resolvedUrl);
}
}
else
{
resolvedPath = [[NSString alloc] initWithString:path];
}
}
}
CFRelease(url);
}
//if (resolvedPath==nil)
// resolvedPath = [[NSString alloc] initWithString:path];
return [resolvedPath autorelease];
}
- (BOOL) createAliasForPath:(NSString*)targetPath toPath:(NSString*)destinationPath
{
// actually create the link
NDAlias *alias = [[NDAlias alloc] initWithPath:targetPath];
if ( ![alias writeToFile:destinationPath] )
{
NSLog(@"%@ %s - unable to alias %@ to %@", [self className], _cmd, targetPath, destinationPath);
return NO;
}
else
return YES;
}
#pragma mark -
- (BOOL) canPlayFile:(NSString*)filename
{
NSString *extension = [filename pathExtension];
NSArray *allFileTypes = [NSSound soundUnfilteredFileTypes];
NSString *app = nil, *fileType = nil;
[[NSWorkspace sharedWorkspace] getInfoForFile:filename application:&app type:&fileType];
if ( [[fileType lowercaseString] isEqualToString:@"pdf"]
|| [[extension lowercaseString] isEqualToString:@"pdf"] )
return NO; //pdf documents are a special case
if ( [allFileTypes containsObject:extension] )
return YES;
else
return NO;
}
- (BOOL) canWatchFile:(NSString*)filename
{
long quickTimeVersion = 0;
NSString *extension = [filename pathExtension];
NSArray *allFileTypes;
NSString *app = nil, *fileType = nil;
[[NSWorkspace sharedWorkspace] getInfoForFile:filename application:&app type:&fileType];
if ( [[fileType lowercaseString] isEqualToString:@"pdf"]
|| [[extension lowercaseString] isEqualToString:@"pdf"] )
return NO; //pdf documents are a special case
if (Gestalt(gestaltQuickTime, &quickTimeVersion) || ((quickTimeVersion & 0xFFFFFF00) < 0x07008000))
{
allFileTypes = [NSMovie movieUnfilteredFileTypes];
if ( [allFileTypes containsObject:extension] || [allFileTypes containsObject:fileType] )
return YES;
else
return NO;
}
else
{
allFileTypes = [QTMovie movieFileTypes:QTIncludeCommonTypes];
if ( [allFileTypes containsObject:extension] || [allFileTypes containsObject:fileType] )
return YES;
else
return NO;
//return [QTMovie canInitWithFile:filename];
}
}
- (BOOL) canViewFile:(NSString*)filename
{
NSString *app = nil, *fileType = nil;
NSString *extension = [filename pathExtension];
NSArray *allImageFileTypes = [NSImage imageFileTypes];
[[NSWorkspace sharedWorkspace] getInfoForFile:filename application:&app type:&fileType];
if ( !fileType || [fileType length] == 0
|| [[fileType lowercaseString] isEqualToString:@"pdf"]
|| [[extension lowercaseString] isEqualToString:@"pdf"] )
return NO; //pdf documents are a special case
return ( [allImageFileTypes containsObject:fileType] || [allImageFileTypes containsObject:extension] );
}
@end