-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStatusItemAppDelegate.m
233 lines (189 loc) · 6.45 KB
/
StatusItemAppDelegate.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
//
// StatusItemAppDelegate.m
// StatusItem
//
// Created by Zack Smith on 11/3/11.
// Copyright 2011 318. All rights reserved.
//
#import "StatusItemAppDelegate.h"
#import "Constants.h"
#import "ScriptPlugins.h"
#import "RunAppleScript.h"
@class ScriptPlugins;
@implementation StatusItemAppDelegate
@synthesize window;
//Delegate Methods
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
//Overrides
-(id)init
{
[ super init];
// Set our mainBundle
mainBundle = [NSBundle bundleForClass:[self class]];
// Read in our Settings
[ self readInSettings];
// Grab our Debug Variable
debugEnabled = [[ settings objectForKey:@"debugEnabled"] boolValue];
// Load Our Plugins
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(pluginsHaveLoaded:)
name:PluginsHaveLoadedNotification
object:nil];
plugins = [[ ScriptPlugins alloc] init];
// and Return
if (!self) return nil;
return self;
}
- (void)awakeFromNib
{
// Load the Status Item
[self createStatusItem];
scriptTimer = [[NSTimer scheduledTimerWithTimeInterval:[[settings objectForKey:@"scriptTimer"] intValue]
target:self
selector:@selector(updateScriptsOnInterval)
userInfo:nil
repeats:YES]retain];
[scriptTimer fire];
// Setup our applescript controller
if (!appleScript) {
appleScript = [[RunAppleScript alloc] init];
}
}
//Our Methods
- (void)readInSettings
{
NSString *settingsPath = [mainBundle pathForResource:MySettingsFileResourceID
ofType:@"plist"];
settings = [[NSDictionary alloc] initWithContentsOfFile:settingsPath];
}
- (void)createStatusItem
{
// Setup our status Item
statusItem = [[[NSStatusBar systemStatusBar]
statusItemWithLength:NSVariableStatusItemLength] retain];
[statusItem setHighlightMode:YES];
// We set as not enabled until the plugins have loaded.
[statusItem setEnabled:NO];
[statusItem setToolTip:[settings objectForKey:@"toolTip"]];
[statusItem setMenu:statusMenu];
// Update the menu text (if any) and set the icon
[self updateMenuText];
[self setMenuIcon];
}
- (void)updateMenuText
{
NSColor *fontColor = [NSColor blackColor];
NSDictionary *attrsDictionary =
[NSDictionary dictionaryWithObject:fontColor
forKey:NSForegroundColorAttributeName];
NSString *statusItemTitle = MyStatusText;
NSAttributedString *statusItemTitleWithColor = [[NSAttributedString alloc]
initWithString:statusItemTitle
attributes:attrsDictionary];
[statusItem setAttributedTitle:statusItemTitleWithColor];
}
- (void)setMenuIcon
{
NSString *path = [mainBundle pathForResource:MyLogo
ofType:@"png"];
menuIcon= [[NSImage alloc] initWithContentsOfFile:path];
[statusItem setImage:menuIcon];
}
-(NSInteger)addPluginMenuHeader:(NSString *)myTitle
{
if(debugEnabled) NSLog(@"DEBUG: Adding Menu Header: %@",myTitle);
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:myTitle
action:@selector(headerMenuClicked:)
keyEquivalent:@""];
// Check where our Updates menu is and add below
updateMenuIndex = [statusMenu indexOfItem:pluginsPlaceHolder];
NSInteger menuIndex = updateMenuIndex;
if(debugEnabled) NSLog(@"DEBUG: Found Plugin Place holder at Index of %d",menuIndex);
// Check if the current index already exists
if (!currentMenuIndex){
// Find one place above th quit menu option.
if (menuIndex > 0) {
menuIndex = menuIndex -1;
}
currentMenuIndex = menuIndex;
if(debugEnabled) NSLog(@"DEBUG: Found Menu Index of %d",menuIndex);
}
// Insert the header
[statusMenu insertItem:item atIndex:currentMenuIndex];
NSInteger menuTag = menuTag + 1 * MyHeaderMenuTag;
//NSInteger sepTag = menuTag + 1;
// Set the Menu Tag
NSMenuItem *myMenuItem = [ statusMenu itemAtIndex:currentMenuIndex];
[myMenuItem setTag:menuTag];
return menuTag;
}
-(NSInteger)addPluginMenuChild:(NSString *)myTitle
withToolTip:(NSString *)myToolTip
asAlternate:(BOOL)alternate
{
if(debugEnabled) NSLog(@"DEBUG: Adding Menu Header: %@",myTitle);
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:myTitle
action:NULL keyEquivalent:@""];
// Check where our Updates menu is and add below
updateMenuIndex = [statusMenu indexOfItem:quitMenuItem];
NSInteger menuIndex = updateMenuIndex;
if(debugEnabled) NSLog(@"DEBUG: Found Updates at Index of %d",menuIndex);
// Check if the current index already exists
if (!currentMenuIndex){
// Find one place above the repair menu option.
menuIndex = menuIndex -1;
currentMenuIndex = menuIndex;
if(debugEnabled) NSLog(@"Found Menu Index of %d",menuIndex);
}
[statusMenu insertItem:item atIndex:currentMenuIndex + 1];
// Set Our Tag 100+ are Child Menu Items
NSInteger menuTag = menuTag + 1 * MyChildMenuTag;
[item setTag:menuTag];
[item setToolTip:myToolTip];
if(debugEnabled)NSLog(@"Checking is menu:%@ is alternate:%d",myTitle,alternate);
if (alternate) {
if(debugEnabled)NSLog(@"Was told YES menu: %@ is alternate: %d",myTitle,alternate);
[ item setKeyEquivalent:@""];
[ item setAlternate:YES];
[ item setKeyEquivalentModifierMask:NSAlternateKeyMask];
}
// Add The Seperator
if (currentMenuIndex == 1) {
NSInteger sepTag = sepTag + 1 * MyChildMenuTag;
NSInteger seperatorIndex = [statusMenu indexOfItem:[ statusMenu itemAtIndex:currentMenuIndex]] +1;
[statusMenu insertItem:[NSMenuItem separatorItem] atIndex:seperatorIndex];
[[statusMenu itemAtIndex:seperatorIndex] setTag:sepTag];
currentMenuIndex = currentMenuIndex +1;
}
return menuTag;
}
- (void) pluginsHaveLoaded:(NSNotification *) notification
{
// Enable the Status Menu now that plugins have loaded
[statusItem setEnabled:YES];
}
- (IBAction)headerMenuClicked:(id)sender
{
// Post notification
[[NSNotificationCenter defaultCenter]
postNotificationName:RequestAppleScriptNotification
object:self];
}
-(void)updateScriptsOnInterval
{
// This is kind of lame way to do this but It works
NSArray * menuItems = [ statusMenu itemArray];
for (NSMenuItem *menuItem in menuItems){
if ([menuItem respondsToSelector:@selector(tag)]) {
NSInteger menuTag = [menuItem tag];
if (menuTag >= MyHeaderMenuTag) {
currentMenuIndex = 1;
[statusMenu removeItem:[statusMenu itemWithTag:menuTag]];
}
}
}
[plugins runPluginScripts:self];
}
@end