AppleScript say command sometimes produces a file with no data
Problem
AppleScript produces an aiff file with no sound and zero (0) bytes. I am looping through many lists of text and using three different voices. When I run my script I get many files with no sound. There does not appear t be any common reason for the failure. I've tried using:
- delay n
- stopping with current voice
- wating for completion
Nothing has fixed the problem. Some of the files simply have no sound but if I say the text via my automator service then I do hear the output . So it seems that something is off in my repeat loop. Any thoughts on how to fix this (timing issue?) using AppleScript?
setalistto {}
set input to every paragraph of (readFile("textFileWithChineseWords"))
repeat with nextLine in input
if first character of nextLine is not "#" then
--^.*\s(.*)\s\[(.*)\]\s
--^.*\s(.*)\s\[
set output to (find text "^.*\\s(.*)\\s\\[(.*)\\]" in nextLine with regexp and string result)
set pinyin to (find text "\\[(.*)\\]" in nextLine with regexp and string result)
set pinyin to change "[" into "" in pinyin
set pinyin to change "]" into "" in pinyin
set pinyin to change space into "" in pinyin
set pinyin to change "," into "" in pinyin
--set pinyin to change "," into "" in pinyin
set alist to (split(output, space))
set simple to item 2 of alist
logsimple
saveAIFFFile(simple, pinyin)
--log simple
--log alist
--log pinyin
delay 0.3
end if
end repeat
on readFile(unixPath)
set foo to (open for access (POSIX file unixPath))
set txt to (read foo for (get eof foo) as «class utf8»)
close accessfoo
return txt
end readFile
on fileIsGood(filename)
set theItem to quoted form of POSIX path of filename
set output to false
--logtheItem
if exists (theItem) then
set fileSize to word 1 of (do shell script "du -sk " & theItem)
--log fileSize
if (fileSize is equal to 0) then return false
else
return false
end if
return output
end fileIsGood
on saveAIFFFile(msg, filename)
set cnfilname to ("/tmp" & filename & ".aiff") -- quoted form of POSIX path of
delay 4
set msg to change "," into ", " in msg
saymsgusing "Ting-Ting" saving toPOSIX filecnfilname with stopping current speech and waiting until completion
--say "" with stopping current speech
if (not fileIsGood(cnfilname)) then
repeat until (fileIsGood(cnfilname))
--delay 4
set msg to change "," into ", " in msg
saymsgusing "Ting-Ting" saving tocnfilname with stopping current speech and waiting until completion
end repeat
end if
end saveAIFFFile
to split(input, delimiter)
set AppleScript'stext item delimiters to delimiter
set someText to (every text item in input) as list
set AppleScript'stext item delimiters to {""} --> restore delimiters to default value
return someText
end split
MacBook Air (13-inch Mid 2012), Mac OS X (10.7.5), Love the mac (OSX 10.9+)