如何在 Applescript 中获取 Word 2011 中选定的文本

问题描述 投票:0回答:3

我想从 Apple-Script 获取 Word 2011 活动文档中选定的文本。

谢谢

applescript
3个回答
0
投票

尝试:

tell application "Microsoft Word" to get selection's content

0
投票

我已经尝试过了,有效

    tell application "Microsoft Word"
        activate
        try
            set selectedText to content of text object of selection
            display dialog selectedText buttons {"OK"}
        on error
            display dialog "erreur" buttons {"OK"}
        end try
    end tell

0
投票

不幸的是,当未选择任何内容时,Office 2021 中的 Word 返回单个字符,即光标旁边的字符。这似乎是 Word 实现“获取所选内容”或“获取所选文本对象的内容”的错误。看来下面的代码可以解决这个问题:

            tell application "Microsoft Word"
                set theSelection to get selection's content as string
                tell window 1
                    tell pane 1
                        set theSelection to get selection
                        set ixBeg to selection start of theSelection
                        set ixEnd to selection end of theSelection
                    end tell
                end tell
                if ixBeg = ixEnd then
                    set theSelection to "" -- get selection's content returns character after cursor if nothing selected
                end if
            end tell
© www.soinside.com 2019 - 2024. All rights reserved.