问题描述
我是MacOS独立应用程序开发的新手,正在努力解决现有应用程序中的问题。我们的应用程序访问Microsoft Word文档,并通过AppleScript将文本插入到Word文档中。当我运行应用程序并通过XCode执行插入操作时,文本没有插入到Word文档中。它早些时候运行良好,但是从最近几天开始,文本没有插入到Word文档中
以下代码在XCode中用于将文本复制到粘贴板,然后调用AppeScript将文本插入Word文档中
NSPasteboard * pasteboard = [NSPasteboard generalPasteboard];
[pasteboard clearContents];
[pasteboard writeObjects:@[textToInsert]];
以下是用于将文本插入Word文档的AppleScript
on insertSampleText()
try
using terms from application "Microsoft Word"
tell application "Microsoft Word"
activate
tell application "System Events"
keystroke "v" using {command down}
end tell
end tell
end using terms from
return "0"
on error the error_message number the error_number
set the error_text to "" & the error_number
return the error_text
end try
end insertSampleText
您能否建议将文本插入Word文档需要做些什么?
解决方法
行
keystroke "v" using {command down}
除非用户在控制面板中启用了“辅助功能键盘”,否则它将无法正常工作。
假设您的代码已成功运行on insertSampleText
处理程序(我个人将在其中放置一些显示对话框语句以尝试检查执行流程),那么我将首先尝试类似的操作
tell application "Microsoft Word"
activate
paste object text object of selection
end tell
但是有效的方法可能取决于剪贴板中的内容以及现有选择中想要发生的事情
(尽管这显然与您的问题无关,但是如果您根本无法运行处理程序代码,则可能会发现Using AppleScript with Apple Events in macOS - Script not working上的讨论很有帮助)