Introduction
转自自己的GitHub:
DesktopSwitcher is an OSX application written in swift, which is served as an tool that can switching the desktop by clicking while scrolling the scrolling wheel on the mouse. The tool can especially bring convenience to those who seldom uses trackpad and intensively relies on mouse and keyboard. If you have any suggestion or advise, please feel free to contact me.
项目创建
不要使用storyboard
鼠标的监听
在APPDelegate中:
1 | NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.otherMouseUp) { (event) in |
以此类推,具体详见API。
Preference界面
创建WindowController,并且生成Xib。具体干活的方法和iOS差不多,就是代码要加点东西(windowNibName):
1 | // |
然后再APPDelegate当中:
1 | private var preferenceWindow = PreferenceWindowController() |
Info.plist修改
由于是任务栏app,所以图标什么的不要出现。在Info.plist中添加:
1 | Application is agent (UIElement): YES |
桌面切换的AppleScript实现
使用AppleScript封装好App,比方说,切换到上一个桌面就是:
1 | tell application "System Events" |
以此类推。具体源码详见我的GitHub,script可以通过解压app文件看到。
最后在Swift当中:
1 | NSWorkspace.shared.launchApplication("DLeft") |
其中,DLeft
是封装好的应用名。