diff --git a/make_game.py b/make_game.py index f1af5a172d..93e1e8447f 100644 --- a/make_game.py +++ b/make_game.py @@ -638,10 +638,14 @@ def visit(d: str, depth: int) -> None: def native_picker(): """The desktop's own folder dialog on Linux and the BSDs, where Tk's is a bare one of its own: kdialog on - KDE, zenity elsewhere (GNOME and the rest), either one when only that one is installed. Returns - (tool, path) or None: on Windows and macOS Tk already opens the system dialog, and with no display there - is nothing to open.""" - if os.name == "nt" or sys.platform == "darwin": + KDE, zenity elsewhere (GNOME and the rest), either one when only that one is installed. On macOS, osascript's + "choose folder", the system dialog with no Tk needed (Homebrew's Python ships without Tkinter). Returns + (tool, path) or None: on Windows Tk already opens the system dialog, and with no display there is nothing to + open.""" + if sys.platform == "darwin": + exe = shutil.which("osascript") + return ("osascript", exe) if exe else None + if os.name == "nt": return None if not (os.environ.get("DISPLAY") or os.environ.get("WAYLAND_DISPLAY")): return None @@ -657,6 +661,13 @@ def native_ask(tool: str, exe: str, start: str) -> str: """One folder dialog from `tool`: the chosen folder, or "" on cancel. OSError when the tool itself fails.""" if tool == "kdialog": cmd = [exe, "--title", "Leonida", "--getexistingdirectory", start] + elif tool == "osascript": + # The prompt and folder go in as arguments, not into the script text, so nothing needs quoting. Cancel is + # error -128, which osascript exits 1 with, as kdialog and zenity do. + cmd = [exe, "-e", "on run argv", + "-e", "return POSIX path of (choose folder with prompt (item 1 of argv) " + "default location (POSIX file (item 2 of argv)))", + "-e", "end run", PICK_TITLE, start] else: cmd = [exe, "--file-selection", "--directory", "--title=" + PICK_TITLE, "--filename=" + os.path.join(start, "")] @@ -669,8 +680,14 @@ def native_ask(tool: str, exe: str, start: str) -> str: def native_warn(tool: str, exe: str, msg: str) -> None: - cmd = ([exe, "--title", "Leonida", "--sorry", msg] if tool == "kdialog" - else [exe, "--warning", "--title=Leonida", "--text=" + msg]) + if tool == "osascript": + cmd = [exe, "-e", "on run argv", + "-e", 'display dialog (item 1 of argv) with title "Leonida" buttons {"OK"} default button 1 ' + "with icon caution", + "-e", "end run", msg] + else: + cmd = ([exe, "--title", "Leonida", "--sorry", msg] if tool == "kdialog" + else [exe, "--warning", "--title=Leonida", "--text=" + msg]) subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) diff --git a/tests/test_make_game_pick.py b/tests/test_make_game_pick.py index 3162de3b3f..92b95be309 100644 --- a/tests/test_make_game_pick.py +++ b/tests/test_make_game_pick.py @@ -111,15 +111,24 @@ def test_takes_whichever_is_installed(self): patch("make_game.shutil.which", side_effect=lambda t: "/usr/bin/zenity" if t == "zenity" else None): self.assertEqual(make_game.native_picker(), ("zenity", "/usr/bin/zenity")) - def test_none_without_a_display_or_on_windows_and_macos(self): + def test_none_without_a_display_or_on_windows(self): which = patch("make_game.shutil.which", return_value="/usr/bin/x") with self.env(), patch.object(make_game.os, "name", "posix"), \ patch.object(make_game.sys, "platform", "linux"), which: self.assertIsNone(make_game.native_picker()) - for name, platform in (("nt", "win32"), ("posix", "darwin")): - with self.env(DISPLAY=":0"), patch.object(make_game.os, "name", name), \ - patch.object(make_game.sys, "platform", platform), which: - self.assertIsNone(make_game.native_picker()) + with self.env(DISPLAY=":0"), patch.object(make_game.os, "name", "nt"), \ + patch.object(make_game.sys, "platform", "win32"), which: + self.assertIsNone(make_game.native_picker()) + + def test_macos_uses_osascript_with_or_without_tk(self): + # Homebrew's Python has no Tkinter; osascript's "choose folder" is the system dialog on every Mac. + with self.env(), patch.object(make_game.os, "name", "posix"), \ + patch.object(make_game.sys, "platform", "darwin"), \ + patch("make_game.shutil.which", side_effect=lambda t: "/usr/bin/osascript" if t == "osascript" else None): + self.assertEqual(make_game.native_picker(), ("osascript", "/usr/bin/osascript")) + with self.env(), patch.object(make_game.sys, "platform", "darwin"), \ + patch("make_game.shutil.which", return_value=None): + self.assertIsNone(make_game.native_picker()) class PickNativeTest(unittest.TestCase): @@ -147,6 +156,25 @@ def test_zenity_asks_again_from_the_last_folder_after_a_warning(self): self.assertIn("--warning", warn) self.assertIn("--filename=" + os.path.join(os.path.normpath("/games/wrong"), ""), ask2) + def test_osascript_passes_prompt_and_folder_as_arguments(self): + run = runs((0, "/Volumes/Games/wrong/\n"), (0, ""), (0, "/Volumes/Games/right/\n")) + got = self.pick("osascript", run, version=lambda d: VERSION if d.endswith("right") else None) + self.assertEqual(got, os.path.normpath("/Volumes/Games/right")) + ask1, warn, ask2 = (c[0][0] for c in run.call_args_list) + self.assertEqual(ask1[0], "/usr/bin/osascript") + self.assertTrue(any("choose folder" in a for a in ask1)) + self.assertEqual(ask1[-2:], [make_game.PICK_TITLE, os.path.expanduser("~")]) # data, not script text + self.assertTrue(any("display dialog" in a for a in warn)) + self.assertTrue(warn[-1].startswith(make_game.PICK_UNSUPPORTED)) + self.assertEqual(ask2[-1], os.path.normpath("/Volumes/Games/wrong")) + + def test_osascript_cancel_stops_with_a_pointer_to_game(self): + err = io.StringIO() + with redirect_stderr(err), self.assertRaises(SystemExit) as stop: + self.pick("osascript", runs((1, ""))) # error -128, user canceled + self.assertEqual(stop.exception.code, 1) + self.assertIn("--game", err.getvalue()) + def test_cancel_stops_with_a_pointer_to_game(self): err = io.StringIO() with redirect_stderr(err), self.assertRaises(SystemExit) as stop: