交互式“打开”对话框模式?

交互式“打开”对话框模式?

今年我从 Windows 转到了 Ubuntu。我喜欢 Ubuntu,但我缺少智能的“打开方式”对话框。您应该手动从长列表中搜索程序,无法过滤,或者从磁盘中搜索,我甚至不知道我的程序在哪里。这就是 Ubuntu 开箱即用的功能。是否有任何修改可以允许轻松搜索并选择一个程序在“打开方式”对话框中打开文件?它应该像我们在 Win 键上的“搜索您的计算机”屏幕中输入程序名称一样工作。

答案1

剧本简介

该脚本允许选择一个文件,从文件选择对话框中选择一个应用程序,使用该应用程序打开用户选择的文件,并可选择将该应用程序设置为默认应用程序。

由于脚本使用文件对话框,因此您可以.desktop从文件系统中的任何位置选择应用程序的文件。例如,如果您在/opt目录中有一个文件,则可以轻松导航到那里。默认情况下,对话框从开始,/usr/share/applications因为大多数应用程序都将其.desktop文件放在那里。

该脚本旨在作为右键单击选项添加到 Nautilus。

安装

脚本已发布在这里,并发布在我的个人github 存储库. 最简单的方法是通过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

如果您已经git安装,可以按照以下步骤进行安装。

  1. cd /tmp
  2. git clone https://github.com/SergKolo/sergrep.git
  3. mv /tmp/sergrep/select_application.py $HOME/.local/share/nautilus/scripts
  4. chmod +x $HOME/.local/share/nautilus/scripts/select_application.py

怎么运行的:

安装后,用户可以右键单击文件

在此处输入图片描述

选择select_application.py将会出现以下对话框。(注意:输入程序的首几个字母可以自动聚焦特定文件。)

在此处输入图片描述

选择您想要打开文件的应用程序。最后,系统会要求您将该应用程序设置为默认应用程序:

在此处输入图片描述

脚本源代码

#!/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()

相关内容