Pin files & folders to your app grid on GNOME
This script generates a desktop entry that opens a folder, with the same icon and all.
#!/usr/bin/env python3
import sys, textwrap
from pathlib import Path
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gio, GLib, Gtk
def get_icon_name(path: Path):
"""Get the icon associated with a file."""
file = Gio.File.new_for_path(path.as_posix())
info = file.query_info('standard::icon', 0, Gio.Cancellable())
return info.get_icon().get_names()[0]
def install_desktop_file(path: Path):
"""Generate and install desktop entry for a file or folder."""
content = textwrap.dedent(f'''
[Desktop Entry]
Type=Application
Version=1.0
Name={path.name}
Exec=xdg-open {path}
Icon={get_icon_name(path)}
''')
print(content)
destpath = Path(GLib.get_user_data_dir()) / 'applications' / (path.stem + '.desktop')
destpath.write_text(content)
if __name__ == '__main__':
install_desktop_file(Path(sys.argv[1]))