Como obter outro aplicativo para colar a partir do meu atalho global
Eu escrevi uma pequena ferramenta de produtividade que faz algumas manipulações de strings através da área de transferência.
Está atualmente registrando uma tecla de atalho, onde puxa o texto da área de transferência, processa-o e despeja o resultado de volta na área de transferência.
Eu tenho isso instalado no CMD + SHIFT + V
atualmente o que você precisa fazer de outra apppiclation é copy (CMD + C) e então ativar meu hothandler (CMD + SHIFT + V), e então você tem que colá-lo de volta no aplicativo original com (CMD + V).
Eu gostaria de eliminar o terceiro passo, se possível, então meu hothandler de alguma forma diz qual é o aplicativo ativo para colar.
Alguma sugestão de como fazer isso?
Meu código (menos o material de substituição de texto chato real) é este:
Por favor, note que isso precisa de estrutura de carbono para manipuladores de hotkey
[EDIT:] Eu peguei o código emprestadoesta resposta no estouro de pilha para o código do manipulador de atalho.
AppDelegate.h
#import <Cocoa/Cocoa.h>
#import <Foundation/Foundation.h>
#import <Carbon/Carbon.h>
@interface AppDelegate : NSObject <NSApplicationDelegate> {
EventHotKeyRef hotKeyRef;
}
@property (assign) IBOutlet NSWindow *window;
-(IBAction) checkClipboard:(id) sender ;
@end
AppDelegate.m
#import "AppDelegate.h"
//#import "NSString+cppMacros.h" // not relevant to question
OSStatus _AppDelegateHotKeyHandler(EventHandlerCallRef nextHandler, EventRef event, void *userData) {
AppDelegate *appDel = [[NSApplication sharedApplication] delegate];
[appDel checkClipboard:nil];
return noErr;
}
@implementation AppDelegate
- (void)installHotkey {
if (!hotKeyRef) {
EventHotKeyID hotKeyId;
EventTypeSpec eventType;
eventType.eventClass = kEventClassKeyboard;
eventType.eventKind = kEventHotKeyPressed;
InstallApplicationEventHandler(&_AppDelegateHotKeyHandler, 1, &eventType, NULL, NULL);
hotKeyId.signature = 'hotk';
hotKeyId.id = 1337;
RegisterEventHotKey(kVK_ANSI_V, cmdKey + shiftKey, hotKeyId, GetApplicationEventTarget(), 0, &hotKeyRef);
NSLog(@"_AppDelegateHotKeyHandler installed");
}
}
-(void) uninstallHotkey {
if (hotKeyRef) {
UnregisterEventHotKey(hotKeyRef);
hotKeyRef = nil;
NSLog(@"_AppDelegateHotKeyHandler uninstalled");
}
}
-(IBAction) checkClipboard:(id) sender {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
NSString *text = [pasteboard stringForType:NSPasteboardTypeString];
NSLog(@"clipboard input:%@",text);
/* actual code is this: (not relevant to question)
NSString *newText = [text isMacroEncoded] ? [text macroDecodedString] : [text macroEncodedString];
*/
// demo code for question
NSString *newText = [@"Pasted:" stringByAppendingString:text];
[pasteboard declareTypes:types owner:self];
[pasteboard setString:newText forType:NSStringPboardType];
NSLog(@"clipboard output:%@",newText);
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
hotKeyRef = nil;
[self installHotkey];
}
-(void) applicationWillTerminate:(NSNotification *)notification {
[self uninstallHotkey];
}
@end