Skip to content

Instantly share code, notes, and snippets.

@jaybosamiya
Last active February 3, 2020 20:40
Show Gist options
  • Save jaybosamiya/3c073be9c407fd2e09885cd19fee17ad to your computer and use it in GitHub Desktop.
Save jaybosamiya/3c073be9c407fd2e09885cd19fee17ad to your computer and use it in GitHub Desktop.
# Drop this into ~/.local/share/nautilus-python/extensions
# (creating the directory if needed) and restart nautilus.
#
# Provides a new option to "Download from Clipboard" in the
# context menu of folders, which uses `wget` to download
# whatever is in the clipboard.
import gi
from gi import require_version
require_version('Gtk', '3.0')
require_version('Nautilus', '3.0')
from gi.repository import GObject
from gi.repository import Nautilus
import os
# A way to get unquote working with python 2 and 3
try:
from urllib import unquote
except ImportError:
from urllib.parse import unquote
class DownloadClipboardExtension(Nautilus.MenuProvider, GObject.GObject):
def __init__(self):
pass
def _download_clipboard(self, file):
filename = unquote(file.get_uri()[7:])
os.chdir(filename)
command = ''
command += r'wget -o- -- "$(xclip -selection clipboard -o)" | '
command += r"sed -u 's/^[a-zA-Z\-].*//; s/.* \{1,2\}\([0-9]\{1,3\}\)%.*/\1\n#Downloading... \1%/; s/^20[0-9][0-9].*/#Done./' | "
command += r'zenity --progress --percentage=0 --title=Download dialog --text=Starting... --auto-close --auto-kill'
os.system(command)
def menu_activate_cb(self, menu, file):
self._download_clipboard(file)
def menu_background_activate_cb(self, menu, file):
self._download_clipboard(file)
def get_file_items(self, window, files):
if len(files) != 1:
return
file = files[0]
if not file.is_directory() or file.get_uri_scheme() != 'file':
return
item = Nautilus.MenuItem(name='NautilusPython::downloadclipboard_file_item',
label='Download From Clipboard',
tip='Download URL from Clipboard Into %s' % file.get_name())
item.connect('activate', self.menu_activate_cb, file)
return item,
def get_background_items(self, window, file):
item = Nautilus.MenuItem(name='NautilusPython::downloadclipboard_file_item2',
label='Download From Clipboard',
tip='Download URL from Clipboard Into %s' % file.get_name())
item.connect('activate', self.menu_background_activate_cb, file)
return item,
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment