Updated key equivalents on Autotype selection window. Enter now confirms the selection

Refactored ordering application to front to not use Apple Script anymore
This commit is contained in:
michael starke
2014-08-21 15:38:33 +02:00
parent e20e6419fc
commit 4ae8350b91
2 changed files with 26 additions and 19 deletions

View File

@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES"> <document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="5056" systemVersion="13E28" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES">
<dependencies> <dependencies>
<deployment defaultVersion="1080" identifier="macosx"/> <deployment defaultVersion="1080" identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="5053"/> <plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="5056"/>
</dependencies> </dependencies>
<objects> <objects>
<customObject id="-2" userLabel="File's Owner" customClass="MPAutotypeDaemon"> <customObject id="-2" userLabel="File's Owner" customClass="MPAutotypeDaemon">
@@ -41,6 +41,9 @@ Gw
<buttonCell key="cell" type="push" title="Perform Autotype" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="AER-eU-kcu"> <buttonCell key="cell" type="push" title="Perform Autotype" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="AER-eU-kcu">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/> <behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/> <font key="font" metaFont="system"/>
<string key="keyEquivalent" base64-UTF8="YES">
DQ
</string>
</buttonCell> </buttonCell>
<connections> <connections>
<action selector="executeAutotypeWithSelectedMatch:" target="-2" id="icM-Aj-OHO"/> <action selector="executeAutotypeWithSelectedMatch:" target="-2" id="icM-Aj-OHO"/>

View File

@@ -24,8 +24,8 @@
#import "DDHotKey+Keydata.h" #import "DDHotKey+Keydata.h"
#import <Carbon/Carbon.h> #import <Carbon/Carbon.h>
NSString *const kMPWindowTitleKey = @"windowTitle"; NSString *const kMPWindowTitleKey = @"kMPWindowTitleKey";
NSString *const kMPApplciationNameKey = @"applicationName"; NSString *const kMPProcessIdentifierKey = @"kMPProcessIdentifierKey";
/* /*
Enable to activate autotype Enable to activate autotype
@@ -37,7 +37,7 @@ NSString *const kMPApplciationNameKey = @"applicationName";
@property (nonatomic, assign) BOOL enabled; @property (nonatomic, assign) BOOL enabled;
@property (nonatomic, copy) NSData *hotKeyData; @property (nonatomic, copy) NSData *hotKeyData;
@property (strong) DDHotKey *registredHotKey; @property (strong) DDHotKey *registredHotKey;
@property (copy) NSString *targetApplicationName; @property (assign) pid_t targetPID;
@property (copy) NSString *targetWindowTitle; @property (copy) NSString *targetWindowTitle;
@end @end
@@ -51,6 +51,7 @@ NSString *const kMPApplciationNameKey = @"applicationName";
self = [super init]; self = [super init];
if (self) { if (self) {
_enabled = NO; _enabled = NO;
_targetPID = -1;
[self bind:NSStringFromSelector(@selector(enabled)) [self bind:NSStringFromSelector(@selector(enabled))
toObject:[NSUserDefaultsController sharedUserDefaultsController] toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyEnableGlobalAutotype] withKeyPath:[MPSettingsHelper defaultControllerPathForKey:kMPSettingsKeyEnableGlobalAutotype]
@@ -108,8 +109,8 @@ NSString *const kMPApplciationNameKey = @"applicationName";
- (void)cancelAutotypeSelection:(id)sender { - (void)cancelAutotypeSelection:(id)sender {
[self.matchSelectionWindow orderOut:sender]; [self.matchSelectionWindow orderOut:sender];
if(self.targetApplicationName) { if(self.targetPID) {
[MPAutotypeDaemon _orderApplicationToFront:self.targetApplicationName]; [MPAutotypeDaemon _orderApplicationToFront:self.targetPID];
} }
} }
@@ -175,7 +176,12 @@ NSString *const kMPApplciationNameKey = @"applicationName";
} }
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSArray *commands = [MPAutotypeCommand commandsForContext:context]; NSArray *commands = [MPAutotypeCommand commandsForContext:context];
[MPAutotypeDaemon _orderApplicationToFront:self.targetApplicationName]; if([MPAutotypeDaemon _orderApplicationToFront:self.targetPID]) {
/* Sleep a bit after the app was activated */
NSLog(@"App wasn frontmost, did order it there. Wating a bit.");
usleep(1000*500);
NSLog(@"Done waiting.");
}
BOOL lastCommandWasPaste = NO; BOOL lastCommandWasPaste = NO;
for(MPAutotypeCommand *command in commands) { for(MPAutotypeCommand *command in commands) {
if(lastCommandWasPaste) { if(lastCommandWasPaste) {
@@ -214,7 +220,6 @@ NSString *const kMPApplciationNameKey = @"applicationName";
- (NSDictionary *)_frontMostApplicationInfoDict { - (NSDictionary *)_frontMostApplicationInfoDict {
NSRunningApplication *frontApplication = [[NSWorkspace sharedWorkspace] frontmostApplication]; NSRunningApplication *frontApplication = [[NSWorkspace sharedWorkspace] frontmostApplication];
NSString *name = frontApplication.localizedName;
NSArray *currentWindows = CFBridgingRelease(CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID)); NSArray *currentWindows = CFBridgingRelease(CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID));
for(NSDictionary *windowDict in currentWindows) { for(NSDictionary *windowDict in currentWindows) {
@@ -226,7 +231,7 @@ NSString *const kMPApplciationNameKey = @"applicationName";
if(processId && [processId isEqualToNumber:@(frontApplication.processIdentifier)]) { if(processId && [processId isEqualToNumber:@(frontApplication.processIdentifier)]) {
return @{ return @{
kMPWindowTitleKey: windowDict[(NSString *)kCGWindowName], kMPWindowTitleKey: windowDict[(NSString *)kCGWindowName],
kMPApplciationNameKey : name kMPProcessIdentifierKey : processId
}; };
} }
} }
@@ -271,15 +276,14 @@ NSString *const kMPApplciationNameKey = @"applicationName";
#pragma mark - #pragma mark -
#pragma mark Application information #pragma mark Application information
+ (void)_orderApplicationToFront:(NSString *)applicationName { + (BOOL)_orderApplicationToFront:(pid_t)processIdentifier {
//NSLog(@"Moving %@ to the front.", applicationName); NSRunningApplication *runingApplication = [NSRunningApplication runningApplicationWithProcessIdentifier:processIdentifier];
NSString *appleScript = [[NSString alloc] initWithFormat:@"activate application \"%@\"", applicationName]; NSRunningApplication *frontApplication = [[NSWorkspace sharedWorkspace] frontmostApplication];
NSAppleScript *script = [[NSAppleScript alloc] initWithSource:appleScript]; if(frontApplication.processIdentifier == processIdentifier) {
NSDictionary *error; return NO;
NSAppleEventDescriptor *descriptor = [script executeAndReturnError:&error];
if(!descriptor) {
NSLog(@"Error trying to execure %@: %@", script, error);
} }
[runingApplication activateWithOptions:0];
return YES;
} }
- (void)_updateTargetApplicationAndWindow { - (void)_updateTargetApplicationAndWindow {
@@ -288,7 +292,7 @@ NSString *const kMPApplciationNameKey = @"applicationName";
Start searching the db for the best fit (based on title, then on window associations Start searching the db for the best fit (based on title, then on window associations
*/ */
NSDictionary *frontApplicationInfoDict = [self _frontMostApplicationInfoDict]; NSDictionary *frontApplicationInfoDict = [self _frontMostApplicationInfoDict];
self.targetApplicationName = frontApplicationInfoDict[kMPApplciationNameKey]; self.targetPID = [frontApplicationInfoDict[kMPProcessIdentifierKey] intValue];
self.targetWindowTitle = frontApplicationInfoDict[kMPWindowTitleKey]; self.targetWindowTitle = frontApplicationInfoDict[kMPWindowTitleKey];
} }