2011-05-16 59 views
6

我希望能夠在我的.desktop文件的頂部添加#!評論,以便如果它具有執行權限並且被執行,它將實際運行。但是,我不知道.desktop文件的解釋器是什麼,所以我不知道在hashbang中寫了哪個/usr/bin/文件。有任何想法嗎?Hashbang for Gnome .desktop文件


編輯:

到目前爲止,我做了一個小bash腳本,execdesktop,可以執行桌面文件:

`sed -nr 's/Exec=(.*)$/\\1/p' $1` 

如果我然後添加以下到我的.desktop files:

#!/usr/bin/execdesktop 

T它運行良好。此方法可行,但我不想使用它,因爲它需要安裝execdesktop。

+2

除非你已經創建了一個使用你的系統包管理器('dpkg'?RPM?'emerge'?等)來安裝'execdesktop'的包,腳本可能應該放在'/ usr/local/bin'中,而不是'的/ usr/bin'。 – tripleee 2015-08-23 06:09:36

回答

4

您可以隨時使用xdg-open您的家當,如:

#!/usr/bin/env xdg-open 

這不會引起任何麻煩,因爲#開始評論也.desktop文件。

+0

有趣的是,儘管xdg-open似乎想要在gedit中打開所有的.desktop文件,而不是實際啓動它們。 – Reinderien 2011-05-16 19:06:15

+0

@Reinderien:你有沒有將'.desktop'文件關聯到'gedit'? 'xdg-mime查詢默認應用程序/ x-desktop'返回什麼? – ninjalj 2011-05-16 19:12:39

+0

@ninjalj它什麼也沒有返回(空白)。 – Reinderien 2011-05-16 19:16:05

5

沒有一個; .desktop文件不打算執行。改爲運行Exec鍵中給出的可執行文件。

+1

他們以這種或那種方式執行。絕對沒有辦法從命令行調用.desktop文件?這似乎不像linux一樣... – Reinderien 2011-05-16 18:00:07

+2

桌面文件不會被執行。他們內部具有可執行命令行的行是被執行的行。桌面環境解析文件的其餘部分。 – 2013-04-09 19:00:20

+1

問題是,在我的情況下,在Exec屬性中提到的腳本工作,但桌面文件不能在Ubuntu下工作(它在Mageia上工作)。這就是爲什麼在命令行中打開桌面文件是有意義的,以便它以與操作系統相同的方式執行命令。 – gouessej 2015-10-29 11:47:54

5

只是要明確,Ignacio是正確here在那。桌面文件不應該直接執行。這是可能的(如你所發現的),但是不明智。

另一方面,請勿使用xdg-open。如果存在正確關聯的MIME類型,它可能會正常工作,但這不可靠。您可以使用gtk-launch。它的用法如下:

gtk-launch APPLICATION [URI...] 
gtk-launch app-name.desktop 
gtk-launch app-name 

這裏是男人的條目:

NAME

gtk-launch - Launch an application 

提要

gtk-launch [APPLICATION] [URI...] 

說明

gtk-launch launches an application using the given name. The 
    application is started with proper startup notification on a default 
    display, unless specified otherwise. 

    gtk-launch takes at least one argument, the name of the application to 
    launch. The name should match application desktop file name, as 
    residing in /usr/share/application, with or without the '.desktop' 
    suffix. 

    If called with more than one argument, the rest of them besides the 
    application name are considered URI locations and are passed as 
    arguments to the launched application. 

請注意,gtk-launch需要安裝的的.desktop文件(即位於在/ usr /共享/應用$ HOME /。本地/共享/應用)。

因此,要解決這個問題,我們可以使用它啓動之前臨時安裝所需.desktop文件hackish的小bash函數。「正確」的方式來安裝。桌面文件是通過desktop-file-install,但我會忽略這一點。

launch(){ 
    (
    # where you want to install the launcher to 
    appdir=$HOME/.local/share/applications 

    # the template used to install the launcher 
    template=launcher-XXXXXX.desktop 

    # ensure $1 has a .desktop extension, exists, is a normal file, is readable, has nonzero size 
    # optionally use desktop-file-validate for stricter checking 
    # if ! desktop-file-validate "$1" 2>/dev/null; then 
    if [[ ! ($1 = *.desktop && -f $1 && -r $1 && -s $1) ]]; then 
     echo "ERROR: you have not supplied valid .desktop file" >&2 
     exit 1 
    fi 

    # ensure the temporary launcher is deleted upon exit 
    trap 'rm "$launcherfile" 2>/dev/null' EXIT 

    launcherfile=$(mktemp -p "$appdir" "$template") 
    launchername=${launcherfile##*/} 

    if cp "$1" "$launcherfile" 2>/dev/null; then 
     gtk-launch "$launchername" "${@:2}" 
    else 
     echo "ERROR: failed to copy launcher to applications directory" >&2 
     exit 1 
    fi 

    exit 0 
    ) 
} 

你可以使用它像這樣(也沿着其他參數或URI的,如果你想通過):或者

launch ./path/to/shortcut.desktop 

,我寫了概括所有的方式來回答here啓動。桌面文件。它提供了gtk-launch的一些替代方案,可能有所幫助。

+0

Upvoted上介紹技術優點;但應該接受正確的答案是一個由伊格納西奧巴斯克斯 - 艾布拉姆斯。 – tripleee 2015-08-23 06:12:22

相關問題