End Service如果已在Mac上运行

时间:2014-09-30 05:38:16

标签: applescript automator

我正在尝试创建一个 Automator 服务,该服务允许我说出所选文字。

我希望能够使用键盘快捷键,但是,我还希望在完成之前使用键盘快捷键来结束服务。

我无法弄清楚如何在服务开始运行后停止服务。

以下是 Automator 中的AppleScript代码:

on run {input, parameters}

    say input using "Alex" speaking rate 400

    return input
end run

我知道你可以在系统首选项中讲文字。但它最高可以达到每周300次。我需要这个超过300.因此Automator服务。

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

另一种方式应该是开始说什么。

say "" stopping current speech true

如果执行它将停止当前的输出。

答案 1 :(得分:0)

通过获取服务的pid,只需将pid写入临时文件即可。

如果要停止发言,脚本会在临时文件中获取pid以退出此进程ID。

on run {input, parameters}
    set tFile to POSIX path of (path to temporary items as text) & "__auto_Runner_Speak_text__last_Pid__.txt"
    try
        set lastPid to word 1 of (read tFile) -- get the pid of the last speak text
        if lastPid is not "" then
            set tPids to words of (do shell script "/bin/ps -Axcro pid,command | sed -n '/Automator Runner/s/^ \\([0-9]*\\).*/\\1/p'")
            if lastPid is in tPids then
                do shell script "echo '' > " & (quoted form of tFile) & ";/bin/kill " & lastPid & " > /dev/null 2>&1 &" -- empty the file and stop the speaking
                return -- quit this service
            end if
        end if
    end try
    do shell script "echo $PPID > " & quoted form of tFile -- write the PID of this workflow to the temp file
    say input using "Alex" speaking rate 400
    do shell script "echo '' > " & quoted form of tFile -- remove the PID of this workflow in the temp file
end run

答案 2 :(得分:-1)

基本上你必须杀死语音合成过程...

try
    set thePID to word 1 of (do shell script "/bin/ps -Axcro pid,command | grep speechsynthesis")
    do shell script "kill -15 " & thePID
end try
相关问题