So I am a newbie to Swift and wanted to create a simple example status bar app on MacOS.
To keep things clean I created a subclass App which is creating the status item. This class is then created in the applicationDidFinishLaunching function of the AppDelegate.swift.
But somehow nothing is printed on the console when I press the status icon. However if I copy the code in the AppDelegate file it works. Does someone know what I am doing wrong and why it is not working in the subclass?
Here is the code of my own class:
import Cocoa
class App: NSObject {
let menuBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.squareLength)
override init() {
print("created app instance");
if let button = menuBarItem.button {
button.image = NSImage(named: NSImage.Name("StatusBarButtonImage"))
button.action = #selector(test(_:))
}
}
#objc func test(_ sender: Any?) {
print("button was pressed")
}
}
and the AppDelegate:
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var appInstance: App!
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
appInstance = App()
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
If the button is showing up and nothing is happening when you click it, it looks to me like you need to make sure you're setting your button's target to your App instance. E.g.:
button.target = self
Otherwise the action is only followed up the responder chain.
Related
I am building an macOS app for the menu bar and it should automatically start with system start.
I started with implementing the autostart functionality for a standard window based macOS app following this tutorial this tutorial. I have
added a new target inside the main project (the helper app)
changed skip install to yes for the helper app
set the helper app to be a background only app
added a new copy file build phase to the main application to copy the helper application into the bundle
linked the ServiceManagement.framework
Implemented the functionality in the app delegates, that the helper app gets launched with system start. After it has launched, it launches the main app (see the tutorial link for more info or the source code down below)
That worked fine, the app launched automatically :) So I started changing the project, that the main application becomes a menu bar app. However than, the app wouldn't auto launch anymore :/ Does someone have a solution for that?
Heres the code of the app delegate of the main app:
import Cocoa
import ServiceManagement
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
let statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
func applicationDidFinishLaunching(_ aNotification: Notification) {
statusItem.button?.title = "Test"
statusItem.button?.target = self
statusItem.button?.action = #selector(showWindow)
// auto start
let launcherAppId = "com.####.####Helper"
let runningApps = NSWorkspace.shared.runningApplications
let isRunning = !runningApps.filter { $0.bundleIdentifier == launcherAppId }.isEmpty
SMLoginItemSetEnabled(launcherAppId as CFString, true)
if isRunning {
DistributedNotificationCenter.default().post(name: .killLauncher, object: Bundle.main.bundleIdentifier!)
}
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
#objc func showWindow() {
let storyboard = NSStoryboard(name: "Main", bundle: nil)
guard let vc = storyboard.instantiateController(withIdentifier: "ViewController") as? ViewController else {
fatalError("Unable to find main view controller")
}
guard let button = statusItem.button else {
fatalError("Unable to find status item button")
}
let popover = NSPopover()
popover.contentViewController = vc
popover.behavior = .transient
popover.show(relativeTo: button.bounds, of: button, preferredEdge: .maxY)
}
}
extension Notification.Name {
static let killLauncher = Notification.Name("killLauncher")
}
And this is the app delegate of the helper app:
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
let mainAppIdentifier = "com.####.####"
let runningApps = NSWorkspace.shared.runningApplications
let isRunning = !runningApps.filter { $0.bundleIdentifier == mainAppIdentifier }.isEmpty
if !isRunning {
DistributedNotificationCenter.default().addObserver(self, selector: #selector(self.terminate), name: .killLauncher, object: mainAppIdentifier)
let path = Bundle.main.bundlePath as NSString
var components = path.pathComponents
components.removeLast()
components.removeLast()
components.removeLast()
components.append("MacOS")
components.append("####") //main app name
let newPath = NSString.path(withComponents: components)
NSWorkspace.shared.launchApplication(newPath)
}
else {
self.terminate()
}
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
#objc func terminate() {
NSApp.terminate(nil)
}
}
extension Notification.Name {
static let killLauncher = Notification.Name("killLauncher")
}
Thank you very much for your help :)
My code looks pretty much the same, except how I compose the path in the helper app:
var pathComponents = (Bundle.main.bundlePath as NSString).pathComponents
pathComponents.removeLast()
pathComponents.removeLast()
pathComponents.removeLast()
pathComponents.removeLast()
let newPath = NSString.path(withComponents: pathComponents)
NSWorkspace.shared.launchApplication(newPath)
Also, if I remember correctly, I had to make sure the Main.storyboard file still had the "Application Scene" with an Application object and an empty main menu.
So here is the full code:
main.swift
import Cocoa
let delegate = AppDelegate()
NSApplication.shared.delegate = delegate
NSApplicationMain(CommandLine.argc, CommandLine.unsafeArgv)
AppDelegate.swift
import Cocoa
class AppDelegate: NSObject, NSApplicationDelegate {
override init() {
Swift.print("AppDelegate.init")
super.init()
Swift.print("AppDelegate.init2")
}
func applicationDidFinishLaunching(aNotification: NSNotification) {
Swift.print("AppDelegate.applicationDidFinishLaunching")
}
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool {
Swift.print("AppDelegate.applicationShouldTerminateAfterLastWindowClosed")
}
}
Then I compile it with:
swiftc Sources/*
./main
It logs:
AppDelegate.init
AppDelegate.init2
But then it doesn't log anything else, so I close it with CTRL+C. Not sure why it's not reaching the applicationDidFinishLaunching method ever. Wondering if one knows of a fix for this, it seems like there is just one method that needs to be called somewhere, but I'm not sure.
I think this may be causing other issues such as NSMenu not working.
Hooray, it was just because of the format of the methods, which were silently failing I guess.
func applicationWillFinishLaunching(_ notification: Notification) {
Swift.print("AppDelegate.applicationWillFinishLaunching")
}
func applicationDidFinishLaunching(_ notification: Notification) {
Swift.print("AppDelegate.applicationDidFinishLaunching")
}
I'm working through a cocoa application tutorial from the Big Nerd Ranch's Cocoa Programming 5th edition book (I'm in the beginning chapters). On their blog website for discussing the book, a user mentions that passing in 'self' isn't necessary and that it's covered in chapter 18. I'm very curious now though as to how this could be refactored without having to pass in 'self'. Is it possible?
This code is basically creating an instance of a custom ViewController which will need to load from the AppDelegate.
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
var mainWindowController: MainWindowController?
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
let mainWindowController = MainWindowController()
//put the window of the window controller on the screen
mainWindowController.showWindow(self)
//set the property to point to the window controller
self.mainWindowController = mainWindowController
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
}
MainWindowController class if you need to see the functionality. It's very basic, its not doing much:
import Cocoa
class MainWindowController: NSWindowController {
#IBOutlet weak var textField: NSTextField!
override var windowNibName: NSNib.Name {
return NSNib.Name.init("MainWindowController")
}
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.
}
#IBAction func generatePassword(_ sender: AnyObject) {
//Get a random string of length 8
let length = 8
let password = generateRandomString(length: length)
//tell the text field to display the string
textField.stringValue = password
}
}
Trying to setup validation for a few text fields in a new (and very small) Swift Mac app. Following various other topics here on SO and a few other examples, I can still not get controlTextDidChange to propagate (to my ViewController).
E.g: How to live check a NSTextField - Swift OS X
I have read at least a dozen variations of basically that same concept. Since none of the accepted answers seem to work I am just getting more and more confused by something which is generally a fairly simple task on most platforms.
I have controlTextDidChange implemented to just call NSLog to let me know if I get anything.
AppDelegate should be part of the responder chain and should eventually handle controlTextDidChange but I see nothing there either.
Using the current Xcode I start a new project. Cocoa app, Swift, Storyboard and nothing else.
From what I can gather the below isolated example should work. In my actual app I have tried some ways of inserting the ViewController into the responder chain. Some answers I found suggested it was not always there. I also tried manually adding the ViewController as the delegate in code theTextField.delegate = self
Nothing I have done seems to get text changed to trigger any events.
Any ideas why I have so much trouble setting up this delegation?
My single textfield example app
Storyboard is about as simple as it gets:
AppDelegate
import Cocoa
#NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate, NSTextFieldDelegate, NSTextDelegate {
func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application
}
func applicationWillTerminate(_ aNotification: Notification) {
// Insert code here to tear down your application
}
func controlTextDidChange(notification: NSNotification) {
let object = notification.object as! NSTextField
NSLog("AppDelegate::controlTextDidChange")
NSLog("field contains: \(object.stringValue)")
}
}
ViewController
import Cocoa
class ViewController: NSViewController, NSTextFieldDelegate, NSTextDelegate {
#IBOutlet var theTextField: NSTextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func controlTextDidChange(notification: NSNotification) {
let object = notification.object as! NSTextField
NSLog("ViewController::controlTextDidChange")
NSLog("field contains: \(object.stringValue)")
}
}
I think the samples you're following are a bit out-of-date.
Try...
override func controlTextDidChange(_ notification: Notification) {
...as the function definition for your method in your NSTextFieldDelegate.
I want a function to fire every time the user makes a change to my NSTextView. I got this to work in an iOS app and am now trying to make it work in an OS X app. I created an outlet for my NSTextView and wrote the following Swift code:
import Cocoa
class ViewController: NSViewController, NSTextViewDelegate {
#IBOutlet var textViewOutlet: NSTextView!
func textViewDidChange(textView: NSTextView) {
print("Text view changed!")
}
}
I don't get any errors but my statement doesn't print.
It should be textDidChange(notification:). Try like this:
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
print(textView.string)
}
This should be why there is no delegate assignment.
TextViewOutlet.delegate = self;
And the method actually looks like this. Notice the parameters
func textDidChange(_ notification: Notification) {
}