美文网首页
OSX开发之守护进程(appserver)

OSX开发之守护进程(appserver)

作者: xinle | 来源:发表于2017-07-22 01:20 被阅读180次

    1.状态栏程序制作

    • xcode创建项目

    • 删除Main.storyboard 中的Window和ViewController

      15006520234330.jpg
    • appdelegate 添加代码

    import Cocoa
    @NSApplicationMain
    class AppDelegate: NSObject, NSApplicationDelegate {
    
        let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
        var statusMenu: NSMenu!
    
        func applicationDidFinishLaunching(_ aNotification: Notification) {
            statusMenu = NSMenu(title: "")
            let item0 = NSMenuItem(title: "启动App", action: #selector(startApp), keyEquivalent: "")
            item0.isEnabled = true
            statusMenu.insertItem(item0, at: 0)
            
            let item1 = NSMenuItem(title: "设置", action: #selector(setting), keyEquivalent: "")
            item1.isEnabled = true
            statusMenu.insertItem(item1, at: 1)
            
            let item2 = NSMenuItem(title: "退出", action: #selector(exit), keyEquivalent: "")
            item2.isEnabled = true
            statusMenu.insertItem(item2, at: 2)
            
            let icon = NSImage(named: "icon1")
            statusItem.image = icon
            statusItem.menu = statusMenu
        }
        
        func setting() {}
        
        func exit() {
            NSApplication.shared().terminate(self)
        }
        
        func startApp()  {}
    }
    
    
    • info.plist添加 Application is agent (UIElement) 为 true (隐藏dock图标)

    2.主程序target添加

    • 添加target
    • Build Phases 添加copy Files选项,添加target app


      15006535734128.jpg
    • 启动app
    let path = Bundle.main.bundlePath.appending("/Contents/SharedSupport/MainApp.app")
    let task = Process()
    task.launchPath = "/usr/bin/open"
    task.arguments = [path]
    task.launch()
    

    3.进程间的通讯

    4.开机启启动

    #把文本编辑.app 添加到用户登录项
    #!/bin/bash 
    /usr/bin/osascript -e "tell application \"System Events\" to make new login item with properties { path: \"/Applications/TextEdit.app\", hidden:false } at end"
    

    github地址

    相关文章

      网友评论

          本文标题:OSX开发之守护进程(appserver)

          本文链接:https://www.haomeiwen.com/subject/kapykxtx.html