2016-04-28 2 views
0

Verwenden von Psutil-Modul Ich lade eine Liste der laufenden Anwendungen, von dort ich für diese Dateien ".plist" Dateien öffnen. Dann lese ich die '.plist' Dateien, um den Titel ('NSTitle') von Dokumenten zu erhalten, die gerade in der Anwendung geöffnet sind.Name des Dokuments in Anwendung auf OSX (Python) öffnen

Gibt es einen besseren/optimierten Weg, um diese gleiche Aufgabe zu erfüllen?

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") 

Antwort

0

Ich habe es geschafft, einen besseren Weg mit AppleScript mit Python zu finden.

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() 
Verwandte Themen