Created
April 2, 2026 08:42
-
-
Save ewerybody/dd0ef1b540647bdd2d88466d1133a6bd to your computer and use it in GitHub Desktop.
Open File Browser - InkScape Extension to show current SVG document in Nemo, Explorer, Finder ...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="UTF-8"?> | |
| <inkscape-extension xmlns="http://www.inkscape.org/namespace/inkscape/extension"> | |
| <name>Open File Browser</name> | |
| <id>org.inkscape.effect.open_filebrowser</id> | |
| <effect> | |
| <object-type>all</object-type> | |
| <effects-menu> | |
| <submenu name="Document"/> | |
| </effects-menu> | |
| </effect> | |
| <script> | |
| <command location="inx" interpreter="python">open_filebrowser.py</command> | |
| </script> | |
| </inkscape-extension> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| Open File Browser - Inkscape Effect Extension | |
| Tries opening up system file browser with current SVG document file selected. | |
| """ | |
| import sys | |
| import subprocess | |
| from pathlib import Path | |
| import inkex | |
| class OpenFileBrowser(inkex.EffectExtension): | |
| def effect(self): | |
| document_path = Path(self.document_path()) | |
| if not document_path.is_file(): | |
| inkex.errormsg( | |
| f"Source file not found: {document_path}\nSave the document first!" | |
| ) | |
| return | |
| if sys.platform == "win32": | |
| win_shell_open(document_path) | |
| return | |
| if sys.platform == "darwin": | |
| mac_show_in_finder(document_path) | |
| return | |
| if sys.platform == "linux": | |
| linux_show_in_filemanager(document_path) | |
| def win_shell_open(path: Path): | |
| """On Windows make use of `SHOpenFolderAndSelectItems` to open existing | |
| Explorer window if any or open a fresh one. | |
| Faster and resource friendly. | |
| """ | |
| import ctypes | |
| if not isinstance(path, str): | |
| path = str(path) | |
| shell32 = ctypes.windll.shell32 | |
| ole32 = ctypes.windll.ole32 | |
| # CRITICAL: declare types or ctypes truncates 64-bit pointers to 32-bit | |
| shell32.ILCreateFromPathW.restype = ctypes.c_void_p | |
| shell32.ILCreateFromPathW.argtypes = [ctypes.c_wchar_p] | |
| shell32.ILFree.restype = None | |
| shell32.ILFree.argtypes = [ctypes.c_void_p] | |
| shell32.SHOpenFolderAndSelectItems.restype = ctypes.HRESULT | |
| shell32.SHOpenFolderAndSelectItems.argtypes = [ | |
| ctypes.c_void_p, | |
| ctypes.c_uint, | |
| ctypes.c_void_p, | |
| ctypes.c_ulong, | |
| ] | |
| h_init: int = ole32.CoInitialize(None) | |
| pidl: int | None = None | |
| try: | |
| pidl = shell32.ILCreateFromPathW(str(path)) | |
| if not pidl: | |
| raise ctypes.WinError() | |
| h_result: int = shell32.SHOpenFolderAndSelectItems(pidl, 0, None, 0) | |
| if h_result: | |
| raise ctypes.WinError(h_result) | |
| finally: | |
| if pidl: | |
| shell32.ILFree(pidl) | |
| if not h_init: | |
| ole32.CoUninitialize() | |
| def linux_show_in_filemanager(path: Path) -> bool: | |
| """Try D-Bus FileManager1 first (Nemo, Nautilus, Dolphin...), | |
| fall back to `xdg-open` on parent folder. | |
| """ | |
| uri: str = path.as_uri() | |
| try: | |
| subprocess.run( | |
| [ | |
| "dbus-send", | |
| "--session", | |
| "--dest=org.freedesktop.FileManager1", | |
| "--type=method_call", | |
| "/org/freedesktop/FileManager1", | |
| "org.freedesktop.FileManager1.ShowItems", | |
| f"array:string:{uri}", | |
| "string:", | |
| ], | |
| check=True, | |
| timeout=3, | |
| ) | |
| return True | |
| except ( | |
| subprocess.CalledProcessError, | |
| FileNotFoundError, | |
| subprocess.TimeoutExpired, | |
| ): | |
| # dbus-send not available, or no FileManager1 service running | |
| subprocess.Popen(['xdg-open', str(path.parent)], start_new_session=True) | |
| return False | |
| def mac_show_in_finder(path: Path) -> None: | |
| script = f'tell application "Finder" to reveal POSIX file "{path}"' | |
| subprocess.Popen(["osascript", "-e", script], start_new_session=True) | |
| if __name__ == '__main__': | |
| OpenFileBrowser().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment