gyro永不抽风

ああああああああああああああああおおおおおおおおおおおおおおおお

DesktopSwitcher 开发

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
2
3
NSEvent.addGlobalMonitorForEvents(matching: NSEvent.EventTypeMask.otherMouseUp) { (event) in
// Code
}

以此类推,具体详见API。

Preference界面

创建WindowController,并且生成Xib。具体干活的方法和iOS差不多,就是代码要加点东西(windowNibName):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//
// PreferenceWindowController.swift
// DesktopSwitcher
//
// Created by Jefferson Qin on 2020/4/9.
// Copyright © 2020 Jefferson Qin. All rights reserved.
//

import Cocoa

class PreferenceWindowController: NSWindowController {

@IBOutlet var desktopTextField: NSTextField!

@IBOutlet var externalDesktopTextField: NSTextField!

@IBAction func configureButtonTouched(_ sender: Any) {
// code
}

override var windowNibName : String! {
return "PreferenceWindowController"
}

override func windowDidLoad() {
super.windowDidLoad()

// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
self.window?.center()
self.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
}

}

然后再APPDelegate当中:

1
2
3
4
5
private var preferenceWindow = PreferenceWindowController()

@IBAction func preferenceButtonClicked(_ sender: NSMenuItem) {
preferenceWindow.showWindow(sender)
}

Info.plist修改

由于是任务栏app,所以图标什么的不要出现。在Info.plist中添加:

1
Application is agent (UIElement): YES

桌面切换的AppleScript实现

使用AppleScript封装好App,比方说,切换到上一个桌面就是:

1
2
3
tell application "System Events"
key code 123 using {control down}
end tell

以此类推。具体源码详见我的GitHub,script可以通过解压app文件看到。

最后在Swift当中:

1
NSWorkspace.shared.launchApplication("DLeft")

其中,DLeft是封装好的应用名。

GitHub

DesktopSwitcher

__EOF__
-------------本文结束感谢您的阅读-------------

本文标题:DesktopSwitcher 开发

文章作者:gyro永不抽风

发布时间:2020年04月10日 - 23:04

最后更新:2020年09月15日 - 07:09

原始链接:http://gyrojeff.moe/2020/04/10/DesktopSwitcher-%E5%BC%80%E5%8F%91/

许可协议: 署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0) 转载请保留原文链接及作者!

真的不买杯奶茶吗?T^T

欢迎关注我的其它发布渠道