WriteTyper - 在Mac上體驗舊式打字機的觸感

NoisyTyper

首先得说一下WriteTyper的前身NoisyTyper,一开始我用的就是这个。

NoisyTyper

NoisyTyper是一款能够让你一遍打字一边发出以前旧电影里老式打字机的声音的Mac App,声音相当的不错,十分耐听。然而随着时间的老去,系统的不断更新,NoisyTyper开始渐渐的力不从心,不再适用于当前系统。于是乎,本君只好压榨不多的时间,让WriteTyper应运而生。

WriteTyper

NoisyTyper 是用OC写的,为了顺应时代的召唤,我决定,参照NoisyTyper的代码,用Swift 2.0来写WriteTyper。对于Mac桌面应用,小生第一次写表示怕怕,现在其实没那么多的不同,只要将UIxxx的思维转换成NSxxx就已经一只脚踏入大门了。

最后WriteTyper的成品如下图,也就是说,我们要做的是一个menubar app而非window app。

WriteTyper

几点笔记

申请权限:

    // システム環境設定に設定変更を依頼する
    func acquirePrivileges() -> Bool {
        let accessEnabled = AXIsProcessTrustedWithOptions(
            [kAXTrustedCheckOptionPrompt.takeUnretainedValue() as String: true])
        if !accessEnabled {
            print("You need to enable the WriteTyper in the System Prefrences")
        }
        return accessEnabled
    }

显示/隐藏Dock Icon:

func toggleDockIcon(showIcon state: Bool) -> Bool {
    var result: Bool
    if state {
        result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.Regular)
    }
    else {
        result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.Accessory)
    }
    return result
}

全局键盘事件监听:

       if isAcquirePrivileges {
            NSEvent.addGlobalMonitorForEventsMatchingMask(
                NSEventMask.KeyDownMask, handler: {(theEvent: NSEvent) in
                    let key = theEvent.keyCode
                    self.playSoundsByKey(key)
            })
        } 

状态栏上的pop view:

    let statusItem = NSStatusBar.systemStatusBar().statusItemWithLength(30)
    let popover = NSPopover()
    if let button = statusItem.button {
        button.image = NSImage(named: "typewriter")
        button.action = Selector("togglePopover:")
    }
    popover.contentViewController = SettingViewController(nibName: "SettingViewController", bundle: nil)
    ...

    func showPopover(sender: AnyObject?) {
        if let button = statusItem.button {
            popover.showRelativeToRect(button.bounds, ofView: button, preferredEdge: .MinY)
        }
    }
    
    func closePopover(sender: AnyObject?) {
        popover.performClose(sender)
    }

来自 stackoverflow 的搬运

下面设置开机自启的代码来自stackoverflow,稍作修改,替换了过时的API:

    func applicationIsInStartUpItems() -> Bool {
        return (itemReferencesInLoginItems().existingReference != nil)
    }
    
    func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItemRef?, lastReference: LSSharedFileListItemRef?) {
        if let appUrl : NSURL = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) {
            let loginItemsRef = LSSharedFileListCreate(
                nil,
                kLSSharedFileListSessionLoginItems.takeRetainedValue(),
                nil
                ).takeRetainedValue() as LSSharedFileListRef?
            if loginItemsRef != nil {
                let loginItems: NSArray = LSSharedFileListCopySnapshot(loginItemsRef, nil).takeRetainedValue() as NSArray
                //print("There are \(loginItems.count) login items")
                let lastItemRef: LSSharedFileListItemRef = loginItems.lastObject as! LSSharedFileListItemRef
                for var i = 0; i < loginItems.count; ++i {
                    let currentItemRef: LSSharedFileListItemRef = loginItems.objectAtIndex(i) as! LSSharedFileListItemRef
                    
                    if let resUrl = LSSharedFileListItemCopyResolvedURL(currentItemRef, 0, nil){
                        let urlRef: NSURL = resUrl.takeRetainedValue()
                        //print("URL Ref: \(urlRef.lastPathComponent!)")
                        if urlRef.isEqual(appUrl) {
                            return (currentItemRef, lastItemRef)
                        }
                    } else {
                        //print("Unknown login application")
                    }
                }
                //The application was not found in the startup list
                return (nil, lastItemRef)
            }
        }
        return (nil, nil)
    }
    
    func toggleLaunchAtStartup() {
        let itemReferences = itemReferencesInLoginItems()
        let shouldBeToggled = (itemReferences.existingReference == nil)
        let loginItemsRef = LSSharedFileListCreate(
            nil,
            kLSSharedFileListSessionLoginItems.takeRetainedValue(),
            nil
            ).takeRetainedValue() as LSSharedFileListRef?
        if loginItemsRef != nil {
            if shouldBeToggled {
                if let appUrl : CFURLRef = NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath) {
                    LSSharedFileListInsertItemURL(
                        loginItemsRef,
                        itemReferences.lastReference,
                        nil,
                        nil,
                        appUrl,
                        nil,
                        nil
                    )
                    print("Application was added to login items")
                }
            } else {
                if let itemRef = itemReferences.existingReference {
                    LSSharedFileListItemRemove(loginItemsRef,itemRef);
                    print("Application was removed from login items")
                }
            }
        }
    }

refs: http://stackoverflow.com/questions/26475008/swift-getting-a-mac-app-to-launch-on-startup

丝滑般的快感

本文就是伴着 WriteTyper 一字一字发出的老式打字机的声音写下的,全文行云流水,酣畅淋漓,最后那感觉更不知与谁言说。总的来说,上手体验还是不错的,对于经常码字的同志,我相信,这会是提高生产力的必备神器。

最后点评,每当别人使用嘈杂不已的机械键盘浑然天成时,你应该默默的拿出本本,用打字机的声音和他一干到底。

欢迎前往下载与Star。
官网:https://urinx.github.io/app/writetyper
Github:https://github.com/Urinx/WriteTyper

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容