Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 278 lines (188 sloc) 7.94 kb
ee99867 @ccgus oh geeze
authored
1 //
2 // JSTAppDelegate.m
3 // jstalk
4 //
5 // Created by August Mueller on 1/14/09.
6 // Copyright 2009 Flying Meat Inc. All rights reserved.
7 //
8
9 #import "JSTAppDelegate.h"
ad40277 @ccgus Exposing and testing JSTalk's setShouldLoadExtras:
authored
10 #import "JSTalk.h"
dbbb75c @ccgus Fixed some imports, and JSTTextView's line numbers can no longer be clic...
authored
11 #import "JSCocoaController.h"
ee99867 @ccgus oh geeze
authored
12
13 @interface JSTAppDelegate (PrivateStuff)
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
14 - (void)restoreWorkspace;
15 - (void)saveWorkspace;
16 - (void)loadExternalEditorPrefs;
17 - (void)updatePrefsFontField;
ee99867 @ccgus oh geeze
authored
18 @end
19
ab8a23c Added a new "Copy as Bookmarklet" menu command, which does what it sound...
August Mueller authored
20 void JSTUncaughtExceptionHandler(NSException *exception) {
21 NSLog(@"Uncaught exception: %@", exception);
22 }
23
ee99867 @ccgus oh geeze
authored
24 @implementation JSTAppDelegate
25
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
26 + (void)initialize {
ee99867 @ccgus oh geeze
authored
27
28 NSMutableDictionary *defaultValues = [NSMutableDictionary dictionary];
29 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
30
31 [defaultValues setObject:[NSNumber numberWithBool:YES] forKey:@"rememberWorkspace"];
32 [defaultValues setObject:[NSNumber numberWithBool:YES] forKey:@"clearConsoleOnRun"];
3e85be8 @ccgus Code completion is now a pref.
authored
33 [defaultValues setObject:[NSNumber numberWithBool:YES] forKey:@"codeCompletionEnabled"];
bdf823d @ccgus [NEW] Added support for "External Editors". It's small, but it's someth...
authored
34 [defaultValues setObject:@"com.apple.xcode" forKey:@"externalEditor"];
ee99867 @ccgus oh geeze
authored
35
36 [defaults registerDefaults: defaultValues];
37 [[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:defaultValues];
38 }
39
40
41 - (void)awakeFromNib {
42
43 if ([JSTPrefs boolForKey:@"rememberWorkspace"]) {
44 [self restoreWorkspace];
45 }
46
11ffa55 @ccgus [UPDATES] changed documentation and code and such, to reflect the name c...
authored
47 [JSTalk setShouldLoadJSTPlugins:YES];
ad40277 @ccgus Exposing and testing JSTalk's setShouldLoadExtras:
authored
48 [JSTalk listen];
ab8a23c Added a new "Copy as Bookmarklet" menu command, which does what it sound...
August Mueller authored
49
50 NSSetUncaughtExceptionHandler(JSTUncaughtExceptionHandler);
37c98b1 @ccgus Added a service for getting the results of a JSTalk script. Think: simp...
authored
51
52
53 // register this object to handle the services stuff.
54 [NSApp setServicesProvider:self];
55
56 // have all the services menus get updated.
57 //NSUpdateDynamicServices();
9db46e0 @ccgus Added some hooks so that extras can have their class loaded at startup, ...
authored
58
59 [JSTalk loadPlugins]; // some guys will setup custom UI in the app.
ee99867 @ccgus oh geeze
authored
60 }
61
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
62 - (IBAction)showPrefs:(id)sender {
ee99867 @ccgus oh geeze
authored
63
bdf823d @ccgus [NEW] Added support for "External Editors". It's small, but it's someth...
authored
64 [self loadExternalEditorPrefs];
51270e2 @ccgus The prefs now has the ability to set a default font.
authored
65 [self updatePrefsFontField];
bdf823d @ccgus [NEW] Added support for "External Editors". It's small, but it's someth...
authored
66
ee99867 @ccgus oh geeze
authored
67 if (![prefsWindow isVisible]) {
68 [prefsWindow center];
69 }
70
71 [prefsWindow makeKeyAndOrderFront:self];
72 }
73
74 - (void)applicationWillTerminate:(NSNotification *)aNotification {
75 [self saveWorkspace];
76 }
77
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
78 - (void)restoreWorkspace {
ee99867 @ccgus oh geeze
authored
79
80 NSArray *ar = [[NSUserDefaults standardUserDefaults] objectForKey:@"workspaceOpenDocuments"];
81
82 for (NSString *path in ar) {
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
83
84 if ([path hasSuffix:@".jstplugin"]) {
85 debug(@"Skipping %@", path);
86 continue;
87 }
88
ee99867 @ccgus oh geeze
authored
89 [[NSDocumentController sharedDocumentController] openDocumentWithContentsOfURL:[NSURL fileURLWithPath:path] display:YES error:nil];
90 }
91 }
92
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
93 - (void)saveWorkspace {
ee99867 @ccgus oh geeze
authored
94
95 NSMutableArray *openDocs = [NSMutableArray array];
96
97 for (NSDocument *doc in [[NSDocumentController sharedDocumentController] documents]) {
98
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
99 if ([doc fileURL]) {
ee99867 @ccgus oh geeze
authored
100 // saving the file alias would be better.
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
101 [openDocs addObject:[[doc fileURL] path]];
ee99867 @ccgus oh geeze
authored
102 }
103 }
104
105 [[NSUserDefaults standardUserDefaults] setObject:openDocs forKey:@"workspaceOpenDocuments"];
106 [[NSUserDefaults standardUserDefaults] synchronize];
107 }
108
bdf823d @ccgus [NEW] Added support for "External Editors". It's small, but it's someth...
authored
109
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
110 - (void)loadExternalEditorPrefs {
bdf823d @ccgus [NEW] Added support for "External Editors". It's small, but it's someth...
authored
111
112 NSString *editorId = [[NSUserDefaults standardUserDefaults] objectForKey:@"externalEditor"];
113
114 NSWorkspace *ws = [NSWorkspace sharedWorkspace];
115 NSString *appPath = [ws absolutePathForAppBundleWithIdentifier:editorId];
116 NSString *appName = nil;
117
118 if (appPath) {
119
120 NSBundle *appBundle = [NSBundle bundleWithPath:appPath];
121 NSString *bundleName = [appBundle objectForInfoDictionaryKey:@"CFBundleName"];
122
123 if (bundleName) {
124 appName = bundleName;
125 }
126 }
127
128 if (!appName) {
129 appName = @"Unknown";
130 }
131
132 [externalEditorField setStringValue:appName];
133 }
134
135 - (void)openPanelDidEndForExternalEditor:(NSOpenPanel *)sheet returnCode:(int)returnCode contextInfo:(void *)contextInfo {
136 if (returnCode) {
137
4916b19 @ccgus Fixed a bunch of string formatting errors (thanks to clang 3.1 pointing ...
authored
138 NSString *path = [[sheet URL] path];
bdf823d @ccgus [NEW] Added support for "External Editors". It's small, but it's someth...
authored
139
140 NSBundle *appBundle = [NSBundle bundleWithPath:path];
141 NSString *bundleId = [appBundle bundleIdentifier];
142
143 if (!bundleId) {
144 NSBeep();
145 NSLog(@"Could not load the bundle info for %@", bundleId);
146 return;
147 }
148
149 [[NSUserDefaults standardUserDefaults] setObject:bundleId forKey:@"externalEditor"];
150
151 [self loadExternalEditorPrefs];
152
153 }
154 }
155
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
156 - (void)chooseExternalEditor:(id)sender {
bdf823d @ccgus [NEW] Added support for "External Editors". It's small, but it's someth...
authored
157
158 NSOpenPanel *p = [NSOpenPanel openPanel];
159
160 [p setCanChooseFiles:YES];
161 [p setCanChooseDirectories:NO];
162 [p setAllowsMultipleSelection:NO];
163
164 [p beginSheetForDirectory:@"/Applications"
165 file:nil
166 types:[NSArray arrayWithObjects:@"app", @"APPL", nil]
167 modalForWindow:prefsWindow
168 modalDelegate:self
169 didEndSelector:@selector(openPanelDidEndForExternalEditor:returnCode:contextInfo:)
170 contextInfo:nil];
171 }
172
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
173 - (void)prefsChoosefont:(id)sender {
51270e2 @ccgus The prefs now has the ability to set a default font.
authored
174
175 [[NSFontManager sharedFontManager] setTarget:self];
176
177 [[NSFontManager sharedFontManager] orderFrontFontPanel:self];
178
179 }
180
181 - (void)changeFont:(id)sender {
182
183 NSFont *f = [[sender fontPanel:NO] panelConvertFont:[self defaultEditorFont]];
184
185 [self setDefaultEditorFont:f];
186
187 [self updatePrefsFontField];
188
189 [[NSNotificationCenter defaultCenter] postNotificationName:@"JSTFontChangeNotification" object:self];
190
191 }
192
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
193 - (void)updatePrefsFontField {
51270e2 @ccgus The prefs now has the ability to set a default font.
authored
194 NSFont *f = [self defaultEditorFont];
195 [prefsFontField setStringValue:[NSString stringWithFormat:@"%@ %dfp", [f fontName],(int)[f pointSize]]];
196 }
197
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
198 - (void)setDefaultEditorFont:(NSFont*)f {
51270e2 @ccgus The prefs now has the ability to set a default font.
authored
199 NSData *fontAsData = [NSArchiver archivedDataWithRootObject:f];
200 [[NSUserDefaults standardUserDefaults] setObject:fontAsData forKey: @"defaultFont"];
201 }
202
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
203 - (NSFont*)defaultEditorFont {
51270e2 @ccgus The prefs now has the ability to set a default font.
authored
204
205 NSFont *defaultFont = 0x00;
206
1d83afe @ccgus Added support for double clicking JSTalk plugins to install them.
authored
207 NSData *d = [[NSUserDefaults standardUserDefaults] objectForKey:@"defaultFont"];
51270e2 @ccgus The prefs now has the ability to set a default font.
authored
208 if (d) {
209 defaultFont = [NSUnarchiver unarchiveObjectWithData:d];
210 }
211
212 if (!defaultFont) {
213 defaultFont = [NSFont fontWithName:@"Monaco" size:10];
214 }
215
216 if (!defaultFont) {
217 defaultFont = [NSFont systemFontOfSize:12];
218 }
219
220 return defaultFont;
221 }
222
37c98b1 @ccgus Added a service for getting the results of a JSTalk script. Think: simp...
authored
223 - (void)JSCocoa:(JSCocoaController*)controller hadError:(NSString*)error onLineNumber:(NSInteger)lineNumber atSourceURL:(id)url {
224 _serviceError = [error retain];
225 }
226
227
228 - (void)runAsJSTalkScript:(NSPasteboard *)pb userData:(NSDictionary *)userData error:(NSString **)error {
229
230 _serviceError = 0x00;
231
232 // Test for strings on the pasteboard.
233 NSArray *classes = [NSArray arrayWithObject:[NSString class]];
234 NSDictionary *options = [NSDictionary dictionary];
235 if (![pb canReadObjectForClasses:classes options:options]) {
236 *error = NSLocalizedString(@"Error: couldn't read the text.", @"pboard couldn't give string.");
237 return;
238 }
239
240 NSString *result = 0x00;
241 NSString *script = [pb stringForType:NSPasteboardTypeString];
242
243 @try {
244
245 JSTalk *jstalk = [[JSTalk alloc] init];
246
247 JSCocoaController *jsController = [jstalk jsController];
248 jsController.delegate = self;
249
250 [[[NSThread currentThread] threadDictionary] setObject:jstalk forKey:@"org.jstalk.currentJSTalkContext"];
251
252 result = [[jstalk executeString:script] description];
253
254 [[[NSThread currentThread] threadDictionary] removeObjectForKey:@"org.jstalk.currentJSTalkContext"];
255
256 [jstalk release];
257
258 }
259 @catch (NSException *e) {
260 *error = [e reason];
261 return;
262 }
263
264 if (_serviceError) {
265 result = _serviceError;
266 }
267
268 [pb clearContents];
269
270 if (result) {
271 [pb writeObjects:[NSArray arrayWithObject:result]];
272 }
273
274 [_serviceError autorelease];
275 }
276
ee99867 @ccgus oh geeze
authored
277 @end
Something went wrong with that request. Please try again.