| 1 |
/* |
| 2 |
* BSSUtil.c |
| 3 |
* BSSpotlighter |
| 4 |
* |
| 5 |
* Created by Hori,Masaki on 06/12/16. |
| 6 |
* Copyright 2006 masakih. All rights reserved. |
| 7 |
* |
| 8 |
*/ |
| 9 |
|
| 10 |
#import "BSSUtil.h" |
| 11 |
#import "stdarg.h" |
| 12 |
|
| 13 |
#import <Foundation/Foundation.h> |
| 14 |
#import "NSAppleEventDescriptor-HMExtensions.h" |
| 15 |
|
| 16 |
|
| 17 |
#import <AppKit/NSWorkspace.h> |
| 18 |
|
| 19 |
NSString *BSSLogForceWrite = @"BSSLogForceWrite"; |
| 20 |
|
| 21 |
void BSSLog(NSString *format, ...) |
| 22 |
{ |
| 23 |
va_list ap; |
| 24 |
|
| 25 |
va_start(ap, format); |
| 26 |
BSSLogv(format, ap); |
| 27 |
va_end(ap); |
| 28 |
} |
| 29 |
|
| 30 |
#ifdef DEBUG |
| 31 |
void BSSLogv(NSString *format, va_list args) |
| 32 |
{ |
| 33 |
NSLogv(format, args); |
| 34 |
} |
| 35 |
#else |
| 36 |
void BSSLogv(NSString *format, va_list args) |
| 37 |
{ |
| 38 |
NSUserDefaults *def = [NSUserDefaults standardUserDefaults]; |
| 39 |
if([def boolForKey:BSSLogForceWrite]) { |
| 40 |
NSLogv(format, args); |
| 41 |
} |
| 42 |
} |
| 43 |
#endif |
| 44 |
|
| 45 |
OSStatus activateFinderOnlyFrontWindow() |
| 46 |
{ |
| 47 |
[[NSWorkspace sharedWorkspace] launchApplication:@"Finder"]; |
| 48 |
return noErr; |
| 49 |
} |
| 50 |
|
| 51 |
inline static NSAppleEventDescriptor *fileDescriptor(NSString *filePath) |
| 52 |
{ |
| 53 |
NSAppleEventDescriptor *fileDesc; |
| 54 |
NSAppleEventDescriptor *fileNameDesc; |
| 55 |
NSURL *fileURL = [NSURL fileURLWithPath:filePath]; |
| 56 |
const char *fileURLCharP; |
| 57 |
|
| 58 |
fileURLCharP = [[fileURL absoluteString] fileSystemRepresentation]; |
| 59 |
fileNameDesc = [NSAppleEventDescriptor descriptorWithDescriptorType:typeFileURL |
| 60 |
bytes:fileURLCharP |
| 61 |
length:strlen(fileURLCharP)]; |
| 62 |
|
| 63 |
fileDesc = [NSAppleEventDescriptor objectSpecifierWithDesiredClass:cFile |
| 64 |
container:nil |
| 65 |
keyForm:formName |
| 66 |
keyData:fileNameDesc]; |
| 67 |
return fileDesc; |
| 68 |
} |
| 69 |
OSStatus openInFinderWithPath(NSString *filePath) |
| 70 |
{ |
| 71 |
NSAppleEventDescriptor *ae; |
| 72 |
OSStatus err = noErr; |
| 73 |
|
| 74 |
ae = [NSAppleEventDescriptor appleEventWithEventClass:kAEMiscStandards |
| 75 |
eventID:kAEMakeObjectsVisible |
| 76 |
targetAppName:@"Finder"]; |
| 77 |
if(!ae) { |
| 78 |
BSSLog(@"Can NOT create AppleEvent."); |
| 79 |
return kBSSUtilCanNotCreateAppleEventErr; |
| 80 |
} |
| 81 |
|
| 82 |
[ae setParamDescriptor:fileDescriptor(filePath) |
| 83 |
forKeyword:keyDirectObject]; |
| 84 |
|
| 85 |
@try { |
| 86 |
err = [ae sendAppleEventWithMode:kAENoReply | kAENeverInteract |
| 87 |
timeOutInTicks:kAEDefaultTimeout |
| 88 |
replay:NULL]; |
| 89 |
} |
| 90 |
@catch (NSException *ex) { |
| 91 |
if(![[ex name] isEqualTo:HMAEDescriptorSendingNotAppleEventException]) { |
| 92 |
@throw; |
| 93 |
} |
| 94 |
} |
| 95 |
@finally { |
| 96 |
if( err != noErr ) { |
| 97 |
BSSLog(@"AESendMessage Error. Error NO is %d.", err ); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
return activateFinderOnlyFrontWindow(); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
OSStatus openInfomationInFinderWithPath(NSString *filePath) |
| 106 |
{ |
| 107 |
NSAppleEventDescriptor *ae; |
| 108 |
OSStatus err = noErr; |
| 109 |
|
| 110 |
ae = [NSAppleEventDescriptor appleEventWithEventClass:kCoreEventClass |
| 111 |
eventID:kAEOpenDocuments |
| 112 |
targetAppName:@"Finder"]; |
| 113 |
if(!ae) { |
| 114 |
BSSLog(@"Can NOT create AppleEvent."); |
| 115 |
return kBSSUtilCanNotCreateAppleEventErr; |
| 116 |
} |
| 117 |
|
| 118 |
NSAppleEventDescriptor *fileInfoDesc = [NSAppleEventDescriptor |
| 119 |
objectSpecifierWithDesiredClass:cProperty |
| 120 |
container:fileDescriptor(filePath) |
| 121 |
keyForm:cProperty |
| 122 |
keyData:[NSAppleEventDescriptor descriptorWithTypeCode:cInfoWindow]]; |
| 123 |
|
| 124 |
[ae setParamDescriptor:fileInfoDesc |
| 125 |
forKeyword:keyDirectObject]; |
| 126 |
|
| 127 |
@try { |
| 128 |
err = [ae sendAppleEventWithMode:kAENoReply | kAENeverInteract |
| 129 |
timeOutInTicks:kAEDefaultTimeout |
| 130 |
replay:NULL]; |
| 131 |
} |
| 132 |
@catch (NSException *ex) { |
| 133 |
if(![[ex name] isEqualTo:HMAEDescriptorSendingNotAppleEventException]) { |
| 134 |
@throw; |
| 135 |
} |
| 136 |
} |
| 137 |
@finally { |
| 138 |
if( err != noErr ) { |
| 139 |
BSSLog(@"AESendMessage Error. Error NO is %d.", err ); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return noErr; |
| 144 |
} |