【问题标题】:AppleScript for QuickTime recording: "AppleEvent handler failed"用于 QuickTime 录制的 AppleScript:“AppleEvent 处理程序失败”
【发布时间】:2016-05-17 14:19:30
【问题描述】:

我正在使用this AppleScript,它打算在 OS X 上激活 QuickTime 并自动开始录制音频。

在Automator中运行调试时,脚本似乎突然停在"set lastLength to duration of x"这一行,并抛出错误:

“AppleEvent 处理程序失败”。

脚本可能有什么问题?

on run {input, parameters}

    tell application "QuickTime Player"
        activate
        set x to new audio recording
        start x
        delay 1
        set lastLength to duration of x
        delay 1
        set newLength to duration of x
        try
            repeat while lastLength is not equal to newLength
                delay 1
                set lastLength to newLength
                set newLength to duration of x
            end repeat
        end try --  display alert name of document 1
        set v to file of front document
        set audioNotePath to "/Users/me/Dropbox/Inbox/- Voice Memos"
        set thePath to POSIX path of audioNotePath
        set dateVariable to do shell script "date '+%m.%d.%y'"
        set timeVariable to do shell script "date +'%l.%M %p'"
        tell x to activate
        delay 1
        tell application "System Events"
            keystroke "w" using {command down}
            delay 1
            keystroke thePath
            delay 1
            keystroke return
            delay 1
            keystroke "AudioNote "
            keystroke dateVariable
            keystroke " "
            keystroke timeVariable
            delay 1
            click pop up button "Format:" of group 1 of sheet 1 of window "Untitled" of application process "QuickTime Player" of application "System Events"
            delay 1
            key code 125
            delay 1
            keystroke return
            delay 1
            keystroke return
            delay 1
            open thePath
        end tell

        return input
    end tell
end run

【问题讨论】:

  • 我同意 Jon 关于空闲处理程序和保存命令的观点。最重要的是,请注意 QT 文档属性(当前时间、数据速率、持续时间...)在录制时不可用:这是您的脚本失败的地方
  • 对不起,不小心删除了我的评论而不是编辑。正如 pbel 所说,我建议使用空闲处理程序和导出命令来保存文件,而不是运行自己的循环来确定持续时间或使用 UI 脚本进行保存。 pbell 也是正确的,您在录制时无法获取大多数 QT 文档属性,只有在您停止录制后才可以使用持续时间。

标签: macos applescript


【解决方案1】:

如果您将以下内容保存为脚本编辑器中的保持打开应用程序,当您运行该应用程序时,它将创建一个新的录音并继续检查您是否手动停止了录音。一旦你停止录制,它将导出文件,打开它,然后退出。如果您真的希望它作为 Automator 操作,只需让 Automator 操作打开脚本应用程序。

property savePath : "Desktop:" --update e.g.,: "Dropbox:Inbox:- Voice Memos:"
property defaultName : "AudioNote"

my start_recording()

on start_recording()
    tell application "QuickTime Player"
        activate
        tell (new audio recording) to start
    end tell
end start_recording

on export_recording()
    set homeFolder to (path to home folder) as string
    set dateTimeStamp to do shell script "date '+%m%d%y-%H%M%S'"
    set audioNotePath to (homeFolder & savePath & defaultName & " " & dateTimeStamp & ".m4a")
    tell application "QuickTime Player"
        tell document 1
            export in file audioNotePath using settings preset "Audio Only"
            close saving no
        end tell
    end tell
    return audioNotePath
end export_recording

on open_recording(audioNotePath)
    tell application "QuickTime Player" --you could change this if you wanted to open the file in another app
        repeat --keep trying until the file is exported and ready to be opened
            try
                delay 1
                open file audioNotePath
                exit repeat
            end try
        end repeat
    end tell
end open_recording

on idle
    try
        tell application "QuickTime Player" to set documentName to name of document 1
        if (documentName ≠ "Audio Recording") then
            set audioNotePath to my export_recording()
            my open_recording(audioNotePath)
            quit
        end if
    end try
    return 1
end idle

【讨论】:

  • 非常感谢!这帮助我完成了我想要完成的事情。
猜你喜欢
  • 1970-01-01
  • 2015-11-04
  • 2012-02-09
  • 1970-01-01
  • 2011-10-19
  • 1970-01-01
  • 2014-05-05
  • 2011-07-15
  • 1970-01-01
相关资源
最近更新 更多