Last active
November 21, 2023 01:59
-
-
Save sapslaj/74da9dfddab6fa2b9c0f8d2f4e9f9e39 to your computer and use it in GitHub Desktop.
Extract To Folder Nemo Action
This file contains 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
[Nemo Action] | |
Active=true | |
Name=Extract to %f | |
Comment=Extract to new folder called "%f" | |
Exec=<extract-to-folder.py %F> | |
Icon-Name=gnome-mime-application-x-compress | |
Selection=any | |
Extensions=zip;7z;ar;cbz;cpio;exe;iso;jar;tar;tar;7z;tar.Z;tar.bz2;tar.gz;tar.lz;tar.lzma;tar.xz;apk; | |
Quote=double |
This file contains 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
#!/usr/bin/env python3 | |
import sys | |
import pathlib | |
import re | |
import subprocess | |
# Set this to false if you want to be prompted to open new dir after extraction completes | |
ALWAYS_OPEN = True | |
def stem(path): | |
special_extensions = [ | |
'.tar.bz2', | |
'.tar.gz', | |
'.tar.xz', | |
'.tar.lz', | |
'.tar.lzma', | |
] | |
for ext in special_extensions: | |
if path.name.endswith(ext): | |
return re.sub(f"{ext}$", '', path.name) | |
return path.stem | |
def pathdir(path): | |
return path.parent | |
def zenity_dialog(type, text): | |
return subprocess.run(['zenity', f"--{type}", '--title', 'Extract To Folder', '--text', text]) | |
def xdg_open(target): | |
subprocess.run(['xdg-open', target]) | |
if __name__ == "__main__": | |
try: | |
for arg in sys.argv[1:]: | |
path = pathlib.Path(arg) | |
target_dir = pathlib.Path(f"{pathdir(path)}/{stem(path)}") | |
if not target_dir.exists(): | |
target_dir.mkdir() | |
if target_dir.is_file(): | |
raise FileExistsError(f"File exists at {target_dir}. Cannot create a new directory.") | |
file_roller = subprocess.run(['file-roller', '-e', target_dir, arg], stdout=subprocess.PIPE) | |
if file_roller.returncode != 0: | |
raise Exception('file-roller extraction failed.') | |
if ALWAYS_OPEN: | |
xdg_open(target_dir) | |
else: | |
open_new_folder_dialog = zenity_dialog('question', f"Open {target_dir}?") | |
if open_new_folder_dialog.returncode == 0: | |
xdg_open(target_dir) | |
except Exception as e: | |
zenity_dialog('error', str(e)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment