2010-11-29 6 views
2

SparseBundleイメージをマウントするためにAppleScriptと書きました。Time Machineが起動したときに正確に実行します。タイムマシンはon idle文を使用してAppleScriptで実行されている場合Mac OS Xでアプリケーション起動イベントを聞くには?

今、私は定期的に確認してください。

on idle 
    .... 
    return <interval> 
end idle 

堅牢な方法ではありませんています。私の意見では、Application Launchイベントのイベントトリガーを追加する方が良いアプローチになります。

助けてください。

Objective-CまたはPythonサンプルコード(私はPythonを好むでしょう)は歓迎以上です。

答えて

4

あなたがされて探しているもの、NSDistributedNotificationCenterまたはNSWorkspace、これらのココアのクラスは、アプリケーション・イベントの通知を掲示、ワークスペースについては、アプリケーションの起動のようなもの、ドライブの搭載など

PyObjCが必要です。これは基本的にappleのココアクラスのpythonバインディングです。ドキュメンテーションはウェブサイトではまばらですが、ドキュメンテーションは基本的にAppleのdocsと同じであるため、pyobjc apiとcocoa APIの違いだけが含まれています。あなたが目的のC APIがどのようにPythonに変換されるのか分かっているなら、あなたは良いことです。ここをクリックしてください:http://pyobjc.sourceforge.net/documentation/pyobjc-core/intro.html

以下の例は、Pythonを使用した分散通知をリッスンしています。以下のコードは、基本的にオブザーバーを追加し、iTunesの通知を待ち受けます。あなたは同様の構造に従うことができますが、代わりにNSWorkspaceのオブザーバを追加します。何を聞くべきかを理解するために、システムを通るすべての通知を表示するアプリケーションがあります。それはnotification watcherと呼ばれています。これを使用して、何を聞くべきかを把握します。目的のCコードをPythonに変換することもできます。以下のコードは

  1. やっている何

  2. PyObjC
  3. によって定義されるように、NSObjectのから継承して出て
  4. を作成し、実際の通知やプリントを渡される方法を定義し、新しいクラスを定義しますFoundation.NSDistributedNotificationCenterのインスタンスdefaultCenter
  5. 、すなわちを監視するために、クラス、通知を受信したときに呼び出されるメソッドを渡し、オブザーバーを登録したアプリケーションイベント&「com.apple.iTunes.playerInfoを」
  6. GetSongs
  7. のインスタンスを作成し
  8. は、イベントループ、Pythonの属性にアクセスすると同じ動作しない属性(Objective Cの属性)にアクセスする、あなたをつまずかます

一つのことを実行します。つまり、あなたがpythonでObjective Cのためclass_name.attを行うpythonで、あなたは下の私の例からすなわち関数のようにそれを呼び出す必要があり:song.userInfo()

import Foundation 
from AppKit import * 
from PyObjCTools import AppHelper 

class GetSongs(NSObject): 
    def getMySongs_(self, song): 
     print "song:", song 
     song_details = {} 
     ui = song.userInfo() 
     print 'ui:', ui 
     for x in ui: 
      song_details[x] = ui.objectForKey_(x) 
     print song_details 

nc = Foundation.NSDistributedNotificationCenter.defaultCenter() 
GetSongs = GetSongs.new() 
nc.addObserver_selector_name_object_(GetSongs, 'getMySongs:', 'com.apple.iTunes.playerInfo',None) 

NSLog("Listening for new tunes....") 
AppHelper.runConsoleEventLoop() 

ここでは(YES BRITNEYは!NOT ROCKS ...実際の出力の例です!;)

song NSConcreteNotification 0x104c0a3b0 {name = com.apple.iTunes.playerInfo; object = com.apple.iTunes.player; userInfo = { 
    Album = Circus; 
    "Album Rating" = 0; 
    "Album Rating Computed" = 1; 
    Artist = "Britney Spears"; 
    "Artwork Count" = 1; 
    Genre = Pop; 
    "Library PersistentID" = 8361352612761174229; 
    Location = "file://localhost/Users/izze/Music/iTunes/iTunes%20Music/Britney%20Spears/Circus/02%20Circus.mp3"; 
    Name = Circus; 
    PersistentID = 4028778662306031905; 
    "Play Count" = 0; 
    "Play Date" = "2010-06-26 08:20:57 +0200"; 
    "Player State" = Playing; 
    "Playlist PersistentID" = 7784218291109903761; 
    "Rating Computed" = 1; 
    "Skip Count" = 1; 
    "Skip Date" = "2010-06-26 12:20:57 +0200"; 
    "Store URL" = "itms://itunes.com/link?n=Circus&an=Britney%20Spears&pn=Circus"; 
    "Total Time" = 192444; 
    "Track Count" = 16; 
    "Track Number" = 2; 
}} 
ui { 
    Album = Circus; 
    "Album Rating" = 0; 
    "Album Rating Computed" = 1; 
    Artist = "Britney Spears"; 
    "Artwork Count" = 1; 
    Genre = Pop; 
    "Library PersistentID" = 8361352612761174229; 
    Location = "file://localhost/Users/izze/Music/iTunes/iTunes%20Music/Britney%20Spears/Circus/02%20Circus.mp3"; 
    Name = Circus; 
    PersistentID = 4028778662306031905; 
    "Play Count" = 0; 
    "Play Date" = "2010-06-26 08:20:57 +0200"; 
    "Player State" = Playing; 
    "Playlist PersistentID" = 7784218291109903761; 
    "Rating Computed" = 1; 
    "Skip Count" = 1; 
    "Skip Date" = "2010-06-26 12:20:57 +0200"; 
    "Store URL" = "itms://itunes.com/link?n=Circus&an=Britney%20Spears&pn=Circus"; 
    "Total Time" = 192444; 
    "Track Count" = 16; 
    "Track Number" = 2; 
} 
{u'Album Rating Computed': 1, u'Album': u'Circus', u'Rating Computed': True, u'Name': u'Circus', u'Artist': u'Britney Spears', u'Track Number': 2, u'Skip Date': 2010-06-26 12:20:57 +0200, u'Library PersistentID': 8361352612761174229L, u'Player State': u'Playing', u'Total Time': 192444L, u'Genre': u'Pop', u'Playlist PersistentID': 7784218291109903761L, u'Album Rating': 0, u'Location': u'file://localhost/Users/izze/Music/iTunes/iTunes%20Music/Britney%20Spears/Circus/02%20Circus.mp3', u'Skip Count': 1, u'Track Count': 16L, u'Artwork Count': 1, u'Play Date': 2010-06-26 08:20:57 +0200, u'PersistentID': 4028778662306031905L, u'Play Count': 0, u'Store URL': u'itms://itunes.com/link?n=Circus&an=Britney%20Spears&pn=Circus'} 
3

これはObjc-Cではあまり難しくありません。 NSWorkspaceおよびNSNotificationCenterを使用して、すべてのアプリケーションの通知にアクセスできます。オブジェクトを作成し、NSWorkspaceDidTerminateApplicationNotification型の通知用のメソッドを登録します。次のようなものがあります。

@interface NotificationObserver : NSObject { } 
- (void) applicationDidLaunch:(NSNotification*)notification; 
@end 

@implementation NotificationObserver : NSObject 
- (void) applicationDidLaunch:(NSNotification*)notification 
{ 
    // Check the notification to see if Time Machine is being launched. 
} 
@end 

void watch(void) 
{ 
    NSNotificationCenter* notificationCenter 
    = [[NSWorkspace sharedWorkspace] sharednotificationCenter]; 
    NotificationObserver* observer = [[NotificationObserver alloc] init]; 
    [notificationCenter addObserver:observer 
         selector:@selector(applicationDidTerminate:) 
          name:@"NSWorkspaceDidTerminateApplicationNotification" 
          object:nil]; 
} 
0

これはあなたの質問に対する回答ではありませんが、問題を解決する可能性があります。

アップルスクリプトがディスクイメージをマウントした後にTime Machineを起動させるだけではないのはなぜですか?その後、Time Machineを直接起動するのではなく、常にスクリプトを使用してTime Machineを起動します。あなたはAppleScriptファイルにTime Machineアイコンを貼り付けることもでき、 "Time Machine"という名前で幻想を完成させることができます。 pythonでこれを行うに:-)

関連する問題