1

我正在开发一个小的AppleScript程序,它可以让你输入一行文本和一个数字,OS X 中的文本到语音功能会大声说出来,并带有一些由你输入的数字控制的原始混响。(详细研究我的代码。)

一切都很好,除了一件事。我正在尝试将您让计算机说的内容写入文本文件,然后将该文本文件加载到您编写它应该说的文本字段中。

写入部分工作得很好,它正在制作一个文本文件并将我让计算机说的任何内容放在那里。问题在于阅读。

就像现在一样,读取的部分如下所示:

try
    open for access prevSFile
    set defToSay to (read prevSFile)
end try

什么都没有发生。如果我尝试删除“尝试”,它会给我错误-500,程序停止。

这是我的代码:

--define variables
set defToSay to ""
set prevSFile to "~/library/prevSFile.txt"

--Fetch info from save file:
try
    open for access prevSFile
    set defToSay to (read prevSFile)
end try

--Display dialoges:
display dialog "What do you want to say?" default answer defToSay
set whatToSay to the text returned of the result
display dialog "How many times do you want to overlay it?" default answer "5"
set overlays to the text returned of the result

--Create/edit save file:
do shell script "cd /"
try
    do shell script "rm " & prevSFile
end try
do shell script "touch " & prevSFile
do shell script "echo " & whatToSay & " >> " & prevSFile

--Say and kill:
repeat overlays times
    tell application "Terminal"
        do script "say " & whatToSay
    end tell
    delay 0.01
end repeat
delay (length of whatToSay) / 5
do shell script "killall Terminal"
4

1 回答 1

0

你的问题就在这里。AppleScript路径不使用“/”,AppleScript 肯定不知道“~”。

"~/library/prevSFile.txt"

这是代码的那部分应该是什么......

set prevSFile to (path to home folder as text) & "Library:prevSFile.txt"
try
    set defToSay to (read file prevSFile)
end try

现在您有了 AppleScript 的正确路径,您需要修复 shell 命令的路径。请注意,您不需要每次都 rm 并触摸文件。重定向回显命令时只需使用“>”,这将覆盖文件。如果有空格,您还需要使用路径的“引用形式”。

do shell script "echo " & quoted form of whatToSay & " > " & quoted form of POSIX path of prevSFile

但是,您在该脚本中做了很多不必要的事情。以下是我将如何编写您的代码。祝你好运。

property whatToSay : ""
property numberOfTimes : 5

--Display dialoges:
display dialog "What do you want to say?" default answer whatToSay
set whatToSay to the text returned of the result

repeat
    display dialog "How many times do you want to overlay it?" default answer (numberOfTimes as text)
    try
        set numberOfTimes to the (text returned of the result) as number
        exit repeat
    on error
        display dialog "Please enter only numbers!" buttons {"OK"} default button 1
    end try
end repeat

repeat numberOfTimes times
    say whatToSay
end repeat
于 2012-09-03T20:29:14.253 回答