-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQSTerminalMediator.m
More file actions
79 lines (67 loc) · 2.79 KB
/
QSTerminalMediator.m
File metadata and controls
79 lines (67 loc) · 2.79 KB
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
//
// QSTerminalMediator.m
// PlugIns
//
// Created by Nicholas Jitkoff on 9/29/04.
// Copyright 2004 __MyCompanyName__. All rights reserved.
//
#import "QSTerminalMediator.h"
@implementation QSRegistry (QSTerminalMediator)
- (NSString *)preferredTerminalMediatorID{
NSString *key=[[NSUserDefaults standardUserDefaults] stringForKey:kQSTerminalMediators];
if (![[self tableNamed:kQSTerminalMediators]objectForKey:key])key=@"com.apple.Terminal";
return key;
}
- (id <QSTerminalMediator>)preferredTerminalMediator{
id mediator=[prefInstances objectForKey:kQSTerminalMediators];
if (!mediator){
mediator=[self instanceForKey:[self preferredTerminalMediatorID]
inTable:kQSTerminalMediators];
if (mediator)
[prefInstances setObject:mediator forKey:kQSTerminalMediators];
}
return mediator;
}
@end
@implementation QSAppleTerminalMediator
- (void)performCommandInTerminal:(NSString *)command{
TerminalApplication *t = [SBApplication applicationWithBundleIdentifier:@"com.apple.Terminal"];
// when tabs are made to work in Terminal, use this (will they ever?)
/*
TerminalTab *tab = [[[[t classForScriptingClass:@"tab"] alloc] init] autorelease];
[[frontmost tabs] insertObject:tab atIndex:0];
*/
BOOL terminalRunning = [t isRunning];
[t activate];
// developer feature!
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QSTerminalUseTabs"]) {
usleep(terminalRunning ? 5000 : 200000);
TerminalWindow *frontmost = nil;
SBElementArray *windows = [t windows];
NSIndexSet *frontmostIndex = [windows indexesOfObjectsWithOptions:NSEnumerationConcurrent passingTest:^BOOL(TerminalWindow *w, NSUInteger idx, BOOL *stop) {
return w.index == 1;
}];
if ([frontmostIndex count]) {
frontmost = [windows objectAtIndex:[frontmostIndex lastIndex]];
}
if (terminalRunning && frontmost) {
// simulate CMD-T.
CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStatePrivate);
CGEventRef keyDown = CGEventCreateKeyboardEvent (source, (CGKeyCode)17, true); //T
CGEventSetFlags(keyDown, kCGEventFlagMaskCommand);
CGEventPost(kCGHIDEventTap, keyDown);
CGEventRef keyUp = CGEventCreateKeyboardEvent (source, (CGKeyCode)17, false); //T key up
CGEventSetFlags(keyUp, kCGEventFlagMaskCommand);
CGEventPost(kCGHIDEventTap, keyUp);
CFRelease(keyDown);
CFRelease(keyUp);
CFRelease(source);
usleep(5000);
}
[t doScript:command in:frontmost];
} else {
// in the future we should be able to use 'in:tab' to do the script in our new tab... if/when Tabs work in AS/SB
[t doScript:command in:nil];
}
}
@end