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

Objective c 如何在Catalina(10.15)上以编程方式启动键盘?,objective-c,macos,keyboard,macos-sierra,macos-catalina,Objective C,Macos,Keyboard,Macos Sierra,Macos Catalina,我想通过macOS应用程序上的NSAppleScript启动屏幕(虚拟)键盘 下面的代码是在macOS Catalina(10.15)之前编写的。都需要 允许 NSString *theApplication = @"\"KeyboardViewer\""; NSString *thePath = @"\"/System/Library/Input Methods/KeyboardViewer.app\""; NSString *source = [NSString stringWithFor

我想通过macOS应用程序上的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];
NSString*应用程序=@“键盘查看器”;
NSString*thePath=@“\”/System/Library/Input-Methods/KeyboardViewer.app\”;
NSString*source=[NSString stringWithFormat:@“将HFSPath设置为((POSIX文件%@)作为字符串)\n\
将应用程序“系统事件”告知\n\
将isRunning设置为0<(计数(名称为%@的应用程序进程))\n\
如果正在运行,则\n\
告诉应用程序HFSPath退出\n\
其他\n\
忽略应用程序响应\n\
告诉应用程序HFSPath激活\n\
结束忽略\n\
结束如果“,路径,应用程序];
NSAppleScript*脚本=[[NSAppleScript alloc]initWithSource:source];
NSDictionary*dict=nil;
[script executeAndReturnError:&dict];
我收到一条错误消息(找不到文件“Macintosh HD:System:Library:Input Methods:KeyboardViewer.app”)


如何修复它?

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);
-(无效)启动键盘
{
NSString*应用程序=@“辅助控制”;
NSString*thePath=@“\”/System/Library/Input-Methods/Assistive-Control.app\”;
NSOperatingSystemVersion minimumSupportedOSVersion={.majorVersion=10、.minorVersion=15、.patchVersion=0};
BOOL isSupported=[NSProcessInfo.processInfo IsoperatingSystemAllestVersion:minimumSupportedOSVersion];
NSString*source=[NSString stringWithFormat:@“将HFSPath设置为((POSIX文件%@)作为字符串)\n\
将应用程序“系统事件”告知\n\
将isRunning设置为0<(计数(名称为%@的应用程序进程))\n\
如果正在运行,则\n\
告诉应用程序HFSPath退出\n\
其他\n\
忽略应用程序响应\n\
告诉应用程序HFSPath激活\n\
结束忽略\n\
结束如果“,路径,应用程序];
如果(isSupported==是)
{
来源=
@“激活应用程序\“系统首选项\”\n\
告诉应用程序“系统首选项”\n\
在窗格id“com.apple.preference.universalaccess”中显示锚定“虚拟键盘”\n\
结束告诉\n\
告诉应用程序“系统事件”\n\
告诉进程“系统首选项”\n\
如果存在(窗口1组1的选项卡组1的复选框“启用辅助功能键盘”),则\n\
单击窗口1组1的选项卡组1的复选框“启用辅助功能键盘”\n\
如果结束\n\
延迟1\n\
结束告诉\n\
结束讲述”;
}
NSAppleScript*脚本=[[[NSAppleScript alloc]initWithSource:source]autorelease];
NSDictionary*dict=nil;
[script executeAndReturnError:&dict];
//////////////////////////////////////////////////
做
{
如果(dict==nil)
{
打破
}
//////////////////////////////////////////////////
NSString*消息=[dict objectForKey:[[dict allKeys]firstObject]];

如果([消息长度]在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);
-(无效)启动键盘
{
NSString*应用程序=@“辅助控制”;
NSString*thePath=@“\”/System/Library/Input-Methods/Assistive-Control.app\”;
NSOperatingSystemVersion minimumSupportedOSVersion={.majorVersion=10、.minorVersion=15、.patchVersion=0};
BOOL isSupported=[NSProcessInfo.processInfo IsoperatingSystemAllestVersion:minimumSupportedOSVersion];
NSString*source=[NSString stringWithFormat:@“将HFSPath设置为((POSIX文件%@)作为字符串)\n\
将应用程序“系统事件”告知\n\
将isRunning设置为0<(计数(名称为%@的应用程序进程))\n\
如果正在运行,则\n\
告诉应用程序HFSPath退出\n\
其他\n\
忽略应用程序响应\n\
告诉应用程序HFSPath激活\n\
结束忽略\n\
结束如果“,路径,应用程序];
如果(isSupported==是)
{
来源=
@“激活应用程序\“系统首选项\”\n\
告诉应用程序“系统首选项”\n\
在窗格id“com.apple.preference.universalaccess”中显示锚定“虚拟键盘”\n\
结束告诉\n\
告诉应用程序“系统事件”\n\
告诉进程“系统首选项”\n\
如果存在(窗口1组1的选项卡组1的复选框“启用辅助功能键盘”),则\n\
单击窗口1组1的选项卡组1的复选框“启用辅助功能键盘”\n\
如果结束\n\
延迟1\n\
结束告诉\n\
结束讲述”;
}
NSAppleScript*脚本=[[[NSAppleScript alloc]initWithSource:source]autorelease];
NSDictionary*dict=nil;
[script executeAndReturnError:&dict];
//////////////////////////////////////////////////
做
{
如果(dict==nil)
{
打破
}
//////////////////////////////////////////////////
NSString*消息=[dict objectForKey:[[dict allKeys]firstObject]];

如果([消息长度]他们有一个新的应用程序/System/Library/Input Methods/Assistive Control.app-但您似乎无法直接启动它。它被配置为作为LaunchAgent启动。不知道如何通过编程触发它。我刚刚在这里发布了一个类似问题的答案:他们有一个新的应用程序/System/Library/Input Methods/Assistive Control.app-但看起来您无法直接启动它。它被配置为作为La启动