AppleScript/Automator:将所选文本用作变量

AppleScript/Automator:将所选文本用作变量,applescript,automator,Applescript,Automator,我无法找到如何将所选文本用作AppleScript和Automator的变量 有什么想法吗?要了解它的工作原理,请尝试这个非常简单的自动机服务: 在Automator中创建服务,并选择文本和每个应用程序作为输入 第一个工作流步骤是执行Applescript Applescript的输入参数包含所选文本 将Applescript设置为 on run {input, parameters} display dialog (input as text) return input end

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


有什么想法吗?

要了解它的工作原理,请尝试这个非常简单的自动机服务:

在Automator中创建服务,并选择文本每个应用程序作为输入

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

Applescript的
输入
参数包含所选文本

将Applescript设置为

on run {input, parameters}
    display dialog (input as text)
    return input
end run
保存后,无论何时选择文本,都可以在上下文菜单中使用此操作

也许名字不一样,我不知道英文描述。但我希望这对你们来说是一个很好的起点


祝你玩得开心,Michael/Hamburg

对于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
如果您需要更具体的帮助,请发布您的代码并指出您正在使用的应用程序。如果它不是可编写脚本的应用程序,那么您必须使用最后一个方法,即调用系统事件。或者,您也可以使用OSX服务,您也曾询问过该服务

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