Introdução ao script
Esse script permite escolher um arquivo, selecionar um aplicativo a partir da caixa de diálogo de seleção de arquivos, abrir o arquivo selecionado pelo usuário com esse aplicativo e, opcionalmente, configurar esse mesmo aplicativo como padrão.
Como o script usa a caixa de diálogo de arquivo, você pode selecionar um arquivo .desktop
de um aplicativo de qualquer lugar no sistema de arquivos. Por exemplo, se você tiver um no diretório /opt
, poderá navegar facilmente até lá. Por padrão, a caixa de diálogo começa em /usr/share/applications
, pois é onde a maioria dos aplicativos coloca seus arquivos .desktop
.
Este script deve ser adicionado ao Nautilus como opção de clique direito.
Instalação
O script está publicado aqui e no meu repositório github pessoal. A maneira mais simples seria através do comando wget
:
cd $HOME/.local/share/nautilus/scripts/ && wget -O select_application.py https://raw.githubusercontent.com/SergKolo/sergrep/master/select_application.py && chmod +x select_application.py
Se você tiver git
instalado, poderá instalá-lo nas etapas a seguir.
-
cd /tmp
-
git clone https://github.com/SergKolo/sergrep.git
-
mv /tmp/sergrep/select_application.py $HOME/.local/share/nautilus/scripts
-
chmod +x $HOME/.local/share/nautilus/scripts/select_application.py
Como funciona:
Após a instalação, o usuário pode clicar com o botão direito do mouse em um arquivo
Aescolhadeselect_application.py
exibiráaseguintecaixadediálogo.(Nota:digitarasletrasiniciaisdeumprogramapermiteofocoautomáticodeumarquivoespecífico.)
Escolhaoaplicativoquevocêdesejaabrirseuarquivo.Finalmente,vocêserásolicitadoadefiniresseaplicativocomopadrãoounão:
Código-fontedoscript
#!/usr/bin/env python
#
###########################################################
# Author: Serg Kolo , contact: [email protected]
# Date: July 11, 2016
# Purpose: Alternative "Open With" software for Nautilus
# filemanager
#
# Written for: http://askubuntu.com/q/797571/295286
# Tested on: Ubuntu 16.04 LTS
###########################################################
# Copyright: Serg Kolo , 2016
#
# Permission to use, copy, modify, and distribute this software is
# hereby granted without fee, provided that the copyright notice
# above and this permission statement appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import subprocess
import sys
import os
import getpass
def run_sh(cmd):
# run shell commands, return stdout
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
out = p.stdout.read().strip()
return out
def extract_command(desktop_file):
# read .desktop file , return command it runs
command=""
with open(desktop_file) as file:
for line in file:
if "Exec=" in line:
for string in line.split('Exec=')[1].split():
if "%" not in string:
command = command + string + " "
break
return command
def set_as_default( mime , desk_file ):
# add the .desktop file to list of apps assigned to
# mime types in mimeapps.list file
# TODO : find out if Nautilus automatically creates this file
# or do we need to ensure that it exists ?
defaults_file = '/home/' + getpass.getuser() \
+ '/.local/share/applications/mimeapps.list'
temp_file = '/tmp/new_files'
write_file = open(temp_file,'w')
defaults_found = False
mime_found = False
with open(defaults_file) as read_file:
for line in read_file:
if '[Default Applications]' in line:
defaults_found = True
if defaults_found and mime in line:
write_file.write( mime + '=' + desk_file + "\n" )
mime_found = True
else:
write_file.write( line.strip() + "\n" )
if not mime_found :
write_file.write( mime_type + '=' + desktop_file + "\n" )
write_file.close()
os.rename(temp_file,defaults_file)
#--------------
def main():
# Open file dialog, let user choose program by its .desktop file
filepath = run_sh('zenity --file-selection --file-filter="*.desktop" \
--filename="/usr/share/applications/" ')
if filepath == "" :
sys.exit(1)
# Get the program user wants to run
program = extract_command(filepath)
# Find out the mimetype of the file user wants opened
mime_type = run_sh("file --mime-type " \
+ sys.argv[1] ).split(':')[1].strip()
# Extract just the .desktop filename itself
desktop_file = filepath.split('/')[-1]
# Check if user wants this program as default
return_code = subprocess.call( [ 'zenity', '--question', '--title=""',
'--text="Would you like to set this app as' + \
' default for this filetype?"'])
if return_code == 0 :
set_as_default( mime_type , desktop_file )
# Finally, launch the program with file user chose
# Can't use run_sh() because it expects stdout
proc = subprocess.Popen( "nohup " + program + " " + sys.argv[1] \
+ " &> /dev/null &" , shell=True)
if __name__ == "__main__" :
main()