2016-04-28 63 views
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") 

回答

0

我設法找到一個更好的方式使用蘋果與python。

import subprocess 
import time 


x = '' 
while 1: 
    time.sleep(1) 

    with open('/Link/to/folder/app_script.txt', 'rb') as appscript: 
     x = appscript.read() 

    p = subprocess.Popen(['osascript', '-'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 
    stdout, stderr = p.communicate(x) 

    x = stdout.split(b',') 

    print(x[0].decode().strip()) 
    print(x[1].decode().strip()) 
    print() 
相關問題