2016-04-28 11 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

私は、AppleScriptを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() 
関連する問題