Applescript 如果已在Mac上运行,则结束服务

Applescript 如果已在Mac上运行,则结束服务,applescript,automator,Applescript,Automator,我正在尝试创建一个自动机服务,允许我说选定的文本 我希望能够使用键盘快捷键,但是,我也希望使用键盘快捷键在服务完成之前结束服务 我不知道如何使服务在开始运行后停止 以下是我在Automator中的applescript代码: 我知道你可以在系统prefs中说文本。但它的最高时速是每分钟300英里。我需要这个做300多个。因此,自动机服务 谢谢你的帮助 基本上你必须停止语音合成过程 try set thePID to word 1 of (do shell script "/bin/ps

我正在尝试创建一个自动机服务,允许我说选定的文本

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

我不知道如何使服务在开始运行后停止

以下是我在Automator中的applescript代码:

我知道你可以在系统prefs中说文本。但它的最高时速是每分钟300英里。我需要这个做300多个。因此,自动机服务


谢谢你的帮助

基本上你必须停止语音合成过程

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
另一种方法应该是什么也不说


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

通过获取服务的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

谢谢如果没有进程在运行,我能让它说话吗?如果已经有进程在运行,我能让它杀死吗?我想我是在问这个try块是否返回我可以在if语句中使用的任何东西。我需要一个键盘命令来开始或停止说话。我明白你想要什么。不幸的是,当您终止进程时,它实际上并没有结束进程。演讲将停止,但进程将继续运行。因此,检查过程是否存在不会告诉您是否有语音发生。我想不出一个方法来知道演讲是否正在进行。我先尝试了这个方法,但它并没有停止当前的演讲。它所做的只是确定新阶段何时开始。例如,如果为false,则新语音将等待当前语音结束后再讲话。如果是真的,它不会等待。但无论哪种情况,它都不会停止当前的演讲。是的,的确,字典撒谎了:-(
say "" stopping current speech true
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