0
使用psutil模塊我從那裏檢索正在運行的應用程序的列表,我檢查這些應用程序的打開「.plist」文件。然後,我閱讀'.plist'文件以獲取當前在應用程序中打開的文檔標題('NSTitle')。獲取在OSX上打開的文檔的名稱(python)
有沒有更好的/優化的方式來完成這個相同的任務?
import psutil
import os
import plistlib
def check_files(application):
plist_original_path = ""
while True:
for i in psutil.process_iter():
try:
if application in i.name():
for j in i.open_files():
if ".plist" in j.path:
plist_original_path = j.path
except psutil.ZombieProcess:
continue
except psutil.NoSuchProcess:
continue
try:
with open(plist_original_path, 'rb') as plist:
read_plist = plistlib.load(plist)
for i in read_plist:
try:
title = i["NSTitle"]
print(title)
except:
pass
except FileNotFoundError:
pass
check_files("Excel")