AppleScript cURL и анализ URL - PullRequest
       19

AppleScript cURL и анализ URL

0 голосов
/ 25 сентября 2011

Я работаю над рабочим процессом 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 голосов
/ 01 ноября 2014

Поскольку вы находитесь внутри цикла 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

Привет, Майкл / Гамбург

0 голосов
/ 25 сентября 2011

Эта подпрограмма может быть тем, что вам нужно (если вы используете 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

Я надеюсь, это поможет!*

...