古い回答:

古い回答:

私はマルチタッチを使用しており、同じジェスチャーでさまざまなアプリケーションでさまざまな操作を実行しようとしています。

Python スクリプトがあり、基本的な動作をします。

ボットはどのようにしてアプリケーションを選択できるのでしょうか? アクティブ ウィンドウのタイトルを取得するにはどうすればよいでしょうか?

ありがとう

システム情報を編集します:

  • Python 2.7.6
  • Ubuntu 14.04 (ユニティ)

答え1

以下は更新されたバージョンです。投票を獲得した回答を削除しないために、古い回答はそのまま残しておきます。

#!/usr/bin/env python3

import gi
gi.require_version("Wnck", "3.0")
from gi.repository import Wnck

scr = Wnck.Screen.get_default()
scr.force_update()
print(scr.get_active_window().get_name())

またはxidを取得します:

print(scr.get_active_window().get_xid())

または(それほど驚くことではありませんが)pid を取得します。

print(scr.get_active_window().get_pid())

こちらもご覧くださいWnck.Window メソッド


古い回答:

xpropまたはxwitの出力を解析するだけです(最初にwmctrlインストールする必要があるかもしれません:)。xpropはwmctrlsudo apt-get install wmctrlたくさんウィンドウに関する情報。

xprop -root

アクティブウィンドウに関する情報、ウィンドウID、

wmctrl -l

現在開いているウィンドウのリストが表示されます。 この-pオプションを使用すると、ウィンドウが属する PID の情報も表示されます。 これらを組み合わせることで、必要な情報をすべて取得できます。

例えば:

Python 3 では、サブプロセス check_output() を使用します。

アクティブウィンドウ(ID)を取得するには:

-xpropを使用する

# [1]
import subprocess

command = "xprop -root _NET_ACTIVE_WINDOW | sed 's/.* //'"
frontmost = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").strip()

print(frontmost)
> 0x38060fd

-xprop を使用し、Python 内で解析する

# [2]
import subprocess

command = "xprop -root _NET_ACTIVE_WINDOW"
frontmost = subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").strip().split()[-1]

print(frontmost)
> 0x38060fd

ウィンドウ ID を取得したら、wmctrl を使用して、そのウィンドウが属するアプリケーション (の PID) を取得します。

注意: まず、上記のコマンドの最前面の ID (出力) を wmctrl 用に「修正」する必要があります。wmctrl と xprop の ID は若干異なります。

0x381e427 (xprop)
0x0381e427 (wmctrl)

上記の関数の出力を修正するには(# [1]またはの「最前面」出力を使用# [2]):

fixed_id = frontmost[:2]+"0"+frontmost[2:]

次に、最前面のウィンドウ(のアプリケーション)の pid を取得します。

command = "wmctrl -lp"
window_pid = [l.split()[2] for l in subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8").splitlines() if fixed_id in l][0]

> 6262


Python 2 では、subprocess.Popen() を使用します。

Python 2 では、subprocess.check_output は利用できないため、手順が若干異なり、少し冗長になります。

アクティブウィンドウ(ID)を取得するには:

-xpropを使用する

# [1]
import subprocess

command = "xprop -root _NET_ACTIVE_WINDOW"                                       
output = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE)
frontmost = output.communicate()[0].decode("utf-8").strip().split()[-1]

print frontmost
> 0x38060fd

wmctrlと以下の出力を使用して、それが属するアプリケーションの(PID)を取得する。# [1]

-(再び)次の出力を使用(および修正)します[1]

# [2]
import subprocess

fixed_id = frontmost[:2]+"0"+frontmost[2:]

command = "wmctrl -lp"
output = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE)
window_pid = [l.split()[2] for l in output.communicate()[0].decode("utf-8").splitlines() if fixed_id in l][0]

print window_pid # pid of the application
> 6262

窓を手に入れる名前wmctrlおよび の出力を使用します# [1]socket.gethostname()の出力をwmctrlマシン名で分割するためにも を使用します)

# [3]
import subprocess
import socket

command = "wmctrl -lp"
output = subprocess.Popen(["/bin/bash", "-c", command], stdout=subprocess.PIPE)
window_list = output.communicate()[0].decode("utf-8")
window_name = [l for l in window_list.split("\n") if fixed_id in l][0].split(socket.gethostname()+" ")[-1]

print window_name

男xprop
男 wmctrl
男はエックスウィット

関連情報