• カテゴリ プログラミング の最新配信
  • RSS
  • RDF
  • ATOM

blog - AppleScript:URLで指定したテキストをTTSで読み上げ、テキストファイルとAIFFファイルに保存する

AppleScript:URLで指定したテキストをTTSで読み上げ、テキストファイルとAIFFファイルに保存する

カテゴリ : 
コンピュータ » プログラミング
執筆 : 
kuha 2012-10-27 08:11
AppleのText To Speechは,テキストファイルを人間の声で読み上げてくれるソフトウェアだが,AppleScriptを使えば、読み上げた声をaiffなどのオーディオファイルに保存することができる.

以下のスクリプトは、Web上にあるテキストをダウンロードして、テキストファイルに保存し,それをTTSで読み上げた音声をaiffファイルに保存する.
このスクリプトをTwitToSpeechというフォルダに保存し、デスクトップに置いて使用する.
処理の流れは以下の通り.

読み上げボイスは,Kathy
このスクリプトを保存するフォルダ名(TwitToSpeech)とフォルダの置き場(desktop)を指定する.
 読み上げられるテキストファイルtextPath(test.txt)と音声ファイルaiffPath(test.aiff)がこのフォルダに保存される.
tweetURLで目的とするURLを指定する。

try - end tryでは、"URL Access Scripting"を使って、tweetURLで指定したWeb上のコンテンツをテキストファイルtextPath(test.txt)に上書き保存する。

テキストファイルの中身をシェルのcatコマンドを使って、tweetWordsに入れて、TTSで読み上げた音声をオーディオファイルaiffPath(test.aiff)に保存する.

最後のコメントアウトされている行は,テキストの内容をダイアログボックスに表示させて確認するときに使う.

set voiceOfHuman to "Kathy"
set folderPath to (path to desktop folder from user domain as text) & "TwitToSpeech:"
set textPath to folderPath & "test.txt" as Unicode text
set aiffPath to folderPath & "test.aiff"
set tweetURL to "http://blossom.media.t-kougei.ac.jp/~kuha/tweet02.php?c=5&d=0"

try
	with timeout of 30 seconds
		tell application "URL Access Scripting"
			activate
			download tweetURL to file textPath replacing yes with progress
		end tell
	end timeout
on error
	display dialog "Download Error"
end try

set tweetWords to do shell script "cat " & (POSIX path of textPath)
say tweetWords using voiceOfHuman saving to aiffPath
--display dialog stringOfMessage

ただし、"URL Access Scripting" は、OSX Lionから廃止になったので,上のスクリプトが使えるのは、Snow Leopardまで.
Lion以降からは、OSXのコマンドのcurlを使って,do shell script "curl http://・・・ "などとする.

以下の例は、curlを使った例。
ちなみに、curlは、Lionだけでなく、Snow Leopard以前でも使える。

tweetURLで指定したURLの内容をcurlで、tweetWordsに入れ、write でtextPathで指定したテキストファイルに保存している。

set voiceOfHuman to "Kathy"
set folderPath to (path to desktop folder from user domain as text) & "TwitToSpeech:"
set textPath to folderPath & "test.txt" as Unicode text
set aiffPath to folderPath & "test.aiff"
set tweetURL to "http://blossom.media.t-kougei.ac.jp/~kuha/tweet02.php?c=5&d=0"

try
	with timeout of 30 seconds
		set tweetWords to do shell script "curl " & tweetURL
	end timeout
on error
	display dialog "Download Error"
end try

try
	set saveFile to open for access file textPath with write permission
	set eof of saveFile to 0
	write tweetWords to saveFile
end try
close access saveFile

say tweetWords using voiceOfHuman saving to aiffPath
--display dialog stringOfMessage

トラックバック

トラックバックpingアドレス http://www.kuhalabo.net/kxoops/modules/d3blog/tb.php/108
スポンサードリンク
検索

blogカテゴリ一覧

blogger一覧

blogアーカイブ