AppleScript cURL 和解析 URL

发布于 2024-12-06 09:11:16 字数 863 浏览 2 评论 0原文

我正在开发 Automator 工作流程,我将 URL 列表传递给“运行 Applescript”,我需要获取每个页面上的内容,连接并将其传递给 BBedit(或任何其他文本编辑器)。

on run {input, parameters}

    tell application "BBEdit"
        activate

        set astid to AppleScript's text item delimiters

        set startHere to "<tbody>"
        set stopHere to "</tbody>"

        repeat with anItem in input

            set blurb0 to (do shell script "curl " & anItem)
            set AppleScript's text item delimiters to startHere
            set blurb1 to text item 2 of blurb0
            set AppleScript's text item delimiters to stopHere
            set blurb2 to text item 1 of blurb1

            set AppleScript's text item delimiters to astid

            return blurb2
            beep

        end repeat      

    end tell

end run

当前代码只能正确获取第一个 URL 中的内容。有人能解决这个问题吗?

I am working on a Automator workflow, I am passing list of URLs to the "Run Applescript" and I need to fetch the contents of on each page, concatenate and pass it to a BBedit (or any other text editor).

on run {input, parameters}

    tell application "BBEdit"
        activate

        set astid to AppleScript's text item delimiters

        set startHere to "<tbody>"
        set stopHere to "</tbody>"

        repeat with anItem in input

            set blurb0 to (do shell script "curl " & anItem)
            set AppleScript's text item delimiters to startHere
            set blurb1 to text item 2 of blurb0
            set AppleScript's text item delimiters to stopHere
            set blurb2 to text item 1 of blurb1

            set AppleScript's text item delimiters to astid

            return blurb2
            beep

        end repeat      

    end tell

end run

The current code only properly gets only the contents from first URL. Can anybody fix this?

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

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

发布评论

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

评论(2

柳絮泡泡 2024-12-13 09:11:16

这个子例程可能正是您所需要的(如果您使用的是 Safari)...

on getSource(this_URL)
    tell application "Safari"
        activate
        set the URL of the current tab of document 1 to this_URL
        set the |source| to the source of the front document
    end tell
    tell application "TextEdit"
        activate
        set the text of the front document to the source
    end tell
    quit application "Safari"
end getSource

调用它时使用:

repeat with anItem in input
    getSource(input)
end repeat

我希望这会有所帮助!

This subroutine may be what you need (if you're using Safari)...

on getSource(this_URL)
    tell application "Safari"
        activate
        set the URL of the current tab of document 1 to this_URL
        set the |source| to the source of the front document
    end tell
    tell application "TextEdit"
        activate
        set the text of the front document to the source
    end tell
    quit application "Safari"
end getSource

Call it using:

repeat with anItem in input
    getSource(input)
end repeat

I hope this helps!

挽手叙旧 2024-12-13 09:11:16

因为您处于 repeat with anItem in input 循环中,所以它会完成第一个项目的所有工作,并且 return 退出循环,实际上是整个 Automator 操作。我认为您从未从脚本中听到过 beep ;-)

另一方面,我想知道为什么您希望 BBEdit 为您完成 cUrl 和排序工作。所有被调用的处理程序都不属于 BBEdit...

我认为您的处理程序应该如下所示:

on run {input, parameters}

    -- define a few parameters
    set astid to AppleScript's text item delimiters
    set startHere to "<tbody>"
    set stopHere to "</tbody>"

    -- define a list to store all found content
    set allFoundContent to {}

    repeat with anItem in input

        set blurb0 to (do shell script "curl " & anItem)
        set AppleScript's text item delimiters to startHere
        set blurb1 to text item 2 of blurb0
        set AppleScript's text item delimiters to stopHere

        -- put the found content at the end of the list
        set end of allFoundContent to text item 1 of blurb1

        set AppleScript's text item delimiters to astid

    end repeat

    -- from here you have three possibilities:
    -- 1. return the list to next Automator action (uncomment the next line):

    -- return allFoundContent


    -- 2. concatenate the list with a delimiter you like (here return & "------" & return)
    -- and give it to your preferred text editor from this point (uncomment the next lines):

    -- set AppleScript's text item delimiters to return & "------" & return
    -- tell application "TextEdit"
    --     make new document with properties {text: allFoundContent as text}
    -- end tell
    -- set AppleScript's text item delimiters to astid


    -- 3. concatenate the list with a delimiter you like (here return & "------" & return)
    -- and give it to the next workflow step, maybe a BBEdit action waiting for a string? (uncomment the next lines):

    -- set AppleScript's text item delimiters to return & "------" & return
    -- set returnString to allFoundContent as text
    -- set AppleScript's text item delimiters to astid
    -- return returnString


    -- Next decision (for choice 1 or 2):
    -- What do you want to give to next Automator action?

    -- you can pass your input (the given URLs) (uncomment next line):
    -- return input

    -- or your result list (uncomment next line):
    -- return allFoundContent
end run

问候,迈克尔/汉堡

Because you are inside the repeat with anItem in input loop it does all your work for the first item and the return exits the loop and in fact the whole Automator action. I think you have never heard the beep from your script ;-)

On the other side I'm wondering why you want BBEdit to do the cUrl and sort work for you. All called handlers don't belong to BBEdit...

I think your handler should look like this:

on run {input, parameters}

    -- define a few parameters
    set astid to AppleScript's text item delimiters
    set startHere to "<tbody>"
    set stopHere to "</tbody>"

    -- define a list to store all found content
    set allFoundContent to {}

    repeat with anItem in input

        set blurb0 to (do shell script "curl " & anItem)
        set AppleScript's text item delimiters to startHere
        set blurb1 to text item 2 of blurb0
        set AppleScript's text item delimiters to stopHere

        -- put the found content at the end of the list
        set end of allFoundContent to text item 1 of blurb1

        set AppleScript's text item delimiters to astid

    end repeat

    -- from here you have three possibilities:
    -- 1. return the list to next Automator action (uncomment the next line):

    -- return allFoundContent


    -- 2. concatenate the list with a delimiter you like (here return & "------" & return)
    -- and give it to your preferred text editor from this point (uncomment the next lines):

    -- set AppleScript's text item delimiters to return & "------" & return
    -- tell application "TextEdit"
    --     make new document with properties {text: allFoundContent as text}
    -- end tell
    -- set AppleScript's text item delimiters to astid


    -- 3. concatenate the list with a delimiter you like (here return & "------" & return)
    -- and give it to the next workflow step, maybe a BBEdit action waiting for a string? (uncomment the next lines):

    -- set AppleScript's text item delimiters to return & "------" & return
    -- set returnString to allFoundContent as text
    -- set AppleScript's text item delimiters to astid
    -- return returnString


    -- Next decision (for choice 1 or 2):
    -- What do you want to give to next Automator action?

    -- you can pass your input (the given URLs) (uncomment next line):
    -- return input

    -- or your result list (uncomment next line):
    -- return allFoundContent
end run

Greetings, Michael / Hamburg

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