使用applescript处理自动机中选定文本的列表

使用applescript处理自动机中选定文本的列表,applescript,automator,Applescript,Automator,当我试图做一个自动操作,从选定的文本中打开多个选项卡作为输入时,我遇到了一个applescript问题,我一时无法解决。这包括答案,我在这里发布这篇文章是因为我无法找到关于如何在“任何应用程序”自动机操作中接收选定“文本”的“输入”中处理数据的文档,所有内容都是针对已经作为列表出现的文件 将applescript操作放入时,您会得到: on run {input, parameters} 这里的问题是,输入不是列表格式,尝试对其执行任何操作都会破坏脚本或抛出错误。我做不到: r

当我试图做一个自动操作,从选定的文本中打开多个选项卡作为输入时,我遇到了一个applescript问题,我一时无法解决。这包括答案,我在这里发布这篇文章是因为我无法找到关于如何在“任何应用程序”自动机操作中接收选定“文本”的“输入”中处理数据的文档,所有内容都是针对已经作为列表出现的文件

将applescript操作放入时,您会得到:

on run {input, parameters}
这里的问题是,输入不是列表格式,尝试对其执行任何操作都会破坏脚本或抛出错误。我做不到:

        repeat with URL in input
        set this_URL to URL

那么,如何将选定文本列表视为项目列表呢?

解决方案是首先将输入视为字符串,然后将每个段落分开

on run {input, parameters}

set inputText to input as string
set URL_list to every paragraph of inputText
如果在执行“每一段”之前不先将输入“视为字符串”,它将无法工作

这是最后的工作脚本,将“some_url”替换为您自己的。您可以在编辑器中选择几行文本,并将每一行作为固定url的参数,在新的safari选项卡中打开每一行。这可以通过为url上的多个参数分隔每一行来扩展

on run {input, parameters}

set inputText to input as string
set URL_list to every paragraph of inputText
tell application "Safari"
    activate
    repeat with URL in URL_list
        set this_URL to URL
        # extra processing of URL could be done here for multiple params
        my new_tab()
        set tab_URL to "http://some_url.com?data=" & this_URL
        set the URL of document 1 to tab_URL
    end repeat
end tell
return input
end run

on new_tab()
    tell application "Safari" to activate
    tell application "System Events"
        tell process "Safari"
            click menu item "New Tab" of ¬
                menu "File" of menu bar 1
        end tell
    end tell
end new_tab
例如,假设您拥有列表,并使用“http://stackoverflow.com/posts/“&此URL

6318162 
6318163 
6318164

您现在可以选择它们单击服务并选择“StackOverflow-view questions”服务,它将在新的safari选项卡中追加并打开每一个服务。在我的例子中,我需要验证服务器中的多个dns条目是否仍然有效,并进行一系列whois查找

我也在寻找同样的东西,只是寻找从Automator到AppleScript的输入文件

ddowns的伎俩对此不起作用,但最终使用了这个,希望它对寻求解决我遇到的相同问题的人有所帮助:

on run {input, parameters}

    -- create empty list
    set selectedFiles to {}

    -- add each list item to the empty list
    repeat with i in input
        copy (POSIX path of i) to end of selectedFiles
    end repeat

    -- show each item (just for testing purposes of course) 
    repeat with currentFile in selectedFiles
        display dialog currentFile as text
    end repeat

end run

正如Hanzaplastique所说,对于Automator中的AppleScript,您不需要Safari AppleScript,因为它有一个操作。我使用以下操作:

  • 从文本中提取URL(实际上是“从文本中提取数据”操作)
  • 运行AppleScript
  • 显示网页
我将其用作添加到“服务”菜单的工作流,这样我就可以右键单击电子邮件中的选定文本,并在Safari选项卡中打开多个URL

特别是,我在电子邮件中收到服务器/WordPress更新,但URL只是域的顶层,我想跳转到WordPress的插件页面。因此,我的贴花(感谢Hanzaplastique)是:


我发现我需要“返回所选文件”。(对我来说)永远神秘的文本分隔符可能不是必需的,它来自以前的版本,该版本只提取了一个URL。

很高兴听到您找到了解决方案。在您的第一个代码示例中,您不能将这两行合并到“将URL\u列表设置为(inputText作为字符串)的每个段落”中吗?
on run {input, parameters}
    set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}
    -- create empty list
    set selectedFiles to {}
    -- add each list item to the empty list
    repeat with i in input
        set AppleScript's text item delimiters to {" "}
        set i to i & "/wp-admin/plugins.php"
        copy i to end of selectedFiles
    end repeat
    return selectedFiles
end run