AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 756072
Accepted
Kalamalka Kid
Kalamalka Kid
Asked: 2016-04-11 16:33:11 +0800 CST2016-04-11 16:33:11 +0800 CST 2016-04-11 16:33:11 +0800 CST

从 SpaceFm 启动时,.desktop 文件将无法在 Google Chrome 中打开

  • 772

在使用 Google Chrome 时,我经常将网站链接从地址栏中拖到桌面上的文件夹中以供日后参考。这将创建一个.desktop本质上是网页链接的文件。

只需双击打开这些桌面文件,就可以从 Nautilus 和 PCmanFM 轻松打开它们,但是,当我使用 SpaceFM 文件管理器打开它们时,Google Chrome 开始下载文件,而不是像其他提到的那样打开它文件管理器。

如果我右键单击它们并使用 Firefox 打开这些文件,但不能使用 Google Chrome 打开,我可以通过 SpaceFM 打开这些文件。

桌面文件的示例如下:

[Desktop Entry]
Encoding=UTF-8
Name=Link to The Hidden Fortress (1958) - IMDb
Type=Link
URL=http://www.imdb.com/title/tt0051808/?ref_=nv_sr_2
Icon=text-html

.desktop从 SpaceFM 浏览器启动时,有没有办法让这些文件在 Google Chrome 中正常打开?

unity
  • 1 1 个回答
  • 839 Views

1 个回答

  • Voted
  1. Best Answer
    Jacob Vlijm
    2016-05-07T01:07:12+08:002016-05-07T01:07:12+08:00

    1.将链接文件转换为启动器

    下面的解决方案提供了右键单击链接(.desktop文件)并选择>“执行”的选项,它将运行Google-Chrome以打开链接。通过编辑SpaceFm的设置,您也可以通过双击运行链接(见注释[3])。

    在此处输入图像描述

    请注意,该解决方案会自动编辑(仅)桌面上的新链接,以专门使用Google-Chrome

    这是什么

    一个小的后台脚本每两秒检查一次桌面上的新 .desktop文件。如果找到相关文件,则将该文件从链接文件编辑为应用程序文件。这是通过编辑文件中的两行来完成的:

    • 该行:

      Type=Link
      

      改为:

      Type=Application
      
    • 该行:

      URL=<link>
      

      改为:

      Exec=/usr/bin/google-chrome-stable <link>
      

    在我运行的测试中,通过右键单击使链接“可打开” SpaceFm:右键单击 > 打开 > 执行

    剧本

    #!/usr/bin/env python3
    import os
    import time
    
    # --- define the (absolute) path to your desktop below
    dr = "/absolute/path/to/your/desktop"
    # edit (if necessary) the command to launch Google-Chrome
    application = "/usr/bin/google-chrome-stable"
    
    def find_relevant():
        return [f for f in os.listdir(dr) if f.endswith(".desktop")]
    
    relevant1 = []
    
    while True:
        time.sleep(2)
        relevant2 = [f for f in os.listdir(dr) if f.endswith(".desktop")]
        new = [f for f in relevant2 if not f in relevant1]
        if new:
            for f in new:
                f = dr+"/"+f
                rewrite = False
                lines = [l.strip() for l in open(f).readlines()]
                for i, l in enumerate(lines):
                    if l.startswith("Type=Link"):
                        rewrite = True
                        lines[i] = "Type=Application"
                    elif l.startswith("URL="):
                        lines[i] = l.replace("URL=", "Exec="+application+" ")
                if rewrite == True:
                    print("rewrite")
                    open(f, "wt").write(("\n").join(lines))            
        relevant1 = relevant2
    

    如何使用

    1. 将脚本复制到一个空文件中,另存为edit_links.py
    2. 在脚本的头部,编辑桌面的路径(如前所述,在此处使用绝对路径)
    3. 通过检查文件中的第一行来检查要运行的命令Google-Chrome(也在 head- 部分中设置):runExec=google-chrome.desktop

      gedit /usr/share/applications/google-chrome.desktop
      

      读取文件。

    4. 通过以下命令测试运行脚本:

      python3 /path/to/edit_links.py
      

      打开Google-Chrome,将链接拖到桌面,几秒钟后测试它是否正常。

    5. 如果一切正常,请将其添加到启动应用程序:Dash > Startup Applications > Add。添加命令:

      python3 /path/to/edit_links.py
      

    笔记

    1. 拖动的.desktop文件(链接)需要在您的桌面上至少一到两秒钟才能找到和编辑,因此如果您移动链接,至少将它们留在桌面上几秒钟。:)
    2. 该脚本所做的是检查.desktop桌面上的新文件,如果有新文件,它只会读取/编辑文件。这对您的系统没有任何意义。
    3. 通过编辑SpaceFm的设置,您还可以通过双击运行“链接”:

      在此处输入图像描述

    递归转换现有的链接文件

    此外,正如聊天中所讨论的,运行一次的脚本以递归方式转换目录中的链接:

    #!/usr/bin/env python3
    import os
    import sys
    
    # --- define the (absolute) path to your desktop below
    dr = sys.argv[1]
    # edit (if necessary) the command to launch Google-Chrome
    application = "/usr/bin/google-chrome-stable"
    
    for root, dirs, files in os.walk(dr):
        for f in files:
            if f.endswith(".desktop"):
                f = root+"/"+f
                rewrite = False
                lines = [l.strip() for l in open(f).readlines()]
                for i, l in enumerate(lines):
                    if l.startswith("Type=Link"):
                        rewrite = True
                        lines[i] = "Type=Application"
                    elif l.startswith("URL="):
                        lines[i] = l.replace("URL=", "Exec="+application+" ")
                if rewrite == True:
                    open(f, "wt").write(("\n").join(lines)) 
    

    要使用它,请将其另存为convert_links.py,并以目标目录作为参数运行它:

    python3 /path/to/convert_links.py <directory>
    

    2.将链接转换成跨平台可用的链接文件

    根据 OP 的要求,在(第一个)(后台)脚本的一个版本下,将通过将链接从浏览器拖到桌面创建的链接文件转换为跨平台链接。用法与第一节中的说明完全相同。

    剧本

    #!/usr/bin/env python3
    import os
    import time
    
    # --- define the (absolute) path to your desktop below
    dr = "/absolute/path/to/your/desktop"
    
    out1 = ["<html>", "<body>", '<script type="text/javascript">']
    out2 = ["</script>", "</body>", "</html>"]
    
    def find_relevant():
        return [f for f in os.listdir(dr) if f.endswith(".desktop")]
    
    relevant1 = []
    
    while True:
        time.sleep(2)
        relevant2 = [f for f in os.listdir(dr) if f.endswith(".desktop")]
        new = [f for f in relevant2 if not f in relevant1]
        if new:
            for f in new:
                f = dr+"/"+f
                rewrite = False
                lines = [l.strip() for l in open(f).readlines()]
                for i, l in enumerate(lines):
                    if l.startswith("Type=Link"):
                        rewrite = True
                    elif l.startswith("URL="): 
                        url = 'window.location.href = "'+l.replace("URL=", "")+'"'
                        out1.append(url)
                    elif l.startswith("Name="):
                        name = l.replace("Name=", "")
                if rewrite == True:
                    open(f.replace(".desktop", ".html"), "wt").write(("\n").join(out1+out2))
                    os.remove(f)
        relevant1 = relevant2
    

    用于在递归目录中转换现有链接的版本(单次运行)

    #!/usr/bin/env python3
    import os
    import sys
    
    dr = sys.argv[1]
    
    out1 = ["<html>", "<body>", '<script type="text/javascript">']
    out2 = ["</script>", "</body>", "</html>"]
    
    for root, dirs, files in os.walk(dr):
        for f in files:
            if f.endswith(".desktop"):
                f = root+"/"+f
                rewrite = False
                lines = [l.strip() for l in open(f).readlines()]
                for i, l in enumerate(lines):
                    if l.startswith("Type=Link"):
                        rewrite = True
                    elif l.startswith("URL="): 
                        url = 'window.location.href = "'+l.replace("URL=", "")+'"'
                        out1.append(url)
                    elif l.startswith("Name="):
                        name = l.replace("Name=", "")
                if rewrite == True:
                    open(f.replace(".desktop", ".html"), "wt").write(("\n").join(out1+out2))
                    os.remove(f)
    

    要使用它,请将其另存为convert_links.py,并以目标目录作为参数运行它:

    python3 /path/to/convert_links.py <directory>
    

    笔记

    此版本基于 Super User 上的这个不错的答案来创建跨平台链接。

    • 1

相关问题

  • 如何将 Web 应用程序放入 Unity Launcher?

  • Ubuntu 上网本 10.10 中没有 Alt+F2?

  • Unity 中的 gnome-do 样式键盘快捷键

  • 在哪里提交 Unity 的错误/愿望清单?

  • Unity 启动器——它可以作为单独的包提供吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve