【问题标题】:AppleScript insert text into Microsoft Word documentAppleScript 将文本插入 Microsoft Word 文档
【发布时间】:2020-10-18 01:08:31
【问题描述】:

我是 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 文档需要做什么?

【问题讨论】:

    标签: xcode ms-word macos-mojave applescript-objc


    【解决方案1】:

    线

    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 的讨论很有帮助)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-27
      • 1970-01-01
      • 1970-01-01
      • 2011-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      相关资源
      最近更新 更多