web-dev-qa-db-ja.com

nautilusの新しいタブで特定のディレクトリを開くコマンドは何ですか?

デフォルトのファイルマネージャのフォルダを新しいタブで開く.desktopファイルを作成したいのですが。それが正しい場所を開いたかどうかをテストするために、私はnautilus ~/*folder*を試しました。ファイルマネージャーが開きますが、問題が発生したと表示されます。 nautilus /home/*username*/*folder*を使用しても同じことが起こります。 .desktopファイルから、ホームの特定のフォルダーを新しいタブで開くには、どのコマンドを使用できますか?

私の目標は、Nautilusの新しいタブでマップ〜/ Downloadsを開くアイコンをドック(板)に置くことです。

1
Maud Kon

編集:

継続的な洞察の結果として、スクリプトの改良版を以下に示します。

スクリプトがtypingパスではなく貼り付けパスになっているため(xdotoolを使用)、スクリプトの信頼性が高まります。スクリプトは実際には高速ではありませんが(nautilusのcliオプションがないため、回避策is回避策と考えてください)、スクリプトはより「クリスピー」に感じます。ただし、スクリプトはユーザーのアクションをシミュレートするため、スクリプトはGUIが次の各ステップの準備ができるのを待つだけでよいことに変わりはありません。スクリプト内のタイミングは保存側です。より高速なシステムでは、いくつかの最適化を実行できる場合があります。

元の回答で説明されているとおりにスクリプトを使用してください。 (まだ)wmctrlxdotoolの両方をインストールする必要があります。このバージョンでは、さらにxclipが必要です。

Sudo apt-get install xclip

スクリプトの新しいバージョン:

#!/usr/bin/env python3
import subprocess
import sys
import time

arg = sys.argv[1:]
arg = arg[0] if arg else ""

def get(cmd):
    return subprocess.check_output(cmd).decode("utf-8").strip()

try:
    pid = get(["pidof", "nautilus"])
except subprocess.CalledProcessError:
    wlist = []    
else:
    wlist = [l for l in get(["wmctrl", "-lp"]).splitlines() if pid in l\
             and "_NET_WM_WINDOW_TYPE_NORMAL" in get(["xprop", "-id", l.split()[0]])]

if wlist:
    w = wlist[-1].split()[0]
    subprocess.call(["wmctrl", "-ia", w])
    subprocess.call(["/bin/bash", "-c", "printf '"+arg+"' | xclip -selection clipboard"])
    subprocess.Popen(["xdotool", "key", "Control_L+t"])
    time.sleep(0.4)
    subprocess.Popen(["xdotool", "key", "Control_L+l"])
    time.sleep(0.2)
    subprocess.call(["xdotool", "key", "Control_L+v"])
    subprocess.Popen(["xdotool", "key", "Return"])

else:
    subprocess.Popen(["nautilus", arg])



古い答え:

Nautilusには、新しいタブを開くコマンドラインオプションがありませんが、xdotoolおよびwmctrlを使用して、スクリプトの助けを借りて「偽造」することができます。

使い方

  1. 必要に応じて、wmctrlxdotoolの両方をインストールします。

    Sudo apt-get install xdotool wmctrl
    
  2. 以下のスクリプトを空のファイルにコピーし、nautilus_tab~/bin(拡張子なし)として保存し、実行可能にするにします。
    ディレクトリ~/binがまだ存在しない場合は、それを作成し、source ~/.profileを実行して、ディレクトリを$PATHに「表示」します。 (または、ログアウト/ログイン)

  3. 次のコマンドを実行して、テストを実行します。

    nautilus_tab <directory>
    

    そうすべき:

    • nautilusウィンドウが開いていない場合は、ディレクトリに新しいnautilusウィンドウを開きます
    • ウィンドウis openの場合、ディレクトリに新しいタブを開きます

スクリプト

#!/usr/bin/env python3
import subprocess
import time
import sys

get = lambda cmd: subprocess.check_output(cmd).decode("utf-8")
def run(cmd):
    subprocess.call(cmd)

try:
    arg = sys.argv[1]
except:
    arg = ""

try:
    pid = get(["pidof", "nautilus"]).strip()
except subprocess.CalledProcessError:
    run(["nautilus ", arg])
else:
    w = [l.split() for l in get(["wmctrl", "-lp"]).splitlines() if pid in l][-1]
    print(w)
    w_id = w[0]   
    if len([l for l in get(["xprop", "-id", w_id]).splitlines() if all(
        ["_NET_WM_WINDOW_TYPE(ATOM)" in l, "_TYPE_NORMAL" in l])]) != 0:
        run(["wmctrl", "-ia", w[0]]); time.sleep(0.1)
        run(["xdotool", "key", "Control_L+t"])
        if arg != "":
            run(["xdotool", "key", "Control_L+l"])
            time.sleep(0.2)
            run(["xdotool", "type", arg])
            time.sleep(0.02*len(arg))
            run(["xdotool", "key", "Return"])
    else:
        run(["nautilus", arg])

ノート

  • スクリプトは、ユーザーアクションをシミュレートするための回避策です。その結果、スクリプトの「内部」でのタイミングが重要になります。システムでタイミングを「安全」な値に設定し、可能であれば「スマート」にしました。システムで正しく機能しない場合は、コメントを残してください。
  • (タブを追加する)対象のウィンドウが別のワークスペースにある場合、スクリプトはおそらくブレーキをかけます
  • スペースを含むディレクトリの場合は、引用符を使用します。

    nautilus_tab '</directory/with spaces>'
    

引数(-directory)を使用しない場合、スクリプトは現在開いているnautilusウィンドウと同じディレクトリに新しいタブを開きます。 「nautilus」ウィンドウが開かれていない場合は、ホームウィンドウに新しいウィンドウが開きます。

3
Jacob Vlijm