如何在Catalina(10.15)上以编程方式启动键盘?

时间:2019-10-23 09:05:39

标签: objective-c macos keyboard macos-sierra macos-catalina

我想通过macOS APP上的NSAppleScript启动屏幕(虚拟)键盘。

以下代码在macOS Catalina(10.15)之前运行良好。全部需要

允许。

NSString *theApplication = @"\"KeyboardViewer\"";
NSString *thePath = @"\"/System/Library/Input Methods/KeyboardViewer.app\"";

NSString *source = [NSString stringWithFormat:@"set HFSPath to ((POSIX file %@) as string)\n\
                    tell application \"System Events\" to ¬\n\
                    set isRunning to 0 < (count (application processes whose name is %@))\n\
                    if isRunning then\n\
                    tell application HFSPath to quit\n\
                    else\n\
                    ignoring application responses\n\
                    tell application HFSPath to activate\n\
                    end ignoring\n\
                    end if",thePath,theApplication];

NSAppleScript *script = [[NSAppleScript alloc] initWithSource:source];
NSDictionary  *dict   = nil;
[script executeAndReturnError:&dict];

我收到一条错误消息(找不到文件“ Macintosh HD:System:Library:Input Methods:KeyboardViewer.app)

我该如何解决?

1 个答案:

答案 0 :(得分:0)

OSX 10.15 以上没有 KeyboardViewer.app。您必须在 OSX 10.15 中启动 /System/Library/Input Methods/Assistive Control.app,

以下是我的解决方案。

- (void)launchKeyboard
{
NSString *theApplication = @"\"Assistive Control\"";
NSString *thePath = @"\"/System/Library/Input Methods/Assistive Control.app\"";

NSOperatingSystemVersion minimumSupportedOSVersion = { .majorVersion = 10, .minorVersion = 15, .patchVersion = 0 };

BOOL isSupported = [NSProcessInfo.processInfo isOperatingSystemAtLeastVersion:minimumSupportedOSVersion];


NSString *source = [NSString stringWithFormat:@"set HFSPath to ((POSIX file %@) as string)\n\
                    tell application \"System Events\" to ¬\n\
                    set isRunning to 0 < (count (application processes whose name is %@))\n\
                    if isRunning then\n\
                    tell application HFSPath to quit\n\
                    else\n\
                    ignoring application responses\n\
                    tell application HFSPath to activate\n\
                    end ignoring\n\
                    end if",thePath,theApplication];


if(isSupported==YES)
{
    source =
    @"activate application \"System Preferences\"\n\
    tell application \"System Preferences\"\n\
    reveal anchor \"Virtual_Keyboard\" in pane id \"com.apple.preference.universalaccess\"\n\
    end tell\n\
    tell application \"System Events\"\n\
    tell process \"System Preferences\"\n\
    if (exists checkbox \"Enable Accessibility Keyboard\" of tab group 1 of group 1 of window 1) then\n\
    click checkbox \"Enable Accessibility Keyboard\" of tab group 1 of group 1 of window 1\n\
    end if\n\
    delay 1\n\
    end tell\n\
    end tell";
}

NSAppleScript *script = [[[NSAppleScript alloc] initWithSource:source] autorelease];
NSDictionary  *dict   = nil;
[script executeAndReturnError:&dict];

//////////////////////////////////////////////////

do
{
    if(dict==nil)
    {
        break;
    }
    
    //////////////////////////////////////////////////
    
    NSString *message = [dict objectForKey:[[dict allKeys] firstObject]];
    
    if([message length]<=0)
    {
        break;
    }
    
    //////////////////////////////////////////////////
    
    NSAlert *alert = [[NSAlert alloc] init];
    
    if(alert==nil)
    {
        break;
    }
    
    alert.messageText = @"You have to allow automation for the app";
    [alert runModal];
    [alert release];
    
    //////////////////////////////////////////////////
    
    NSURL *url = [NSURL fileURLWithPath:@"/System/Library/PreferencePanes/Security.prefPane"];
    
    if(url==nil)
    {
        break;
    }
    
    [[NSWorkspace sharedWorkspace] openURL:url];
}
while (0);

}