使AutoIT等待直到Audacity完成命令

丽莎

我有一个脚本,希望用于自动执行Audacity中的流程。我进行了设置,以便我要在Audacity中执行的所有功能都是键盘快捷键(因为我不认为Audacity使用的标准窗口菜单如所需WinMenuSelectItem()),换句话说,我的整个代码由Send()命令的多个实例组成问题是AutoIT执行代码的速度太快。我尝试使用WinWait(),但是过程花费的时间可变。我也尝试过ShellExecuteWait()RunWait()有没有办法让它等到程序没有做任何事情,然后执行发送命令?这是我的一些代码

Run("C:\Program Files (x86)\Audacity\audacity.exe")
; wait until it's active
WinWaitActive("Audacity")

; get the dialogue box to go away
Send("{ENTER}")
RunWait("Audacity")

; open files
Send("^o")
RunWait("Audacity")

; open the certain file & press enter
Send("test.wav")
RunWait("Audacity")
Send("{ENTER}")
RunWait("Audacity")

; select left boundary of silence period
Send("[")
RunWait("Audacity")
Send("000000100{ENTER}")
RunWait("Audacity")

; select right boundary of silence period
Send("]")
RunWait("Audacity")
Send("200000000{ENTER}")
RunWait("Audacity")
迈克尔·希思
; Use for debugging issues. Systray icon show current line.
Opt('TrayIconDebug', 1)

; Delay default: 250s
Opt('WinWaitDelay', 400)

; Delay default: 5s
Opt('SendKeyDelay', 100)

; Path of the wav file.
$sInFile = @WorkingDir & '\test.wav'

; Optional permanent change of splash screen setting.
_SplashScreen(True)

; Run Audacity and argument of the wav file.
$iPid = Run('"C:\Program Files (x86)\Audacity\audacity.exe" "' & $sInFile & '"')

; Check if Run Audacity failed.
If @error Then
    MsgBox(0x40030, @ScriptName, 'Failed to run Audacity')
    Exit 1
EndIf

; Wait for main window to get handle. Title is the filename with no extension.
$hMainWindow = WinWait('[TITLE:test; CLASS:wxWindowNR]', '', 10)

; Check allowed timeout of window.
If Not $hMainWindow Then
    MsgBox(0x40030, @ScriptName, 'Audacity window not detected.')
    Exit 1
EndIf

; If splash screen setting not 0 then handle the window.
If _SplashScreen() Then
    AdlibRegister('_WelcomeWindow')
    WinWait('Welcome to Audacity', '', 3)
    AdlibUnRegister('_WelcomeWindow')
EndIf

; Send '[' to main window to trigger Left Boundary window.
ControlSend($hMainWindow, '', '', '[')

; Get handle of Left Boundary window.
$hMsgWindow = WinWait('Set Left Selection Boundary', '', 5)

; Check allowed timeout of window.
If Not $hMsgWindow Then
    MsgBox(0x40030, @ScriptName, 'Selection Boundary window not detected.')
    Exit 1
EndIf

; Activate window, set time and click OK.
If WinActivate($hMsgWindow) Then
    ControlSend($hMsgWindow, '', 'wxWindowNR1', '{LEFT 3}1'); 1000
    ControlClick($hMsgWindow, '', 'Button2'); OK
EndIf

; Send ']' to main window to trigger Right Boundary window.
ControlSend($hMainWindow, '', '', ']')

; Get handle of Right Boundary window.
$hMsgWindow = WinWait('Set Right Selection Boundary', '', 5)

; Check allowed timeout of window.
If Not $hMsgWindow Then
    MsgBox(0x40030, @ScriptName, 'Selection Boundary window not detected.')
    Exit 1
EndIf

; Activate window, set time and click OK.
If WinActivate($hMsgWindow) Then
    ; Audacity shows 1000 and focus is on the 1st non zero digit which is 1.
    ControlSend($hMsgWindow, '', 'wxWindowNR1', '2'); 2000
    ControlClick($hMsgWindow, '', 'Button2'); OK
EndIf

; More code to do.
Sleep(1000)

MsgBox(0x40040, @ScriptName, 'End of automation.' & @CRLF & @CRLF & _
     'You can close Audacity to finish.')

; Wait for Audacity process to close.
ProcessWaitClose($iPid)

Exit

Func _WelcomeWindow()
    ; Used by AdlibRegister to handle the Welcome window.

    ; Welcome window hides if closed so need to check if exist and is visible (2).
    If WinExists('Welcome to Audacity') Then
        If BitAND(WinGetState('Welcome to Audacity'), 2) Then
            WinClose('Welcome to Audacity')
        Else
            AdlibUnRegister('_WelcomeWindow')
        EndIf
    EndIf
EndFunc

Func _SplashScreen($bDisable = False)
    ; Write to audacity.cfg to disable splash screen.
    Local $sIniFile = @AppDataDir & '\Audacity\audacity.cfg'

    If IniRead($sIniFile, 'GUI', 'ShowSplashScreen', '1') = '1' Then
        If $bDisable Then
            ; Return 1 if ini file change is success.
            If IniWrite($sIniFile, 'GUI', 'ShowSplashScreen', '0') Then
                Return 1
            EndIf
        Else
            ; Return 1 if check for splash screen is enabled.
            Return 1
        EndIf
    EndIf
EndFunc

Opt()用于减慢窗口和发送的等待。还添加Opt('TrayIconDebug', 1)了调试功能,但是如果脚本被认为是不错的话,则可以将其删除Opt()

ControlSend()代替Send()作为ControlSend()目标窗口和控制基于标题,文本等的Opt()延迟并不需要,虽然被添加到演示使用,虽然也许可以无畏的斗争,以跟上速度AutoIt的自动化。如果可能,建议使用Control*()自动化功能。

将窗口句柄存储在变量中可以帮助保存重新键入标题的代码。WinWait()返回理想的窗口句柄,如果使用了timeout参数,则0表示未找到窗口,因此可以中止自动化。

主窗口的类名本身是不够的,因为Audacity会创建许多具有相同类名的不可见窗口。因此,标题可能也需要使用。标题可以单独使用,尽管有时以文件名命名可能不是唯一的。有关用法,请参见窗口标题和高级文本

WinActivate()在“边界”窗口上使用“边界”窗口,尽管可能不需要,因为control*()通常不需要活动窗口。相比之下,标准Msgbox可能需要处于活动状态才能接受发送给它们的消息。

ShellExecuteWait()并且RunWait()不利于自动化,因为它们会阻止脚本继续执行,直到执行完毕的过程为止。因此,请使用ShellExecute()Run()代替。RunWait("Audacity")尽管存在缺陷,但重复使用似乎无奈地纠正了行为。等待窗口出现是如何控制流程,然后执行诸如ControlCommand()可以检测控件状态的功能。

ControlClick()在按钮上使用。Button2使用Classname的CtrlID,但是如果脚本始终用于英语用户,则可以使用OK用于OK按钮的文本

ProcessWaitClose($iPid)是可选的。在脚本退出之前,等待自动退出的程序有时很有用。

启动Auda​​city后,您在代码注释“使对话框消失”。您可以在对话框或首选项->界面选项上更改设置建议禁用,因为继续处理是将来的问题。我添加了一些代码来禁用audacity.cfg文件中的设置如果不希望_SplashScreen(True)通过手动禁用或手动禁用,则该AdLibRegister('_WelcomeWindow')调用将处理关闭窗口。请注意,“欢迎”窗口不会关闭而是隐藏。

_SplashScreen(True)将启动设置更改0为禁用启动。

_SplashScreen(False)_SplashScreen()不更改设置。1如果启用了启动,则调用返回0

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章