如何根据标签标记文件并稍后搜索?
运行Ubuntu Gnome。
我有很多PDF和其他文档,我想标记它们。 然后根据这些标签搜索它们。 即使我将文件移动到不同的文件夹(因此,标签贴在文件上)。
我搜索但文件和文档不提供此选项。
难道我做错了什么? 如何标记文件以便以后可以根据标签进行搜索?
内容:
- 介绍
- 安装
- 用法
- 源代码
1.简介
该解决方案由两个脚本组成 – 一个用于标记,一个用于读取特定标记下的文件列表。 两者都必须存在于~/.local/share/nautilus/scripts
并通过右键单击任何文件上的Nautilus文件管理器并导航到Scripts子菜单来激活。 这里提供了每个脚本的源代码以及GitHub
2.安装
这两个脚本都必须保存到~/.local/share/nautilus/scripts
,其中~
是用户的主目录,并使用chmod +x filename
可执行。 为便于安装,请使用以下bash脚本:
#!/bin/bash N_SCRIPTS="$HOME/.local/share/nautilus/scripts" cd /tmp rm master.zip* rm -rf nautilus_scripts-master wget https://github.com/SergKolo/nautilus_scripts/archive/master.zip unzip master.zip install nautilus_scripts-master/tag_file.py "$N_SCRIPTS/tag_file.py" install nautilus_scripts-master/read_tags.py "$N_SCRIPTS/read_tags.py"
3.用法:
标记文件 :
在Nautilus文件管理器中选择文件,右键单击它们,然后导航到Scripts子菜单。 选择tag_file.py
。 按Enter键 第一次运行此脚本时,将没有配置文件,因此您将看到:
下一次,当您已经标记了一些文件时,您将看到一个弹出窗口,允许您选择标记和/或添加新标记(这样您就可以在多个标记下记录文件)。 单击 “ 确定”将文件添加到此标记。 注意 :避免使用“|” 标签名称中的符号。
该脚本记录~/.tagged_files
所有内容。 该文件本质上是一个json
字典(这不是普通用户应该关心的东西,但它对程序员来说很方便:))。 该文件的格式如下:
{ "Important Screenshots": [ "/home/xieerqi/\u56fe\u7247/Screenshot from 2016-10-01 09-15-46.png", "/home/xieerqi/\u56fe\u7247/Screenshot from 2016-09-30 18-47-12.png", "/home/xieerqi/\u56fe\u7247/Screenshot from 2016-09-30 18-46-46.png", "/home/xieerqi/\u56fe\u7247/Screenshot from 2016-09-30 17-35-32.png" ], "Translation Docs": [ "/home/xieerqi/Downloads/908173 - \u7ffb\u8bd1.doc", "/home/xieerqi/Downloads/911683\u7ffb\u8bd1.docx", "/home/xieerqi/Downloads/914549 -\u7ffb\u8bd1.txt" ] }
如果您想要“取消”某个文件,只需从该列表中删除一个条目即可。 记住格式和逗号。
按标签搜索 :
既然你有一个很好的~/.tagged_files
文件数据库,你可以读取该文件,或者使用read_tags.py
脚本。
右键单击Nautilus中的任何文件(真的无关紧要)。选择read_tags.py
。 按Enter键
您将看到一个弹出窗口,询问您要搜索的标记:
选择一个,单击“ 确定” 。 您将看到一个列表对话框,显示您希望所选标记的文件。 您可以选择任何单个文件,它将使用分配给该文件类型的默认程序打开。
4.源代码:
tag_file.py
:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: Serg Kolo # Date: Oct 1st, 2016 # Description: tag_file.py, script for # recording paths to files under # specific , user-defined tag # in ~/.tagged_files # Written for: http://askubuntu.com/q/827701/295286 # Tested on : Ubuntu ( Unity ) 16.04 from __future__ import print_function import subprocess import json import os import sys def show_error(string): subprocess.call(['zenity','--error', '--title',__file__, '--text',string ]) sys.exit(1) def run_cmd(cmdlist): """ Reusable function for running external commands """ new_env = dict(os.environ) new_env['LC_ALL'] = 'C' try: stdout = subprocess.check_output(cmdlist, env=new_env) except subprocess.CalledProcessError: pass else: if stdout: return stdout def write_to_file(conf_file,tag,path_list): # if config file exists , read it data = {} if os.path.exists(conf_file): with open(conf_file) as f: data = json.load(f) if tag in data: for path in path_list: if path in data[tag]: continue data[tag].append(path) else: data[tag] = path_list with open(conf_file,'w') as f: json.dump(data,f,indent=4,sort_keys=True) def get_tags(conf_file): if os.path.exists(conf_file): with open(conf_file) as f: data = json.load(f) return '|'.join(data.keys()) def main(): user_home = os.path.expanduser('~') config = '.tagged_files' conf_path = os.path.join(user_home,config) file_paths = [ os.path.abspath(f) for f in sys.argv[1:] ] tags = None try: tags = get_tags(conf_path) except Exception as e: show_error(e) command = [ 'zenity','--forms','--title', 'Tag the File' ] if tags: combo = ['--add-combo','Existing Tags', '--combo-values',tags ] command = command + combo command = command + ['--add-entry','New Tag'] result = run_cmd(command) if not result: sys.exit(1) result = result.decode().strip().split('|') for tag in result: if tag == '': continue write_to_file(conf_path,tag,file_paths) if __name__ == '__main__': main()
read_tags.py
:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # Author: Serg Kolo # Date: Oct 1st, 2016 # Description: read_tags.py, script for # reading paths to files under # specific , user-defined tag # in ~/.tagged_files # Written for: http://askubuntu.com/q/827701/295286 # Tested on : Ubuntu ( Unity ) 16.04 import subprocess import json import sys import os def run_cmd(cmdlist): """ Reusable function for running external commands """ new_env = dict(os.environ) new_env['LC_ALL'] = 'C' try: stdout = subprocess.check_output(cmdlist, env=new_env) except subprocess.CalledProcessError as e: print(str(e)) else: if stdout: return stdout def show_error(string): subprocess.call(['zenity','--error', '--title',__file__, '--text',string ]) sys.exit(1) def read_tags_file(file,tag): if os.path.exists(file): with open(file) as f: data = json.load(f) if tag in data.keys(): return data[tag] else: show_error('No such tag') else: show_error('Config file doesnt exist') def get_tags(conf_file): """ read the tags file, return a string joined with | for further processing """ if os.path.exists(conf_file): with open(conf_file) as f: data = json.load(f) return '|'.join(data.keys()) def main(): user_home = os.path.expanduser('~') config = '.tagged_files' conf_path = os.path.join(user_home,config) tags = get_tags(conf_path) command = ['zenity','--forms','--add-combo', 'Which tag ?', '--combo-values',tags ] tag = run_cmd(command) if not tag: sys.exit(0) tag = tag.decode().strip() file_list = read_tags_file(conf_path,tag) command = ['zenity', '--list', '--text','Select a file to open', '--column', 'File paths' ] selected = run_cmd(command + file_list) if selected: selected = selected.decode().strip() run_cmd(['xdg-open',selected]) if __name__ == '__main__': try: main() except Exception as e: show_error(str(e))
我找到了一种方法来做到这一点。
打开终端( CTRL + ALT + T )然后运行此命令:
sudo add-apt-repository ppa:tracker-team/tracker
输入您的密码,在提示时,按Enter键,然后运行
sudo apt-get update
然后
sudo apt-get install tracker tracker-gui
如果它说它已经是最新版本,请不要担心。
现在打开Nautilus / Files并右键单击要添加标签的文档。 选择属性,然后选择“标签”选项卡。 在文本框中输入标签,然后按Enter键或单击“添加”按钮进行添加。 您还可以单击已添加的标记,然后选择“删除”按钮以删除标记。 请注意,标签区分大小写。 您创建的标记将在整个系统中保持不变,因此您可以轻松地在已创建的标记旁边进行检查以标记文件,而不是再次手动设置标记。
标记所需的项目后,您现在可以搜索它们,但不能在“文件”中搜索它们。 转到活动,然后搜索应用Desktop Search
。 启动它,然后查看顶部的选项。 在窗口的左上角,单击文件夹图标,其工具提示“按列表中的文件显示结果”。 现在你有更多的选择。 使用工具提示“仅在文件标签中查找搜索条件”,选择搜索框左侧的选项。 现在你可以搜索标签了!
要使用此function,请输入要搜索的标记,用逗号分隔,然后按Enter键。 例如:
重要的是,9月,演讲
这将仅显示具有所有三个标签的文件:“重要”,“九月”和“演示”。
通过双击一个,它将在默认程序中打开文件,右键单击并选择“显示父目录”,它将在Nautilus中打开它的位置。
在桌面搜索中,您还可以单击窗口顶部右侧的第二个按钮(通常是星形或心形)来编辑应用程序本身的标签!
你有它! 希望这可以帮助。 如果您还有其他问题,请与我们联系。