Question

Is there any (App Store safe) way of automatically filling another app's first responder text field with text? For example, using a hot-key specified by an NSStatusBar application, a user highlights text in a separate app, like TextEdit, and pastes new text into TextEdit that the status bar app has generated based on situational data.

Was it helpful?

Solution

Is there any way of automatically filling another app's first responder text field with text?

Yes, checkout the following AppleScript:

tell application "System Events" to keystroke "Hello World"

Use NSTask to run from Objective-C:

NSString *theText = @"Hello World";
NSString *theSource = [NSString stringWithFormat:@"tell application \"System Events\" to keystroke \"%@\"",theText];
NSAppleScript *theScript = [[NSAppleScript alloc] initWithSource:theSource]; 
[theScript executeAndReturnError:nil];

Note that the script requires access for assistive devices:

System Preferences → Universal Acces → Enable access for assistive devices

Applications using the code above won't rejected by Apple.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top