0

I am attempting to create an automation script/macro by going through the FreeCADGui and selecting the Button I want, like this:

def click_toolbar_button(button_name, toolbar_name):
    mw = FreeCADGui.getMainWindow()
    # workbench = FreeCADGui.activeWorkbench()
    # print(workbench.__class__.__name__)
    
    # Find the toolbar associated with the active workbench
    for tb in mw.findChildren(QtWidgets.QToolBar):
        # print("toolbar: ", tb.objectName())
        if toolbar_name in tb.objectName():
            toolbar = tb
            break
    else:
        print("Toolbar not found for the active workbench.")
    
    # Find the button by its name
    for action in toolbar.actions():
        if action.objectName() == button_name:
            button = action
            break
    else:
        print(f"Button '{button_name}' not found on the toolbar.")
        return
    
    # Trigger the button's click event
    print("triggering button")
    button.trigger()
    print("finished click toolbar button")

click_toolbar_button("Path_Job", "Project Setup")

This works however upon completion a subwindow/QDialog appears and pauses my script not allowing for the remaining lines - of which are attempting to click ok in this subwindow - to run. Remaining code:

def find_and_click_ok_button():
    print("Attempting findand click")
    subwindows = FreeCADGui.getMainWindow().findChildren(QtWidgets.QDialog)
    for subwindow in subwindows:
        if "Project Setup" in subwindow.windowTitle():
            ok_button = subwindow.findChild(QtWidgets.QPushButton, "okButton")
            if ok_button:
                ok_button.click()
                break

find_and_click_ok_button()

Image of subwindow/QDialog

I have tried searching this up but nobody seems to know what this is, or I'm using the wrong keywords. If anyone can expand my knowledge of the situation that would be great.

musicamante
  • 41,230
  • 6
  • 33
  • 58
Josh R
  • 1
  • 1
  • I'm going on a hunch and assume that the first function "stops" after calling `button.trigger()`. You'll probably need to use two QTimers, one for the clicking (`QTimer.singleShot(0, button.trigger)`) and another that cyclically calls `find_and_click_ok_button` until the dialog is found, for instance by putting `QTimer.singleShot(10, find_and_click_ok_button)` at the end of the `for` loop, and use `return` instead of `break`. – musicamante Aug 30 '23 at 16:12
  • Thanks for the response, I tried adding your loop idea, however the subwindow shown in the image doesn't conform to what I was looking for. Either it's not a QDialog or I got something else wrong but when attempting to print the each subwindow as it goes through them, windows like the 'Execute Macro' works and appears on the list. However upon opening the intended window('Create Job') the scripts looping pauses until the window is resolved. I did however find that, Path.Main.Gui.Job.Create([''], None), can be inserted into my macro as a bypass for the window. – Josh R Aug 31 '23 at 09:17
  • That blocking shouldn't happen, maybe you put the timer in the wrong place. Please update the post with your attempts based on my suggestions above. – musicamante Aug 31 '23 at 12:36

0 Answers0