他のアプリケーションの動作を見る [Swift][Mac]
他のアプリが起動しているかどうかはNSWorkspaceのnotificationで知ることができる。
NSWorkspaceの共有インスタンスの通知センターに対して、通知を登録
以下の場合は、NSWorkspaceDidTerminateApplicationNotificationを登録しているので、アプリケーションの終了を通知してもらう。
通知が来たら、
func notifyNSWorkspaceDidTerminateApplicationNotification(notify:NSNotification) {}に飛ぶようにセレクタを設定。
let notificationName : String = NSWorkspaceDidTerminateApplicationNotification let notificationSelector : Selector = Selector("notifyNSWorkspaceDidTerminateApplicationNotification:") //通知の監視を始める NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: notificationSelector, name: notificationName, object: nil )
飛んできた通知には[NSObject : AnyObject]でuserInfoが含まれているので、それぞれのキー(例えばバンドルIDだったらNSApplicationBundleIdentifier)で値を取り出し。
let bundleID : String = (notify.userInfo! as Dictionary)["NSApplicationBundleIdentifier"]! as! String NSLog("Bundle ID = %@",bundleID) // -> Bundle ID = com.apple.mail
数値が含まれている場合はNSNumberになっている。
let appProcessID : NSNumber = (notify.userInfo! as Dictionary)["NSApplicationProcessIdentifier"]! as! NSNumber NSLog("process id = %d",appProcessID.integerValue) // -> process id = 36580
起動中のアプリケーションはNSRunningApplicationクラスで返される(10.6以降)ので、そこから起動開始時間などの情報が得られる。
let runningApp : NSRunningApplication = (notify.userInfo! as Dictionary)["NSWorkspaceApplicationKey"]! as! NSRunningApplication NSLog("runningApp = %@",runningApp.description) // ->runningApp = <NSRunningApplication: 0x600000105340 (com.apple.mail - 36867)> NSLog("runningApp = %@",runningApp.launchDate!.description) // ->runningApp = 2015-04-29 09:18:27 +0000
更新時のバージョン
OS X 10.10.3
Swift1.2
参照
NSWorkspaceWillLaunchApplicationNotification
NSWorkspaceDidLaunchApplicationNotification
NSWorkspaceDidTerminateApplicationNotification
NSWorkspaceSessionDidBecomeActiveNotification
NSWorkspaceSessionDidResignActiveNotification
NSWorkspaceDidHideApplicationNotification
NSWorkspaceDidUnhideApplicationNotification
NSWorkspaceDidActivateApplicationNotification
NSWorkspaceDidDeactivateApplicationNotification
NSWorkspaceDidRenameVolumeNotification
NSWorkspaceDidMountNotification
NSWorkspaceWillUnmountNotification
NSWorkspaceDidUnmountNotification
NSWorkspaceDidPerformFileOperationNotification
NSWorkspaceDidChangeFileLabelsNotification
NSWorkspaceActiveSpaceDidChangeNotification
NSWorkspaceDidWakeNotification
NSWorkspaceWillPowerOffNotification
NSWorkspaceWillSleepNotification
NSWorkspaceScreensDidSleepNotification
NSWorkspaceScreensDidWakeNotification
関連記事(外部サイト)
例文
//NSWorkspace NSWorkspaceDidTerminateApplicationNotification //アプリケーションが終了しようとするとき var switch049 : Bool = false func notifyNSWorkspaceDidTerminateApplicationNotification(notify:NSNotification) { //NSLog("%@",notify.description) let bundleID : String = (notify.userInfo! as Dictionary)["NSApplicationBundleIdentifier"]! as! String NSLog("Bundle ID = %@",bundleID) // -> Bundle ID = com.apple.mail let appName : String = (notify.userInfo! as Dictionary)["NSApplicationName"]! as! String NSLog("application name = %@",appName) // -> application name = メール let appPathe : String = (notify.userInfo! as Dictionary)["NSApplicationPath"]! as! String NSLog("application path = %@",appPathe) // -> application path = /Applications/Mail.app let appProcessID : NSNumber = (notify.userInfo! as Dictionary)["NSApplicationProcessIdentifier"]! as! NSNumber NSLog("process id = %d",appProcessID.integerValue) // -> process id = 36580 let appProcessSerialNumberHigh : NSNumber = (notify.userInfo! as Dictionary)["NSApplicationProcessSerialNumberHigh"]! as! NSNumber NSLog("process serial number high = %d",appProcessSerialNumberHigh.integerValue) // ->process serial number high = 0 let appProcessSerialNumberLow : NSNumber = (notify.userInfo! as Dictionary)["NSApplicationProcessSerialNumberLow"]! as! NSNumber NSLog("process serial number low = %d",appProcessSerialNumberLow.integerValue) // ->process serial number low = 5145832 let runningApp : NSRunningApplication = (notify.userInfo! as Dictionary)["NSWorkspaceApplicationKey"]! as! NSRunningApplication NSLog("runningApp = %@",runningApp.description) // ->runningApp = <NSRunningApplication: 0x600000105340 (com.apple.mail - 36867)> NSLog("runningApp = %@",runningApp.launchDate!.description) // ->runningApp = 2015-04-29 09:18:27 +0000 let exitStatus : NSNumber = (notify.userInfo! as Dictionary)["NSWorkspaceExitStatusKey"]! as! NSNumber NSLog("exit status = %d",appProcessSerialNumberHigh.integerValue) // ->exit status = = 0 } @IBAction func function049(sender: AnyObject) { let notificationName : String = NSWorkspaceDidTerminateApplicationNotification let notificationSelector : Selector = Selector("notifyNSWorkspaceDidTerminateApplicationNotification:") if switch049 { //通知の監視をやめる NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self, name: notificationName, object: nil ) NSLog("--------observe off") switch049 = false }else{ //通知の監視を始める NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self, selector: notificationSelector, name: notificationName, object: nil ) NSLog("--------observe on") switch049 = true } }