Skip to content

Instantly share code, notes, and snippets.

@lexiwitch
Last active December 25, 2023 03:51
Show Gist options
  • Select an option

  • Save lexiwitch/486b83c2aee0f97ad19593f8e0494656 to your computer and use it in GitHub Desktop.

Select an option

Save lexiwitch/486b83c2aee0f97ad19593f8e0494656 to your computer and use it in GitHub Desktop.
Python script + extra files for adding "Add To Steam" as a context menu button in GNOME/KDE
#!/bin/env python
# add-to-steam
# Adapted from SteamOS Holo's steamos-add-to-steam.sh
# Changes
# - rewrote in Python, added zenity support
#
# License
# Whatever license the other thing is under as of 2023-12-25
#
# - Alexandria
import argparse
import mimetypes
import os
import pathlib
import shutil
import sys
import urllib.parse
def add_to_steam(path: str):
encoded_url = f'steam://addnonsteamgame/{urllib.parse.quote(path, safe="")}'
pathlib.Path('/tmp/addnonsteamgamefile').touch(exist_ok=True)
os.system(f'xdg-open {encoded_url}')
def show_error(err: str, show_dialogue_box: bool):
cmds = [{ 'path': shutil.which('kdialog', os.F_OK)
, 'args': f'--title Error --error "{err}"'
},
{ 'path': shutil.which('zenity', os.F_OK)
, 'args': f"zenity --error --text='{err}'"
}]
if not show_dialogue_box:
return print(err)
for cmd in cmds:
if cmd['path']:
return os.system(f'{cmd["path"]} {cmd["args"]}') and None
def main():
args = argparse.ArgumentParser(prog='add-to-steam')
args.add_argument('-ui', action='store_true', help="Display error graphically")
args.add_argument('filename', help='Executable to add to Steam')
options = args.parse_args()
mime = mimetypes.guess_type(options.filename)[0]
if mime in ('application/x-desktop', 'application/x-ms-dos-executable'):
add_to_steam(options.filename, options.ui)
elif mime in ('application/x-executable',
'application/vnd.appimage',
'application/x-sh'):
if os.access(options.filename, os.X_OK):
add_to_steam(options.filename)
else:
show_error('Unable to add non-Steam game. Is the file executable?',
options.ui)
else:
show_error('Unsupported file type', options.ui)
if __name__ == '__main__':
main()
# Adds it to the dolphin 'right click on file' menubar
# Place in /usr/share/kservices5/ServiceMenus/add-to-steam.desktop
[Desktop Entry]
TryExec=steam
Type=Service
X-KDE-ServiceTypes=KonqPopupMenu/Plugin
MimeType=application/x-desktop;application/x-executable;application/vnd.appimage;application/x-shellscript;application/x-ms-dos-executable;
Actions=addToSteam
[Desktop Action addToSteam]
Exec=add-to-steam -ui %u
Icon=steam
Name=Add to Steam
#!/bin/sh
# Adds it to the nautilus 'right click on file' menubar
# Place in ~/.local/share/nautilus/scripts/nautilus-add-to-steam
add-to-steam -ui $NAUTILUS_SCRIPT_SELECTED_URIS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment