【问题标题】:Processing list of selected text in automator with applescript使用applescript在automator中处理选定文本的列表
【发布时间】:2011-06-10 19:16:11
【问题描述】:

在尝试执行从选定文本中打开多个选项卡作为输入的自动操作时,我遇到了一个 Applescript 问题,我有一段时间无法解决。这包括答案,我将其发布在这里,因为我只是无法找到有关如何处理“输入”中的数据的文档,以便在“任何应用程序”自动化操作中接收选定的“文本”,一切都适用于文件已经作为列表出现了。

当放入一个applescript动作时,你会得到:

on run {input, parameters}

这里的问题是输入不是列表格式,并且尝试对其执行任何操作都会破坏脚本或引发错误。即我做不到:

        repeat with URL in input
        set this_URL to URL

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

【问题讨论】:

    标签: applescript automator


    【解决方案1】:

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

    on run {input, parameters}
    
    set inputText to input as string
    set URL_list to every paragraph of inputText
    

    如果在执行“每一段”之前先将输入“视为字符串”,它将无法正常工作。

    这是最终的工作脚本,将“some_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/”和 this_URL 获得上述服务

    6318162 
    6318163 
    6318164
    

    您现在可以选择它们点击服务并选择您的“StackOverflow - 查看问题”服务,它会在新的 safari 选项卡中附加并打开每个服务。在我的情况下,我需要验证我们服务器中的多个 dns 条目是否仍然有效,并进行大量 whois 查找。

    【讨论】:

    • 很高兴听到您找到了解决方案。在您的第一个代码示例中,您不能将这两行组合成“将 URL_list 设置为 (inputText as string) 的每一段”吗?
    【解决方案2】:

    我一直在寻找同样的东西,只是寻找从 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
    

    【讨论】:

      【解决方案3】:

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

      • 从文本中提取 URL(实际上是“从文本中提取数据”操作)
      • 运行 AppleScript
      • 显示网页

      我将它用作添加到“服务”菜单的工作流程,以便我可以右键单击电子邮件中的选定文本并在 Safari 选项卡中打开多个 URL。

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

      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
      

      我发现我需要'return selectedFiles'。总是神秘的(对我而言)文本分隔符可能不是必需的,并且来自仅提取单个 URL 的先前版本。

      【讨论】:

        猜你喜欢
        • 2016-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-01
        • 1970-01-01
        相关资源
        最近更新 更多