• DesktopSwitcher 开发


    新博客地址:https://gyrojeff.top,欢迎访问! 本文为博客自动同步文章,为了更好的阅读体验,建议您移步至我的博客

    本文标题:DesktopSwitcher 开发

    文章作者:gyro永不抽风

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

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

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

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

    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

  • 相关阅读:
    Binary Tree Zigzag Level Order Traversal
    Add Binary
    Subsets II
    Subsets
    Minimum Depth of Binary Tree
    node Cannot enqueue Quit after invoking quit.
    微擎快速修改数量实例(异步)
    destoon 分页
    ajax里面使用this方法
    微擎系统 微信支付 get_brand_wcpay_request:fail
  • 原文地址:https://www.cnblogs.com/jeffersonqin/p/13671227.html
Copyright © 2020-2023  润新知