Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jumping between davinci resolve and the "save as..." Window. #19

Open
bretzay opened this issue Oct 31, 2023 · 6 comments
Open

Jumping between davinci resolve and the "save as..." Window. #19

bretzay opened this issue Oct 31, 2023 · 6 comments

Comments

@bretzay
Copy link

bretzay commented Oct 31, 2023

So, I've been looking at the documentation of this fantastic tool you've made and wanted to make a ahk script using UIA v2, I've been doing well, going step by steps, from starting my script by a right click, that allows me to click on "export subtitle" on Davinci Resolve, but I believe I'm blocking right there due to a big thing that I have trouble handling, even after searching on the forum and the documentation.
When I make my script click the "export subtitle", it works great, and sends me to the file explorer for me to choose where I want to export my subtitle to, and I wanted my script to click by itself on the 'save' button, and keeping a few variables like the location of the file, and I've found that it does not execute the lines coming after it clicks on "export subtitle", so I wanted to know if you have any ideas on how to go through that and to make my idea work. Thanks you very much !
(The whole idea is to export the script, and then edit it to make it all caps, and then import it back to Resolve, so the subtitles are capitalized. Of course, I am planning to finish the rest of the code myself, but I'm stuck at this windows pop up.)

Here is the code in case it helps you troubleshoot:

#Requires AutoHotkey v2.0
#include ../lib/UIA.ahk

GetDavinci() {
    static hWnd := WinExist("ahk_exe resolve.exe"), Davinci := UIA.ElementFromHandle(hWnd)
    if !WinExist(hWnd) {
        hWnd := WinExist("ahk_exe resolve.exe"), Davinci := UIA.ElementFromHandle(hWnd)
    }
    return Davinci
}
~RButton::
{
    if !(popUphWnd := WinWait("ahk_class Qt5152QWindowPopupDropShadowSaveBits", , 0.5))
        return
    ContextMenuEl := UIA.ElementFromHandle(popUphWnd)
    if !(ContextMenuEl.WaitElement({ Name: "Export Subtitle…" }))
        return
    ContextMenuEl.FindElement({ Name: "Export Subtitle…" }).Click()
    
    ; Here is the part where it stops working
    ResolveEl := UIA.ElementFromHandle("ahk_exe Resolve.exe")
    ResolveEl.WaitElement(Class = "#32770").Highlight()
}

Specification precision, I am on the latest ahk and uia versions, and running on Windows 11, in case it can be due to that. (not the ahk alpha version)

@Descolada
Copy link
Owner

Hello,
After clicking the Export Subtitle... menuitem, a new window appears named Export Subtitle. Since this isn't part of the Davinci window tree, you need to get the element for it with ElementFromHandle, similarly to getting the context menu element previously. Something like

    if !(saveAshWnd := WinWait("Export Subtitle ahk_exe resolve.exe", , 0.5))
        return
    SaveAsEl := UIA.ElementFromHandle(saveAshWnd)
    SaveAsEl.Highlight()

The tricky part will be getting the dialog to save to a specific location. I'm not sure what the dialog looks like in Win 11, but in Win 10 it would likely require clicking a button at the topmost toolbar and then waiting for an edit element to appear...

@bretzay
Copy link
Author

bretzay commented Nov 1, 2023

Well I have tried similar thing before, but even just sending a MsgBox("Test") to see if it was responding didn't make the MsgBox to appear, I tried to add it right after clicking the Export subtitle element, but the message box was appearing only after I did save, manually, the file.

So as of how you wrote the code snippet you gave me, I think it would indefinitely wait or the element Export Subtitle from the resolve.exe file, but will never find it, except if I was to open back the same page by myself (and I'm not even sure if it would work)

@Descolada
Copy link
Owner

The code snippet I gave you would not infinitely wait anything, because WinWait has the timeout argument specified. Your code snippets last working part ContextMenuEl.FindElement({ Name: "Export Subtitle…" }).Click() clicks the context menu item, which then in turn opens the "Export Subtitle" dialog. My provided snippet waits for that dialog, and once it exists then gets the element for its window and highlights it. So if you replace the non-working part of you code with my snippet then it should work.
Also, are you completely sure that MsgBox("Test") appeared after manually saving? In fact it might be that the MsgBox appeared, but subsequently the Export Subtitle dialog also opened and hid the MsgBox behind it (you'd need to move the dialog to see it).

@bretzay
Copy link
Author

bretzay commented Nov 2, 2023

So I've tried to use your snippet another time and, waiting enough time in the "Export Subtitle" window, it will show up an error message from ahk being the following :

Error: (0x80131505) 

	---- D:\4. Automation\UIA-v2-main\lib\UIA.ahk
	000: }
	5486: {
▶	5486: Return ComCall(3, this)
	5486: }
	5489: {

The current thread will exit.

Call stack:
D:\4. Automation\UIA-v2-main\lib\UIA.ahk (5486) : [ComCall] Return ComCall(3, this)
D:\4. Automation\UIA-v2-main\lib\UIA.ahk (5486) : [UIA.IUIAutomationInvokePattern.Prototype.Invoke] Return ComCall(3, this)
D:\4. Automation\UIA-v2-main\lib\UIA.ahk (2340) : [UIA.IUIAutomationElement.Prototype.Click] this.InvokePattern.Invoke()
D:\4. Automation\UIA-v2-main\Projets\Sous titres majuscules.ahk (19) : [<Hotkey>] ContextMenuEl.FindElement({ Name: "Export Subtitle…" }).Click()
> ~RButton

"Sous titres majuscules" being the name of the project I actually work on, and the line 19 being the line of when the script clicks the "Export Subtitle..." element.

If I use alt tab to check if there is any ahk window, there is none giving me an error, and none being my "test windows" that I create with MsgBox.

If I click fast enough (in something around 5 seconds) manually on the Export window to save or cancel, it will trigger the even where Winwait wasn't found, and so return, only after I click (I checked that by using a specific MsgBox() to see if it was appearing.

From all that points, I suppose that it waits for the window to be closed as a sort of "confirmation" but it means that I can't click anything on it, and it was for that I tried to come ask you for any ideas right there

@Descolada
Copy link
Owner

That's what I get for not actually testing it beforehand! You are correct, it doesn't work and hangs. The reason is that the Export Subtitle dialog is a modal window and in some broken implementations of Invoke/DoDefaultAction (which Click uses internally) these hang until the modal window closes. If Invoke is properly implemented then it should return immediately, however I've encountered this kind of "hanging" before: the same problem is discussed in the UIA-v2 thread.

As a workaround I propose to use Element.ControlClick() instead, or Element.Click("left"), to click the "Export Subtitle..." context menu item. These use AHK methods, so they can't hang (and I've verified this).
Theoretically there is an alternative of running Invoke asynchronously in another thread, which I proposed in the same thread a few posts down. Unfortunately in my testings calling Invoke seemed to hang UIA altogether, meaning the subsequent ElementFromHandle call didn't work. I tried initiating another instance of UIA altogether, but that also didn't work. Also I couldn't get the method proposed by malcev to work, since for some reason WinExist nor WinWait seemed to find the "Export Subtitle" dialog window. So I guess we are stuck with ControlClick and Click("left")...

@bretzay
Copy link
Author

bretzay commented Jun 4, 2024

Sorry, I am not a great user of github, and the tool I was using on Davinci had an update about what I wanted to do with the AHK + UIA script I was writting, so I did not try more than that as it was quite complicated to make it run, I am not sure of how exactly I should state the issue here, in anyways, I'll keep in mind that Element.ControlClick() for the next time I come back to the project to make a few new automations ! Thanks for your time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants