【问题标题】:AppleScript / Automator : use selected text as variableAppleScript / Automator:使用选定的文本作为变量
【发布时间】:2016-09-06 13:13:52
【问题描述】:

我无法找到如何将所选文本用作 AppleScript 和 Automator 的变量。

有什么想法吗?

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

    要看看它是如何工作的,试试这个非常简单的 Automator 服务:

    在 Automator 中创建一个Service,然后选择TextEvery application作为输入。

    第一个工作流程步骤是执行 Applescript

    Applescript 的 input 参数包含选定的文本。

    将 Applescript 设置为

    on run {input, parameters}
        display dialog (input as text)
        return input
    end run
    

    保存后,只要您选择了文本,您就可以在上下文菜单中使用此操作。

    可能命名不同,我不知道英文描述。但我希望这对你来说是一个好的起点。

    玩得开心,迈克尔/汉堡

    【讨论】:

      【解决方案2】:

      对于 Applescript,它可以其他应用程序一起使用。要在应用程序中获取选定的前窗文本,Applescript 必须使用此应用程序理解/响应的语言/语法。对于非常可编写脚本、基于文本文档的应用程序,有很多相似之处,如下所示:

      tell app "xyz" to get selection of document 1
      

      但是,确实没有标准。许多应用程序的脚本字典中没有“文本选择”对象,因此您必须采取各种变通方法。请参阅以下示例:

      tell application "Safari" to set selectedText to (do JavaScript "(''+getSelection())" in document 1)
      
      tell application "System Events" to tell application process "TextEdit" to tell attribute "AXSelectedText" of text area 1 of scroll area 1 of window 1 to set selectedText to its value
      
      tell application "Microsoft Word" to set selectedText to content of text object of selection
      

      您还可以编写“系统事件”脚本来模拟 command-c 的击键以复制文本。

      tell application "System Events" to keystroke "c" using {command down}
      delay 1
      set selectedText to the clipboard
      

      如果您需要更具体的帮助,请发布您的代码并说明您正在使用的应用程序。如果它不是可编写脚本的应用程序,那么您将不得不使用最后一种方法,即调用系统事件。或者,您可以使用您也询问过的 OS X 服务。

      当您在 Automator 中创建服务时,您将创建一个服务类型的新工作流。然后,只需确保在窗口顶部显示: “服务接收选定的text”。 然后,您可以使用 Automator 操作与传递给后续操作的选定文本进行交互。 不幸的是,并非所有程序都与服务兼容。

      【讨论】:

        猜你喜欢
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 2023-03-18
        • 1970-01-01
        • 2014-02-18
        • 2016-01-11
        • 2015-06-26
        • 1970-01-01
        相关资源
        最近更新 更多