Skip to content

Instantly share code, notes, and snippets.

@vn-ki
Last active February 29, 2024 13:48
Show Gist options
  • Save vn-ki/e592daa2e429377a0640ba71051175e0 to your computer and use it in GitHub Desktop.
Save vn-ki/e592daa2e429377a0640ba71051175e0 to your computer and use it in GitHub Desktop.
Use ranger as chrome file chooser
#!/bin/sh
XDG_CURRENT_DESKTOP=KDE google-chrome-beta $@
import os
import sys
from ranger.core.loader import CommandLoader
from ranger.api.commands import Command
class chrome_choose(Command):
def execute(self):
""" Extract copied files to current directory """
filename = self.arg(1)
cwd = self.fm.thisdir
if filename == '.':
with open('/tmp/ranger-chrome-choosed', 'w') as f:
f.write(cwd.path + '/')
sys.exit(0)
with open('/tmp/ranger-chrome-choosed', 'w') as f:
f.write(os.path.join(cwd.path, filename))
sys.exit(0)

Use ranger as chrome file chooser

  1. Copy the contents of kdialog to /usr/local/bin/kdialog
  2. Copy the contents of chrome_chooser.py to ~/.config/ranger/plugins/chrome_chooser.py
  3. Optionally map appropriately
  4. Run chrome with XDG_CURRENT_DESKTOP=KDE
#!/bin/sh
for ((i=1;i<=$#;i++));
do
if [ ${!i} = "--getsavefilename" ]
then ((i++))
filename=${!i};
fi
if [ ${!i} = "--version" ]
then
echo "someversion"
exit
fi
done;
path=$( echo ${filename%/*} )
file=$( echo ${filename##/*/} )
rm /tmp/ranger-chrome-choosed
termite -e "ranger" -t file-chooser-ranger
selected=$(cat /tmp/ranger-chrome-choosed 2> /dev/null)
if [ ! $? = 0 ]
then
exit 1
fi
if [ -d $selected ]
then
echo "$selected$file"
else
echo $selected
fi
map dh chrome_choose .
map dn console chrome_choose
@Unixsys
Copy link

Unixsys commented Jun 25, 2020

Could you do something with another file selector? How are the selections transmitted between Chrome and ranger?

@the-anonymous-raven
Copy link

Hey, the code doesn't work if the filename contains spaces.
It works when all the args in chrome-chooser.py are taken. [as spaces are args delimiter i guess]

I just forked the gist and applied the changes, you can edit it accordingly.
Thanks

Copy link

ghost commented Feb 29, 2024

How do I open with Chromium?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment