読者です 読者をやめる 読者になる 読者になる

Cocoa API解説(macOS/iOS)

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。

目次

NSWorkspaceDidDeactivateApplicationNotification

INDEX>AppKit>NSWorkspace

アプリケーションがアクティブな状態でなくなった時の通知

解説

フレームワーク

ApplicationKit

クラス

NSWorkspace

使用可能

10.6-

更新時のバージョン

OS X 10.10.3
Swift1.2

参照

関連記事(外部サイト)

例文

Objective-C

Swift

   //NSWorkspace NSWorkspaceDidActivateApplicationNotification
    //NSWorkspace NSWorkspaceDidDeactivateApplicationNotification
    var switch052 : Bool = false
    func notify052(notify:NSNotification) {
        //NSLog("!! %@",notify.description)
        
        if notify.name == NSWorkspaceDidActivateApplicationNotification {
            NSLog("NSWorkspaceDidActivateApplicationNotification")
                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)
                // ->unningApp = 2015-04-30 22:46:02 +0000

        }
        if notify.name == NSWorkspaceDidDeactivateApplicationNotification {
            NSLog("NSWorkspaceDidDeactivateApplicationNotification")
                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)
                //エラーが出る
        }
    }
    @IBAction func function052(sender: AnyObject) {
        let notificationName1 : String = NSWorkspaceDidActivateApplicationNotification
        let notificationName2 : String = NSWorkspaceDidDeactivateApplicationNotification
        let notificationSelector : Selector = Selector("notify052:")
        
        if switch052 {
            //通知の監視をやめる
            NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self,
                name: notificationName1, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self,
                name: notificationName2, object: nil )
            NSLog("--------observe off")
            switch052 = false
            
        }else{
            //通知の監視を始める
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                selector: notificationSelector, name: notificationName1, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                selector: notificationSelector, name: notificationName2, object: nil )
            NSLog("--------observe on")
            switch052 = true
        }
    }