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

Cocoa API解説(macOS/iOS)

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

目次

NSWorkspaceDidMountNotification

INDEX>AppKit>NSWorkspace

マウントした時の通知

解説

フレームワーク

ApplicationKit

クラス

NSWorkspace

使用可能

10.6-

更新時のバージョン

OS X 10.10.3
Swift1.2

参照

関連記事(外部サイト)

例文

Objective-C

Swift

    //NSWorkspace NSWorkspaceDidRenameVolumeNotification
    //NSWorkspace NSWorkspaceDidMountNotification
    
    var switch053 : Bool = false
    func notify053(notify:NSNotification) {
        //NSLog("!! %@",notify.description)
        
        //マウントされたとき
        if notify.name == NSWorkspaceDidMountNotification {
            //NSLog("!! %@",notify.description)
            let devicePath : String = (notify.userInfo! as Dictionary)["NSDevicePath"]! as! String
            NSLog("device path = %@",devicePath)
            // =>device path = /Volumes/ディスクイメージ
            let localizedName : String = (notify.userInfo! as Dictionary)["NSWorkspaceVolumeLocalizedNameKey"]! as! String
            NSLog("localized new name = %@",localizedName)
            // =>localized new name = ディスクイメージ
            let newURL : NSURL = (notify.userInfo! as Dictionary)["NSWorkspaceVolumeURLKey"]! as! NSURL
            NSLog("new URL = %@",newURL.path!)
            // =>new URL = /Volumes/ディスクイメージ

        }

        if notify.name == NSWorkspaceWillUnmountNotification {
        }
        if notify.name == NSWorkspaceDidUnmountNotification {
        }
        if notify.name == NSWorkspaceDidRenameVolumeNotification {
            NSLog("NSWorkspaceDidRenameVolumeNotification")
            
            let localizedName : String = (notify.userInfo! as Dictionary)["NSWorkspaceVolumeLocalizedNameKey"]! as! String
            NSLog("localized new name = %@",localizedName)
            // =>localized new name = ディスクイメージ2
            let localizedOldName : String = (notify.userInfo! as Dictionary)["NSWorkspaceVolumeOldLocalizedNameKey"]! as! String
            NSLog("localized old name = %@",localizedOldName)
            // =>localized old name = ディスクイメージ1
            
            let newURL : NSURL = (notify.userInfo! as Dictionary)["NSWorkspaceVolumeURLKey"]! as! NSURL
            NSLog("new URL = %@",newURL.path!)
            // =>new URL = /Volumes/ディスクイメージ2
            let oldURL : NSURL = (notify.userInfo! as Dictionary)["NSWorkspaceVolumeOldURLKey"]! as! NSURL
            NSLog("old URL = %@",oldURL.path!)
            // =>old URL = /Volumes/ディスクイメージ1
        }

        
    }
    @IBAction func function053(sender: AnyObject) {
        let notificationName1 : String = NSWorkspaceDidRenameVolumeNotification
        let notificationName2 : String = NSWorkspaceDidMountNotification
        let notificationName3 : String = NSWorkspaceWillUnmountNotification
        let notificationName4 : String = NSWorkspaceDidUnmountNotification
        let notificationSelector : Selector = Selector("notify053:")
        
        if switch053 {
            //通知の監視をやめる
            NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self,
                name: notificationName1, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self,
                name: notificationName2, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self,
                name: notificationName3, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.removeObserver(self,
                name: notificationName4, object: nil )
            NSLog("--------observe off")
            switch053 = false
            
        }else{
            //通知の監視を始める
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                selector: notificationSelector, name: notificationName1, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                selector: notificationSelector, name: notificationName2, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                selector: notificationSelector, name: notificationName3, object: nil )
            NSWorkspace.sharedWorkspace().notificationCenter.addObserver(self,
                selector: notificationSelector, name: notificationName4, object: nil )
            NSLog("--------observe on")
            switch053 = true
        }
    }