Skip to content

Instantly share code, notes, and snippets.

@GeekGawd
Created October 25, 2024 16:13
Show Gist options
  • Save GeekGawd/a24ec9bec58abe84fcb62157531b1f54 to your computer and use it in GitHub Desktop.
Save GeekGawd/a24ec9bec58abe84fcb62157531b1f54 to your computer and use it in GitHub Desktop.
Automate the process to create a desktop entry for an AppImage
import os
import subprocess
import shutil
import requests
def download_appimage(url, download_path):
print(f"Downloading AppImage from {url}...")
response = requests.get(url, stream=True)
with open(download_path, 'wb') as file:
shutil.copyfileobj(response.raw, file)
print(f"Downloaded to {download_path}")
def make_executable(file_path):
subprocess.run(["chmod", "+x", file_path], check=True)
print(f"Made {file_path} executable")
def move_appimage(appimage_path, target_dir):
os.makedirs(target_dir, exist_ok=True)
target_path = os.path.join(target_dir, os.path.basename(appimage_path))
shutil.move(appimage_path, target_path)
print(f"Moved AppImage to {target_path}")
return target_path
def extract_appimage(appimage_path):
print("Extracting AppImage...")
subprocess.run([appimage_path, "--appimage-extract"], check=True)
extract_dir = "squashfs-root"
print(f"Extracted to {extract_dir}")
return extract_dir
def copy_desktop_entry(extract_dir, app_name):
desktop_entry_dir = os.path.expanduser("~/.local/share/applications")
os.makedirs(desktop_entry_dir, exist_ok=True)
desktop_file = next((f for f in os.listdir(os.path.join(extract_dir, "usr/share/applications")) if f.endswith(".desktop")), None)
if not desktop_file:
print("Desktop entry not found in the extracted AppImage.")
return None
desktop_entry_path = os.path.join(desktop_entry_dir, desktop_file)
shutil.copy(os.path.join(extract_dir, "usr/share/applications", desktop_file), desktop_entry_path)
print(f"Copied desktop entry to {desktop_entry_path}")
# Edit the Exec line in the desktop file
with open(desktop_entry_path, "r") as file:
content = file.read()
exec_line = f"Exec={app_name} %F"
content = content.replace("Exec=inkscape", exec_line) # Assuming the Exec line starts with inkscape or similar.
with open(desktop_entry_path, "w") as file:
file.write(content)
# Make the .desktop file executable
subprocess.run(["chmod", "+x", desktop_entry_path], check=True)
print(f"Modified desktop entry and made it executable: {desktop_entry_path}")
def clean_up(extract_dir):
shutil.rmtree(extract_dir)
print(f"Removed extracted directory {extract_dir}")
def main():
# URL to download the AppImage from
url = input("Enter the AppImage download URL: ").strip()
appimage_name = os.path.basename(url)
download_path = os.path.join(os.getcwd(), appimage_name)
target_dir = os.path.expanduser("~/.local/bin")
app_name = os.path.join(target_dir, appimage_name)
try:
# Download the AppImage
download_appimage(url, download_path)
# Make the AppImage executable
make_executable(download_path)
# Move the AppImage to ~/.local/bin
appimage_path = move_appimage(download_path, target_dir)
# Extract the AppImage
extract_dir = extract_appimage(appimage_path)
# Copy and edit the desktop entry
copy_desktop_entry(extract_dir, appimage_path)
# Clean up
clean_up(extract_dir)
print("AppImage registration complete!")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment