"""Forward a script to one explicitly selected DxDesigner acceptance instance.""" import argparse from pathlib import Path import pythoncom import win32com.client import win32process def main() -> None: parser = argparse.ArgumentParser() target = parser.add_mutually_exclusive_group(required=True) target.add_argument("--pid", type=int) target.add_argument("--moniker") target.add_argument("--project", type=Path) parser.add_argument("script", type=Path) args = parser.parse_args() context = pythoncom.CreateBindCtx(0) table = pythoncom.GetRunningObjectTable() entries = table.EnumRunning() while batch := entries.Next(1): moniker = batch[0] name = moniker.GetDisplayName(context, None) if not name.startswith("!Viewdraw.Application"): continue app = win32com.client.Dispatch( table.GetObject(moniker).QueryInterface(pythoncom.IID_IDispatch) ) if args.project: try: data = app.GetProjectData() project = data.GetProjectFilePath project = project() if callable(project) else project matches = Path(project).resolve() == args.project.resolve() except Exception: matches = False elif args.moniker: matches = args.moniker.casefold() in name.casefold() else: handle = int(app.ActiveViewHandle) matches = bool(handle) and win32process.GetWindowThreadProcessId(handle)[1] == args.pid if matches: print(f"Executing {args.script.name} in {name}", flush=True) app.ExecuteCommand(f'run "{args.script.resolve()}"') return raise SystemExit("Selected DxDesigner instance was not found") if __name__ == "__main__": main()