문제

I am trying to create a workflow that converts a list of URLs into plain text using Instapaper, and then saves the text in text documents on my machine.

So far, I have been able to grab the list of URLs, get the title of each webpage, and convert the URLs to plain text.

I have the list of titles saved in a variable "Article Titles." The plain text of each article is then being passed from "Get Text from Webpage" to "New Text File"

I tried putting the Article Titles variable in the Save As input of the "New Text File" action, but no files are being generated (unlike when I simply entered a generic title into the Save As field. But then, all of the files generated were the same name). I suspect that I can't use a variable containing an array as a Save As input. But I'd like each new file to have it's respective name.

How can I have the action iterate over the array of titles so that each item of plain text from "Get Text from Webpage" is saved with it's title from the "Article Titles" variable?

도움이 되었습니까?

해결책

The one thing that frustrates many is the problem you have when you want to pass more than one Variable to an action. There are ways around it like saving to an external script.

But in this case a simple Applescript mixed with the bit of script @adayzdone gave you will get you what I think you want.

You just need to pass the list of URLs to this 'Run Applescript"

  on run {input, parameters}
    set docPath to POSIX path of (path to documents folder)

    repeat with i from 1 to count of items of input
        set this_item to item i of input
        set thePage to (do shell script "curl " & quoted form of this_item)
        set theTitle to docPath & "/" & (do shell script "echo " & quoted form of thePage & " | grep -o \\<title\\>.*\\</title\\> | sed -E 's/<\\/?title>//g'")
        set t_text to (do shell script "echo " & quoted form of thePage & "|textutil -format html -convert txt -stdin -output \"" & theTitle & ".txt\"")

    end repeat

end run

enter image description here


** Update for passing the text on to next action. **


This will pass a list of the text contents from all the URLS.

It will still do what the above does but will now pass a list of the text contents from all the URLS on to the next action.

I have tested it with 'Text to Speech and it reads multiple text content.

on run {input, parameters}
    set docPath to POSIX path of (path to documents folder)
    set bigList to {}
    repeat with i from 1 to count of items of input
        set this_item to item i of input
        set thePage to (do shell script "curl " & quoted form of this_item)
        set theTitle to docPath & "/" & (do shell script "echo " & quoted form of thePage & " | grep -o \\<title\\>.*\\</title\\> | sed -E 's/<\\/?title>//g'")
        set t_text to (do shell script "echo " & quoted form of thePage & "|textutil -format html -convert txt -stdin -output \"" & theTitle & ".txt\"")
        set t_text_for_action to (do shell script "echo " & quoted form of thePage & "|textutil -format html -convert txt -stdin -stdout")
        copy t_text_for_action to end of bigList
    end repeat
    return bigList --> text list can now be passed to the next action
end run

If you want to test : may I suggest a page that has a small amount of text on tit like : http://www.javascripter.net/


Update 2 - Save text to audio file using the unix command 'say'.

Ok there are a couple of things here.

1, Because of the same reason I kept everything in one script in the previous codings. I have done the same here. i.e passing the text objects and titles together to the next Action would be a pain if not impossible.

2,The script uses the unix command and it's output option to save the text as an aiff file. It also names the file by the title.

3, I had a problem where instead of saving the file it started speaking the text. ??? This turned out that the URL I was testing on (http://www.javascripter.net) had a title tag that was in caps. so the @adayzdone grep and sed part of the script was returning "" . Which threw the say command.

I fixed this by using the -i ( ignore case ) option in the grep command and using the "|" ( or) option in sed and adding a caps version of the expression.

4, The Title being returned also had other characters in it that would cause a problem with the file being saved as a recognisable file by the system due to the extension not being added.

This is fixed by a simple handler that returns the title text with allowed characters.

6,

It works.

on run {input, parameters}
    set docPath to POSIX path of (path to documents folder)
    repeat with i from 1 to count of items of input
        set this_item to item i of input
        set thePage to (do shell script "curl -A \"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.112 Safari/534.30\" " & quoted form of this_item)
        set theTitle to replaceBadChars((do shell script "echo " & quoted form of thePage & " | grep -io \\<title\\>.*\\</title\\> | sed -E 's/<\\/?title>|<\\/?TITLE>//g'"))
        set t_text_for_action to (do shell script "echo " & quoted form of thePage & "|textutil -format html -convert txt -stdin -stdout")
        do shell script "cd  " & quoted form of docPath & " ;say -o  \"" & theTitle & "\" , " & quoted form of t_text_for_action
    end repeat
end run

on replaceBadChars(TEXT_)
    log TEXT_
    set OkChars to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "_", space}
    set TEXT_ to characters of TEXT_
    repeat with i from 1 to count of items in TEXT_
        set this_char to item i of TEXT_
        if this_char is not in OkChars then
            set item i of TEXT_ to "_"
        else

        end if
    end repeat
    set TEXT_ to TEXT_ as string

    do shell script " echo " & quoted form of TEXT_
end replaceBadChars
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top