Can't Destroy GUI

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
david_d
Posts: 3
Joined: 30 Jul 2021, 18:34

Can't Destroy GUI

30 Jul 2021, 18:42

I have a script that works well but won't run twice, probably because it employs a GUI. I don't get errors - it simply doesn't run again after the first go. I've tried using "Gui, Destroy" in several different places, but it never works. What am I doing wrong?

Any assistance is appreciated.

Code: Select all

; This AutoHotKey script renders a series of stems (e.g., Master Mix, Instrumental Mix,
; TV Mix, Keys, Guitars, Bass) imported into a Cubase Pro 11 project, whereby all of
; the source stems are arranged in a contiguous series of Audio Tracks and all of the
; stems are routed to a downstream "master" Group/bus that contains the insert effects
; (e.g., equalization, compression) and automation (e.g., volume fades) to be applied
; to each stem.  For mastering engineers, this eliminates the tedious chore of rendering
; a project's stems manually, one by one.
;
; To use this script, the Export Audio Mixdown window must be open (but not necessarily
; visible) with all desired parameters specified.  The following parameter values are
; important:
;
; - Channel Selection:  (e.g., Surround Out or Stereo Out)
; - Name:  (e.g., Auto_Cubase_Export)
; - Path:  (e.g., C:\Temp\Cubase\Batch)
; - Conflicts:  Create Unique Filename
; - Keep Dialog Open:  Yes
;
; The following preparations are also required:
;
; - In the Project Window, mute all Audio Tracks
; - In the Project Window, select the first track to be processed
;
; Multitasking is (mostly) safe.  You will receive a standard Windows alert tone 3
; seconds before AutoHotKey must briefly commandeer your mouse and keyboard.  Then, it
; will return you to your application.  Rendered files are stored in your chosen
; location with auto-incrementing numbers appended to each successive file.

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

+^!e::							; Shift + Ctrl + Alt + e
; Retrieve the location and ID of the "Export Audio Mixdown" window so we can reactivate it
; and press the "Export Audio" button when needed.  This will also reactivate the Project
; window, so we can automatically mute/unmute and select tracks for exporting.

WinGetActiveTitle, Win_Active_Title			; Retrieve the user's currently active window so we can restore it

Gui, Font, s10, Verdana  				; Set pop-up window's typeface and size
Gui, Add, Text, w500, % "This AutoHotKey script renders a series of stems (e.g., Master Mix, Instrumental Mix, TV Mix, Keys, Guitars, Bass) imported into a Cubase Pro 11 project, whereby all of the source stems are arranged in a contiguous series of Audio Tracks and all of the stems are routed to a downstream ""master"" Group/bus that contains the insert effects (e.g., equalization, compression) and automation (e.g., volume fades) to be applied to each stem.  For mastering engineers, this eliminates the tedious chore of rendering a project's stems manually, one by one.`n`nTo use this script, the Export Audio Mixdown window must be open (but not necessarily visible) with all desired parameters specified.  The following parameter values are important:`n`n - Channel Selection:  (e.g., Surround Out or Stereo Out)`n - Name:  (e.g., Auto_Cubase_Export)`n - Path:  (e.g., C:\Temp\Cubase\Batch)`n - Conflicts:  Create Unique Filename`n - Keep Dialog Open:  Yes`n`nThe following preparations are also required:`n`n - In the Project Window, mute all Audio Tracks`n - In the Project Window, select the first track to be processed`n`nMultitasking is (mostly) safe.  You will receive a standard Windows alert tone 3 seconds before AutoHotKey must briefly commandeer your mouse and keyboard.  Then, it will return you to your application.  Rendered files are stored in your chosen location with auto-incrementing numbers appended to each successive file."

Gui, Add, Text,, Stem track count:  			; Launch a pop-up window with a form and instructions
Gui, Add, DropDownList, vStemCount, 1||2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|20|21|22|23|24
Gui, Add, Button, , Cancel				; The label ButtonCancel (if it exists) will be run when the button is pressed.
Gui, Add, Button, default, OK  				; The label ButtonOK (if it exists) will be run when the button is pressed.
Gui, Show,, Export Stems				; Now that it's built, display the window.
return							; End of auto-execute section.  The script idles until the user does something.

ButtonOK:
Gui, Submit  						; Save the input from the user to each control's associated variable.

SetTitleMatchMode, 1					; Match the start of the window title
WinActivate, Cubase Pro Project				; Activate the "Cubase Pro Project" window
WinActivate, Export Audio Mixdown			; Activate the "Export Audio Mixdown" window (might not be necessary)
WinActivate, Cubase Pro Project				; Not sure why, but we must do this twice
WinActivate, Export Audio Mixdown			; Not sure why, but we must do this twice
WinMove, Export Audio Mixdown, , 0, 0			; Make sure the Export Audio Mixdown window is (still) positioned in the topleft corner

Sleep 500						; Sleep for 0.5 second
Send, m							; Unmute the selected track

Loop %StemCount% {
	Sleep 500					; Sleep for 0.5 second
	Click, 790 830					; Press the "Export Audio" button
	Sleep 500					; Wait 0.5 second, just in case the computer needs time to respond

	WinActivate, %Win_Active_Title%			; Reactivate the user's previously active window

	SetTitleMatchMode, 3				; Match the window title exactly
	WinWait, Perform Audio Export, , 60		; Wait for "Perform Audio Export" progress window to appear
	WinWaitClose, Perform Audio Export, , 60	; Wait for "Perform Audio Export" progress window to disappear

	SoundPlay, *48					; Warn user (Windows Exclamation tone) to stop working while script hijacks mouse
	Sleep 3000					; Give user 3 seconds to comply

	WinGetActiveTitle, Win_Active_Title		; Retrieve the user's currently active window so we can restore it

	SetTitleMatchMode, 1				; Match the start of the window title
	WinActivate, Cubase Pro Project			; Activate the "Cubase Pro Project" window
	WinActivate, Export Audio Mixdown		; Activate the "Export Audio Mixdown" window (might not be necessary)
	WinMove, Export Audio Mixdown, , 0, 0		; Make sure the Export Audio Mixdown window is (still) positioned in the topleft corner

	Sleep 500					; Sleep for 0.5 second
	Send, m						; Re-mute the selected track
	Sleep 500					; Sleep for 0.5 second
	Send, {Down}					; Select next track
	Sleep 500					; Sleep for 0.5 second
	Send, m						; Unmute the selected track
	}	

MsgBox Cubase Pro 11 has finished exporting your audio stems.
ExitApp

GuiEscape:
GuiClose:
ButtonCancel:
ExitApp
User avatar
mikeyww
Posts: 26879
Joined: 09 Sep 2014, 18:38

Re: Can't Destroy GUI

30 Jul 2021, 18:57

Does the hotkey routine finish, or is it still waiting for your audio window?

If you don't need to keep rebuilding the same GUI, then don't. You can just show the same GUI that has already been built. If you want to destroy it, then you can issue a Destroy command or Gui, New when you build it. An example is below.

Code: Select all

Gui, Font, s10
Gui, Add, Edit, vttext
Gui, Add, Button, Default, OK
Gosub, F3

F3::Gui, Show, w220, Test

ButtonOK:
Gui, Submit
MsgBox, 64, Your text, %ttext%`n`nPress F3 to show the GUI again.
GuiControl,, ttext
Return
Or:

Code: Select all

Gosub, F3

F3::
Gui, New
Gui, Font, s10
Gui, Add, Edit, vttext
Gui, Add, Button, Default, OK
Gui, Show, w220, Test
Return

ButtonOK:
Gui, Submit
MsgBox, 64, Your text, %ttext%`n`nPress F3 to show the GUI again.
Return
david_d
Posts: 3
Joined: 30 Jul 2021, 18:34

Re: Can't Destroy GUI

31 Jul 2021, 13:02

Hi Mike,

Thank you for your help.

I suspect it's finishing alright because the tasks are complete (my audio files are successfully written to disk) and the "finished" MsgBox appears before the final ExitApp.

I'll try your suggestions right now and get back.
david_d
Posts: 3
Joined: 30 Jul 2021, 18:34

Re: Can't Destroy GUI

03 Aug 2021, 12:51

Thanks again! That got me on the right track.

I'm still wrapping my head around AutoHotKey's flow control, but this seems to be working: I finished the GUI code with a simple Return, and the re-executable code with an Exit (as opposed to ExitApp).

All is good. :-)
User avatar
mikeyww
Posts: 26879
Joined: 09 Sep 2014, 18:38

Re: Can't Destroy GUI

03 Aug 2021, 13:14

Good to hear!

Regarding flow, I'd say that there are just a few things to remember overall, where people from other languages might get a bit stuck.

1. As usual, flow starts from the top, and proceeds until reaching Return or a hotkey or hotstring.
2. Showing a GUI does not stop the script. A script can "wait" for (respond to) a GUI response by moving the responding code into a routine that executes when the user acts upon the GUI.
3. Braces enclose blocks for If, Else, and some other commands, but they are not a substitute for Return in a labeled subroutine. Similarly, Return is not a substitute for braces.

From my perspective, these are the most common flow-related problems that I see in this AHK help forum, aside from bugs connected to the use of Goto.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, JoeWinograd, mikeyww, Nerafius, Spawnova and 103 guests