William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 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 | //
// AdiumApplescriptRunner.m
// Adium
//
// Created by Evan Schoenberg on 4/29/06.
//
#import "AdiumApplescriptRunner.h"
@implementation AdiumApplescriptRunner
- (id)init
{
if ((self = [super init])) {
NSDistributedNotificationCenter *distributedNotificationCenter = [NSDistributedNotificationCenter defaultCenter];
[distributedNotificationCenter addObserver:self
selector:@selector(applescriptRunnerIsReady:)
name:@"AdiumApplescriptRunner_IsReady"
object:nil];
[distributedNotificationCenter addObserver:self
selector:@selector(applescriptRunnerDidQuit:)
name:@"AdiumApplescriptRunner_DidQuit"
object:nil];
[distributedNotificationCenter addObserver:self
selector:@selector(applescriptDidRun:)
name:@"AdiumApplescript_DidRun"
object:nil];
//Check for an existing AdiumApplescriptRunner; if there is one, it will respond with AdiumApplescriptRunner_IsReady
[distributedNotificationCenter postNotificationName:@"AdiumApplescriptRunner_RespondIfReady"
object:nil
userInfo:nil
deliverImmediately:NO];
}
return self;
}
- (void)dealloc
{
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"AdiumApplescriptRunner_Quit"
object:nil
userInfo:nil
deliverImmediately:NO];
[super dealloc];
}
- (void)_executeApplescriptWithDict:(NSDictionary *)executionDict
{
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:@"AdiumApplescriptRunner_ExecuteScript"
object:nil
userInfo:executionDict
deliverImmediately:NO];
}
- (void)launchApplescriptRunner
{
NSString *applescriptRunnerPath = [[NSBundle mainBundle] pathForResource:@"AdiumApplescriptRunner"
ofType:nil
inDirectory:nil];
//Houston, we are go for launch.
if (applescriptRunnerPath) {
LSLaunchFSRefSpec spec;
FSRef appRef;
OSStatus err = FSPathMakeRef((UInt8 *)[applescriptRunnerPath fileSystemRepresentation], &appRef, NULL);
if (err == noErr) {
spec.appRef = &appRef;
spec.numDocs = 0;
spec.itemRefs = NULL;
spec.passThruParams = NULL;
spec.launchFlags = kLSLaunchDontAddToRecents | kLSLaunchDontSwitch | kLSLaunchNoParams | kLSLaunchAsync;
spec.asyncRefCon = NULL;
err = LSOpenFromRefSpec(&spec, NULL);
if (err != noErr) {
NSLog(@"Could not launch %@",applescriptRunnerPath);
}
}
} else {
NSLog(@"Could not find AdiumApplescriptRunner...");
}
}
/*!
* @brief Run an applescript, optinally calling a function with arguments, and notify a target/selector with its output when it is done
*/
- (void)runApplescriptAtPath:(NSString *)path function:(NSString *)function arguments:(NSArray *)arguments notifyingTarget:(id)target selector:(SEL)selector userInfo:(id)userInfo
{
NSString *uniqueID = [[NSProcessInfo processInfo] globallyUniqueString];
if (!runningApplescriptsDict) runningApplescriptsDict = [[NSMutableDictionary alloc] init];
if (target && selector) {
[runningApplescriptsDict setObject:[NSDictionary dictionaryWithObjectsAndKeys:
target, @"target",
NSStringFromSelector(selector), @"selector",
userInfo, @"userInfo", nil]
forKey:uniqueID];
}
NSDictionary *executionDict = [NSDictionary dictionaryWithObjectsAndKeys:
path, @"path",
(function ? function : @""), @"function",
(arguments ? arguments : [NSArray array]), @"arguments",
uniqueID, @"uniqueID",
nil];
if (applescriptRunnerIsReady) {
[self _executeApplescriptWithDict:executionDict];
} else {
if (!pendingApplescriptsArray) pendingApplescriptsArray = [[NSMutableArray alloc] init];
[pendingApplescriptsArray addObject:executionDict];
[self launchApplescriptRunner];
}
}
- (void)applescriptRunnerIsReady:(NSNotification *)inNotification
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDictionary *executionDict;
applescriptRunnerIsReady = YES;
for (executionDict in pendingApplescriptsArray) {
[self _executeApplescriptWithDict:executionDict];
}
[pendingApplescriptsArray release]; pendingApplescriptsArray = nil;
[pool release];
}
- (void)applescriptRunnerDidQuit:(NSNotification *)inNotification
{
applescriptRunnerIsReady = NO;
}
- (void)applescriptDidRun:(NSNotification *)inNotification
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDictionary *userInfo = [inNotification userInfo];
NSString *uniqueID = [userInfo objectForKey:@"uniqueID"];
NSDictionary *targetDict = [runningApplescriptsDict objectForKey:uniqueID];
if (targetDict) {
// Prevent a secondary "finish" from returning in the middle of the invocation.
[targetDict retain];
//No further need for this dictionary entry
[runningApplescriptsDict removeObjectForKey:uniqueID];
//If there's no others, release the dictionary.
if (![runningApplescriptsDict count]) {
[runningApplescriptsDict release]; runningApplescriptsDict = nil;
}
id target = [targetDict objectForKey:@"target"];
//Selector will be of the form applescriptDidRun:resultString:
SEL selector = NSSelectorFromString([targetDict objectForKey:@"selector"]);
//Notify our target
[target performSelector:selector
withObject:[targetDict objectForKey:@"userInfo"]
withObject:[userInfo objectForKey:@"resultString"]];
[targetDict release];
}
[pool release];
}
@end
|