如何在 Catalina (10.15) 上以编程方式启动键盘?
Posted
技术标签:
【中文标题】如何在 Catalina (10.15) 上以编程方式启动键盘?【英文标题】:How could I launch keyboard programmatically on Catalina (10.15)? 【发布时间】:2020-02-19 11:41:51 【问题描述】:我想在 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)
我该如何解决?
【问题讨论】:
Same question 他们有一个新的应用程序 /System/Library/Input Methods/Assistive Control.app - 但您似乎无法直接启动它。它被配置为作为 LaunchAgent 启动。不知道如何以编程方式触发它。 我刚刚在这里发布了一个类似问题的答案:***.com/questions/58488965/… 【参考方案1】: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);
【讨论】:
以上是关于如何在 Catalina (10.15) 上以编程方式启动键盘?的主要内容,如果未能解决你的问题,请参考以下文章
旧 C++ 代码中的目录查找导致 OS X Catalina 10.15 中的目录错误
使用向后兼容的 MacOS 10.15 (Catalina) 创建 Python C 扩展 (MacOS10.9+)
将 JonesForth 移植到 macOS v10.15 (Catalina)