Say a Text File to a AIF

Posted on
Wed Jun 20, 2012 1:16 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Say a Text File to a AIF

Hey,

Getting nowhere, as usual, with this script to try to say a text file saving as an aif.

Code: Select all
set theFile to ":Users:TV:Music:iTunes:Indigo:Indigo Audio:Text.rtf" as alias

open for access theFile
set theText to theFile
say theText saving to ":Users:TV:Music:iTunes:Indigo:Indigo Audio:Text.aif"
close access theFile


Am I even close? Been trying lot's of variations I've found here and there but still no go.

Any help?

Thaks,

Carl

Posted on
Thu Jun 21, 2012 2:18 am
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Say a Text File to a AIF

Hi Carl.

The AppleScript "say" command can't read RTF formatted text. You'll need to open the file in TextEdit, copy the text from the file, then "say" the copied text.

Code: Select all
set theFile to ":Users:TV:Music:iTunes:Indigo:Indigo Audio:Text.rtf" as alias
tell application "TextEdit"
   open theFile
   set theText to text of document 1
   close document 1
   quit
end tell
say theText saving to ":Users:TV:Music:iTunes:Indigo:Indigo Audio:Text.aif"


That should do it.

Posted on
Thu Jun 21, 2012 10:42 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Say a Text File to a AIF

Thanks. That works fine. I did come across this bit of code that works as well.

Code: Select all
set indigoAudioFolder to (path to music folder as text) & "iTunes:Indigo:Indigo Audio:"
set plainText to do shell script "textutil -convert txt -stdout " & quoted form of (POSIX path of indigoAudioFolder & "Text.rtf")
say plainText saving to file (indigoAudioFolder & "Text.aif")


Cal

Posted on
Thu Jun 21, 2012 10:56 am
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Say a Text File to a AIF

Cool.

I'd found similar code, but didn't realize that the textutil command-line utility was now included in Mac OS X (I don't believe it was in previous versions of the OS). Using textutil is probably a more elegant way of doing it since that script wouldn't need to open a GUI application to work.

Posted on
Thu Jun 21, 2012 11:47 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Say a Text File to a AIF

...now on to the hard part...downloading an RSS news file and having Indigo
read it.

Carl

Posted on
Thu Jun 21, 2012 11:51 am
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Say a Text File to a AIF

Haha! Sounds interesting. The built-in "curl" command-line utility is your friend. :-)

Posted on
Thu Jun 21, 2012 4:35 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Say a Text File to a AIF

No doubt...now if I only knew what that was.

Carl

Posted on
Thu Jun 21, 2012 11:30 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Say a Text File to a AIF

The curl utility downloads the content of just about any URL you give it. It's particularly useful if you're downloading text-based content for further manipulation (as you intend on doing).

Posted on
Thu Jun 21, 2012 11:48 pm
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Say a Text File to a AIF

Cool..sounds like I'll be curling soon.

Carl

Posted on
Wed Jun 27, 2012 7:28 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Say a Text File to a AIF

Pretty much there with a curl download script for RSS feeds, I could post if anyone likes...is nice to have an easy way to have
news, sports etc. read by Indigo.

Can this be modified to convert a standard text file, txt, instead of an rtf, to an aif?

Code: Select all
    set indigoAudioFolder to (path to music folder as text) & "iTunes:Indigo:Indigo Audio:"
    set plainText to do shell script "textutil -convert txt -stdout " & quoted form of (POSIX path of indigoAudioFolder & "Text.rtf")
    say plainText saving to file (indigoAudioFolder & "Text.aif")


Thanks,

Carl

Posted on
Wed Jun 27, 2012 10:18 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Say a Text File to a AIF

Got the above say to file script working but I'm stuck with the curl download script to get rid of the </title></item>. at
the end of each row of text. Other than that the script is fast and works really well.

Example: Orders For Durable Goods Rose In May</title></item>.

Code: Select all
set TID to AppleScript's text item delimiters
set {startTitles, endTitles} to {{}, {}}

set FolderLocdo to quoted form of (POSIX path of (path to desktop) & "Temp Export Folder" as text)
do shell script "mkdir -p " & FolderLocdo

set xxx to do shell script "curl http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fwww.npr.org%2Frss%2Frss.php%3Fid%3D1001%22&diagnostics=true"

set AppleScript's text item delimiters to {"<item><title>", "</title></item>"}
set xxx to text items 2 thru -2 of xxx
repeat with aTitle in xxx
   if aTitle's contents is not "" then set end of endTitles to aTitle's contents
end repeat

set text item delimiters to "." & return
set endTitles to endTitles as text
set AppleScript's text item delimiters to TID

do shell script "echo " & quoted form of endTitles & " > " & FolderLocdo & "/RSS.txt"


Any help in getting the </title></item>. removed?

Many thanks,

Carl

Posted on
Wed Jun 27, 2012 11:11 am
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Say a Text File to a AIF

ckeyes888 wrote:
Got the above say to file script working but I'm stuck with the curl download script to get rid of the </title></item>. at
the end of each row of text. Other than that the script is fast and works really well.


You could probably use the command-line "sed" program (included in all UNIXes like OS X) to remove those tags from each line. Perhaps if you modified your curl command as follows...

Code: Select all
curl http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fwww.npr.org%2Frss%2Frss.php%3Fid%3D1001%22&diagnostics=true | sed "s/<\/.*//g"

The sed utility takes any input and processes it against whatever "regular expression" you give it. Regular expressions are ugly and annoying, but powerful. The regular expression in the above command tells sed to substitute ("s") the following ("/") text pattern ("<\/.*") with ("/") nothing at all ("/") and do it globally for every line ("g"). The text pattern contains a backslash ("\") to "escape" the forward slash because the forward slash is a special character to sed. The ".*" in the text pattern means "zero or more of any character other than return characters." Oh, and the vertical bar character ("|") just before "sed" tells the command-line shell to "pipe" all output from the previous command (curl in this case) to the input of the next command (sed in this case).

Hope that helps some.

Posted on
Wed Jun 27, 2012 11:18 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Say a Text File to a AIF

This is what is returned from the curl download that I need to edit:

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
yahoo:count="15" yahoo:created="2012-06-27T17:16:58Z" yahoo:lang="en-US">
<diagnostics>
<publiclyCallable>true</publiclyCallable>
<url execution-start-time="1" execution-stop-time="55"
execution-time="54" proxy="DEFAULT"><![CDATA[http://www.npr.org/rss/rss.php?id=1001]]></url>
<user-time>57</user-time>
<service-time>54</service-time>
<build-version>28595</build-version>
</diagnostics>
<results>
<item>
<title>Is That Frozen Foam On Your Beer Or Are You Just Happy To See Me?</title>
</item>
<item>
<title>A Guide To The Cleanest And Filthiest U.S. Beaches</title>
</item>
<item>
<title>ACLU Will Defend KKK In Bid To Adopt Stretch Of Ga. Highway</title>
</item>
<item>
<title>Where's The Bathroom? Half Of All State Lawmakers Will Be New On The Job</title>
</item>
<item>
<title>What Issues Really Matter To Latinos?</title>
</item>
<item>
<title>Why Flying Is No Fun (And May Be More Dangerous)</title>
</item>
<item>
<title>Violence In Syria Is As Bad As, Or Worse Than, Before Ceasefire, U.N. Says</title>
</item>
<item>
<title>Feds Move To Curb Abusive Debt Collection By Nonprofit Hospitals</title>
</item>
<item>
<title>Contracts For Home Sales Rose Sharply In May</title>
</item>
<item>
<title>City Of Stockton's Looming Bankruptcy: Pictures Tell The Story</title>
</item>
<item>
<title>Orders For Durable Goods Rose In May</title>
</item>
<item>
<title>Sign Of Peace: Queen Elizabeth Shakes Hand Of Former IRA Commander</title>
</item>
<item>
<title>On The Left, Rangel Survives; On The Right, Hatch Easily Wins</title>
</item>
<item>
<title>'Epic' Wildfire Sends Tens Of Thousands Scrambling For Safety In Colorado</title>
</item>
<item>
<title>Thousands Flee Ferocious Fire In Colorado Springs</title>
</item>
</results>
</query>

Thanks,

Carl

Posted on
Wed Jun 27, 2012 11:22 am
ckeyes888 offline
Posts: 2425
Joined: Nov 26, 2009
Location: Kalispell, MT

Re: Say a Text File to a AIF

Thanks Nathan. So something like this should work?
Can't get it to compile.

Code: Select all
set TID to AppleScript's text item delimiters
set {startTitles, endTitles} to {{}, {}}

set FolderLocdo to quoted form of (POSIX path of (path to desktop) & "Temp Export Folder" as text)
do shell script "mkdir -p " & FolderLocdo

set xxx to do shell script "curl http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fwww.npr.org%2Frss%2Frss.php%3Fid%3D1001%22&diagnostics=true | sed "s/<\/.*//g"

set AppleScript's text item delimiters to {"<item><title>", "</title></item>"}
set xxx to text items 2 thru -2 of xxx
repeat with aTitle in xxx
   if aTitle's contents is not "" then set end of endTitles to aTitle's contents
end repeat

set text item delimiters to "." & return
set endTitles to endTitles as text
set AppleScript's text item delimiters to TID

do shell script "echo " & quoted form of endTitles & " > " & FolderLocdo & "/RSS.txt"

delay 0.05

set indigoAudioFolder to (path to desktop as text) & "Temp Export Folder:"
set plainText to read file (indigoAudioFolder & "RSS.txt")
say plainText saving to file (indigoAudioFolder & "RSS.aif")


Carl

Posted on
Wed Jun 27, 2012 12:40 pm
nsheldon offline
Posts: 2469
Joined: Aug 09, 2010
Location: CA

Re: Say a Text File to a AIF

ckeyes888 wrote:
Can't get it to compile.

Ugh. Yea. When put in a "do shell script" AppleScript statement, quotes have to be escaped, and the backslashes have to be double-escaped. Also, while trying it myself I discovered that curl starts a new thread and doesn't actually send what it gets to standard output (i.e. it won't work directly with sed). So, it'll first have to save the output to a temporary text file which sed can then read. (If you save the file in /tmp, your system will automatically clean out the text file each night). Try this...

Code: Select all
set xxx to do shell script "curl -o /tmp/rssfeed.txt -s http://query.yahooapis.com/v1/public/yql?q=select%20title%20from%20rss%20where%20url%3D%22http%3A%2F%2Fwww.npr.org%2Frss%2Frss.php%3Fid%3D1001%22&diagnostics=true; sleep 3; cat /tmp/rssfeed.txt | sed -E \"s/<\\?xml.*\\?>//\" | sed -E \"s/<.?query.*\\\">//\" | sed -E \"s/<\\/title>/.  /g\" | sed -E \"s/<.?(results|title|item)>//g\" | sed -E \"s/<.?query>.*//\" | sed -E \"s/<.*//\""

It's super ugly because it pipes data through multiple instances of sed, but it worked for me when I tested it.

Who is online

Users browsing this forum: No registered users and 27 guests