I want to send keystrokes to OBS to trigger scene changes. I've written a method to do this through AppleScript:
+ (void)sendKeyWithControlDown:(NSString*)key afterMSDelay:(NSInteger)delay toOBSType:(OBSType)obsType completion:(void(^)(NSDictionary *error))completion {
NSString *appName = (obsType == OBSTypeOBS) ? @"OBS" : @"RPAN Studio";
NSString *source = [NSString stringWithFormat:@"tell application \"%@\" to activate \n tell application \"System Events\" to keystroke \"%@\" using control down", appName, key];
NSAppleScript *applescript = [[NSAppleScript alloc] initWithSource:source];
NSDictionary *dict;
[applescript executeAndReturnError:&dict];
completion(dict);
}
The dictionary returns an error 1002, saying System Events got an error: <my app> is not allowed to send keystrokes
I have tried the following:
- Disabling the app sandbox (this software is only for my use anyway)
- Adding
com.apple.security.temporary-exception.apple-events
andApple Events
to the entitlements file, each with a true value - Adding
Privacy - AppleEvents Sending Usage Description
to the app's info.plist with a description. The app asks me for permission to allow this the first time I run a new build, and I agree, and the app shows inSystem Preferences > Security & Privacy
in both theAccessibility
andAutomation
sections. It is ticked in both locations. I have tried unticking and re-ticking - Cleaning and re-building
- Deleting derived data
It has worked a couple of times, but not consistently enough to work out why it stopped. I didn't make any code changes in between when it worked and stopped working.