AppleScript cURL和解析URL

时间:2011-09-25 08:10:15

标签: applescript automator osx-snow-leopard

我正在开发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中的内容。任何人都能解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

这个子程序可能就是你所需要的(如果你正在使用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

我希望这有帮助!

答案 1 :(得分:0)

因为你在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

问候,迈克尔/汉堡