0

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 and Apple 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 in System Preferences > Security & Privacy in both the Accessibility and Automation 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.

  • Does this answer your question? [AppleScript Application is not allowed to send keystrokes](https://stackoverflow.com/questions/54973241/applescript-application-is-not-allowed-to-send-keystrokes) – wp78de Oct 30 '20 at 22:28

1 Answers1

0

It seems unticking and re-ticking the permission in System Preferences, and I forgot you can actually remove them from the list. I think it had stored the permission for a different build, and was confused about what app I had allowed permission. After removing it and re-adding it, it worked.