forked from acidprime/StatusItem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RunAppleScript.m
112 lines (91 loc) · 3.04 KB
/
RunAppleScript.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
//
// RunAppleScript.m
// StatusItem
//
// Created by Zack Smith on 7/20/11.
// Copyright 2011 318. All rights reserved.
//
#import "RunAppleScript.h"
#import "Constants.h"
@implementation RunAppleScript
#pragma mark Method Overide
- (id)init
{
[super init];
[self readInSettings];
// Set our selfs up as an observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(runAppleScript)
name:RequestAppleScriptNotification
object:nil];
return self;
}
//Our Methods
- (void)readInSettings
{
NSString *settingsPath = [mainBundle pathForResource:MySettingsFileResourceID
ofType:@"plist"];
settings = [[NSDictionary alloc] initWithContentsOfFile:settingsPath];
}
# pragma mark Apple Script Task
-(void)runAppleScript{
NSLog(@"Received Request to run AppleScript");
//Detach thread as we want things running in parralle
// Apple Script is not thread safe FYI so don't try multi run.
[ NSThread detachNewThreadSelector:@selector(runAppleScriptTask)
toTarget:self
withObject:nil];
}
-(void)runAppleScriptTask
{
mainBundle = [NSBundle bundleForClass:[self class]];
NSDictionary* errorDict;
NSAppleEventDescriptor* returnDescriptor = NULL;
// Note the change in file name from the uncompiled to compiled
NSString *scriptPath = [[NSBundle mainBundle] pathForResource: MyAppleScriptFile
ofType: @"scpt"];
NSLog(@"Found AppleScript Path:%@",scriptPath);
// Run the Apple Script
NSAppleScript *scriptObject = [[NSAppleScript alloc]initWithContentsOfURL:[NSURL fileURLWithPath: scriptPath]
error:&errorDict];
returnDescriptor = [scriptObject executeAndReturnError: &errorDict];
NSLog(@"Return Discriptor,%@",returnDescriptor);
NSString *returnValue = @"User Canceled";
NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] init];
returnValue = [ returnDescriptor stringValue];
[ returnDict setValue:returnValue forKey:@"returnValue"];
// Put the Log in the Console Log
if (errorDict) {
// Notify
[[NSNotificationCenter defaultCenter]
postNotificationName:CompletedAppleScriptNotification
object:self];
[self performSelectorOnMainThread:@selector(displayDialog:)
withObject:returnValue
waitUntilDone:false];
}
else {
// Notify
[[NSNotificationCenter defaultCenter]
postNotificationName:CompletedAppleScriptNotification
object:self];
[self performSelectorOnMainThread:@selector(displayDialog:)
withObject:returnValue
waitUntilDone:false];
}
}
-(void)displayDialog:(NSString *)message
{
// Activate Our Application
[NSApp arrangeInFront:self];
[NSApp activateIgnoringOtherApps:YES];
// Display a standard alert
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:@"OK"];
[alert setMessageText:MyScriptEndTitle];
[alert setInformativeText:message];
[alert setAlertStyle:NSWarningAlertStyle];
[alert runModal];
[alert release];
}
@end