이전 답변:

이전 답변:

저는 멀티터치로 작업하고 있으며 동일한 동작으로 다양한 애플리케이션에서 다양한 작업을 수행하려고 합니다.

나는 파이썬 스크립트와 기본 작업을 가지고 있습니다.

Bot 애플리케이션 중에서 어떻게 결정할 수 있나요? 활성 창 제목을 얻는 방법은 무엇입니까?

감사해요

시스템 정보 편집:

  • 파이썬 2.7.6
  • 우분투 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

window-id가 있으면 wmctrl을 사용하여 이것이 속한 애플리케이션(pid)을 가져옵니다.

주의: 먼저 wmctrl에 대한 위 명령의 맨 앞부분 ID(출력)를 "수정"해야 합니다. 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
남자 xwit

관련 정보