From 7daa9d068201dfd0a73ea27af5ed1fe4abd2dfce Mon Sep 17 00:00:00 2001 From: Chris Dumas Date: Wed, 5 Aug 2026 13:20:35 +0000 Subject: [PATCH] Replace tkinter folder picker with PowerShell --- __init__.py | 52 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/__init__.py b/__init__.py index 6a201bc..40d3fe2 100644 --- a/__init__.py +++ b/__init__.py @@ -20,6 +20,8 @@ WEB_DIRECTORY = "./js" try: import os + import subprocess + import sys from aiohttp import web from server import PromptServer @@ -31,18 +33,50 @@ try: return web.FileResponse(path) @PromptServer.instance.routes.get("/dumas/api/pick_directory") - async def dumas_pick_directory(_request): + async def dumas_pick_directory(request): try: - import tkinter as tk - from tkinter import filedialog + if sys.platform != "win32": + return web.json_response({"ok": False, "message": "Native folder picker is only available on Windows right now."}) - root = tk.Tk() - root.withdraw() - root.attributes("-topmost", True) - selected = filedialog.askdirectory( - initialdir=str(_request.query.get("path", "") or "") or None + start_path = str(request.query.get("path", "") or "").strip() + if start_path and not os.path.isdir(start_path): + start_path = "" + + ps_lines = [ + "Add-Type -AssemblyName System.Windows.Forms", + "$dialog = New-Object System.Windows.Forms.FolderBrowserDialog", + '$dialog.Description = "Select a folder"', + "$dialog.ShowNewFolderButton = $true", + ] + if start_path: + escaped = start_path.replace("'", "''") + ps_lines.append(f"$dialog.SelectedPath = '{escaped}'") + ps_lines.extend( + [ + "if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) {", + " [Console]::OutputEncoding = [System.Text.Encoding]::UTF8", + " Write-Output $dialog.SelectedPath", + "}", + ] ) - root.destroy() + command = "; ".join(ps_lines) + result = subprocess.run( + [ + "powershell", + "-NoProfile", + "-STA", + "-ExecutionPolicy", + "Bypass", + "-Command", + command, + ], + capture_output=True, + text=True, + encoding="utf-8", + errors="replace", + check=False, + ) + selected = (result.stdout or "").strip() if not selected: return web.json_response({"ok": False, "cancelled": True}) return web.json_response({"ok": True, "path": selected})