如何将文件固定到书签?

如何将文件固定到书签?

我想将文件添加到书签窗格。这可以实现吗?

在此处输入图片描述

答案1

介绍

虽然从技术上来说,在 Nautilus 中无法为文件添加书签,但可以添加特定目录并在那里保存文件的符号链接。我编写的脚本就是这么做的。

用法

该脚本的行为方式如下:右键单击选定的文件,导航到脚本,单击bookmark_file.py。该脚本将自动Bookmarked_Files在您的文件中创建目录,并将其添加到 Nautilus 面板。下次单击它时,您将在那里找到指向您文件的符号链接

请注意,可以安全地从书签文件夹中删除符号链接,而实际文件将保留在各自的位置。

获取脚本。

脚本源代码在此答案中以及GitHub

如果您想手动添加文件:

  1. 复制脚本源代码并将其保存为bookmark_files.py目录$HOME/.local/share/nautilus/scripts
  2. 确保将文件设为可执行文件。您可以通过在 Nautilus 中单击右键或chmod +x $HOME/.local/share/nautilus/scripts/bookmark_files.py在终端中执行此操作。

或者,您可以通过命令行执行此操作git(您需要git先安装):

git clone https://github.com/SergKolo/nautilus_scripts.git  $HOME/.local/share/nautilus/scripts

另一种方法是从同一个 github 存储库获取 zip 文件。

源代码

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Author: Sergiy Kolodyazhnyy
# Contact: [email protected]
# Description: Creates a symlink to user-selected file in 
#              /home/username/Bookmarked_Files
#
# The MIT License (MIT)
# 
# Copyright (c) 2016 Sergiy Kolodyazhnyy 
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# 
# 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.
__author__ = "Sergiy Kolodyazhnyy <[email protected]>"

import os
import sys
import subprocess

def main():

    user_home = os.path.expanduser('~')
    bookmark_dir = os.path.join( user_home,'Bookmarked_Files'  )
    file_path = os.path.abspath(sys.argv[1])


    # ensure the directory exists
    if not os.path.lexists( bookmark_dir  ):
       os.mkdir( bookmark_dir  )

    # check if the directory is already added as bookmark
    try:

        bookmarks = os.path.join(user_home,'.config/gtk-3.0/bookmarks')
        with open(bookmarks ,'a+') as file:
            file.seek(0)
            data = file.read()  
            if not bookmark_dir in data:
                file.write('file://' + bookmark_dir )   

    except IOError:
         text = '--text="Cannot auto-add bookmark directory to panel.'
         subprocess.call(['zenity','--error',text])             

    # create symlink to file
    try:
        basename = sys.argv[1].split('/')[-1]
        os.symlink(file_path, os.path.join(bookmark_dir,basename))
    except OSError as error :
        text = '--text="' + str(error) + '"'
        subprocess.call( ['zenity','--error',text] ) 

if __name__ == '__main__':
    main()

截图

运行脚本之前

在此处输入图片描述

运行脚本后。该图显示了新创建的 Bookmarked_Files 目录和指向上一张图片中文件的符号链接

在此处输入图片描述

相关内容