使用 applescript 处理 automator 中选定文本的列表

发布于 2024-11-14 16:28:59 字数 411 浏览 3 评论 0原文

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

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

on run {input, parameters}

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

        repeat with URL in input
        set this_URL to URL

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

While trying to make an automator action that opens multiple tabs from selected text as input I ran into an applescript issue I wasn't able to solve for awhile. This includes the answer and I'm posting this here because I just wasn't able to find documentation on how to handle data in "input" for a receives selected "text" in "any application" automator action, everything is for files which comes in as a list already.

When putting an applescript action in, you get:

on run {input, parameters}

the problem here is that input isn't in a list format and trying to do anything with it breaks the script or throws an error. ie I can't do:

        repeat with URL in input
        set this_URL to URL

So how can I treat a list of selected text as a list of items?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放我走吧 2024-11-21 16:28:59

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

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/”和“http://stackoverflow.com/posts/”获得了上述服务。 this_URL

6318162 
6318163 
6318164

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

the solution is first treat input as a string then break apart every paragraph.

on run {input, parameters}

set inputText to input as string
set URL_list to every paragraph of inputText

Without treating input "as string" first before doing "every paragraph of" it won't work.

Here's the end working script, replace the "some_url" with your own. You'll be able to select several lines of text in an editor and treat each one as a parameter to your fixed url opening each in a new safari tab. This could be expanded upon by having each line be delimited for multiple params on the 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

As an example say you had the list and had a service of the above using "http://stackoverflow.com/posts/" & this_URL

6318162 
6318163 
6318164

you could now select them click services and choose your "StackOverflow - view questions" service and it'll append and open each one in a new safari tab. In my case I needed to verify multiple dns entries in our server as still valid and do a bunch of whois lookups.

落花随流水 2024-11-21 16:28:59

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

I was looking for the same thing, just for files as input from Automator to AppleScript.

ddowns's trick didn't work for that, but ended up using this, hope it's helpful for someone looking for solving the same issue I ran into:

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
塔塔猫 2024-11-21 16:28:59

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

  • 从文本中提取 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

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

As Hanzaplastique says, for AppleScript within Automator, you don't need the Safari AppleScript because there's an Action for that. I use the following Actions:

  • Extract URLs from Text (actually the 'Extract Data from Text' Action)
  • Run AppleScript
  • Display Webpages

I use it as a Workflow added to the Services menu so that I can right-click on selected text in an email and open multiple URLs in Safari tabs.

In particular, I get Server / WordPress updates in an email but the URLs are just the top level of the domains and I want to jump to the plugins page of WordPress. So, my AppleScript (with thanks to Hanzaplastique) is:

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

I found I needed the 'return selectedFiles'. The always mysterious (to me) text delimiters may not be necessary and come from the previous version which only pulled out a single URL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文