你需要通过AppleScript(即。
windowSize
)作为输入
Popen.communicate()
:
例子:
from subprocess import Popen, PIPE
app = "Terminal"
windowSize = '''
tell application "%(app)s"
get size of window 1
end tell
''' % {'app': app}
proc = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
wSize, error = proc.communicate(windowSize)
print("Window size is: " + wSize)
笔记
:
-
在你的
窗口大小
AppleScript不需要
tell application "System Events" to tell ...
-你可以
tell application "%(app)s"
相反。但是,你的AppleScript仍然有效假设
已启用对辅助设备的访问
在系统首选项中。
-
这会将如下内容记录到控制台:
Window size is: 487, 338
你可以考虑利用
str.replace()
在你的
print
替换逗号的语句(
,
)用一个
x
. 例如,改变
打印
上述要点声明如下:
print("Window size is: " + wSize.replace(",", " x"))
将打印如下内容:
Window size is: 487 x 338
-
如果您想替换上面以
proc
和
wSize
)用一行(类似于OP)替换为以下内容:
(wSize, error) = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True).communicate(windowSize)
-
去拿窗户
宽度
和
高度
作为两个独立的变量,您可以随后使用
str.split()
方法来拆分
大小
变量(使用字符串
", "
作为分隔符)。例如:
# ...
wWidth = wSize.split(", ")[0]
wHeight = wSize.split(", ")[1]
print("Window width is: " + wWidth)
print("Window height is: " + wHeight)