Unable to Re-Activate App From Dock

When I use the red ‘X’ button to close the PasswordGenerator application, the app remains in the dock, as it should. However, I cannot click the PasswordGenerator app in the dock and have it restore to the screen like other apps do. This happens both when running the app from Xcode 7.2.1 and when I launch the app from Launchpad.

Shouldn’t clicking the PasswordGenerator app in the dock restore the app to the screen? If not, how do I restore the app to the screen?

Thank you…
Earl

Have a look at the reference material for NSApplicationDelegate protocol.

Ibex10,

Thank you for your reply. I read several articles on NSApplicationDelegate, but understood almost nothing about it. Getting used to Apple programming has been a real challenge.

One snippet of code I copy and pasted from the internet that was supposed to work is this:

  • (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
    {
    [window makeKeyAndOrderFront:self];
    return NO;
    }

However I got an error message “Expected declaration” which I don’t why I am getting it nor how to fix it. Here is all the code in the AppDelegate.swift file:

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var mainWindowController: MainWindowController?


func applicationDidFinishLaunching(aNotification: NSNotification) {
	
	//create a window controller with an xib file with the same name
	let mainWindowController = MainWindowController(windowNibName: "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: NSNotification) {
	// Insert code here to tear down your application
}	

//“Expected declaration” error occurs on the following line of code
- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender
{
[window makeKeyAndOrderFront:self];
return NO;
}
}

Any help you can give me is greatly appreciated.

Earl

It takes time to get used to Apple’s documentation for reference material, mainly because it is mostly poorly written. However, you can reduce the pain by reading tutorials and programming guides.

Now for the problem you are experiencing.

[quote]- (BOOL) applicationShouldOpenUntitledFile:(NSApplication *)sender { [window makeKeyAndOrderFront:self]; return NO; } [/quote]
That looks like the correct ApplicationDelegate method, but it is written in Objective-C; you need to write it in Swift.

Here is a line-by-line translation to Swift:

func applicationShouldOpenUntitledFile (sender: NSApplication) -> Bool
{
   window.makeKeyAndOrderFront (self);
   return false;
}

ibex10,

Thank you for your reply. Learning Apple programming has been a real struggle for me. I have no clue when I am using Objective-C programming vs Swift programming vs Cocoa features. Plus I am told that Swift is a work-in-progress, so I am never sure if the error messages I receive are a result of an actual error in the code or the error is a result of the evolving Swift language. I am using these four books as guides:

Cocoa Programming for OS X: The Big Nerd Ranch Guide
Objective-C Programming: The Big Nerd Ranch Guide
Learn to Code in Swift
Mastering Xcode 7 & Swift, iOS App Development for Non-Programmers

I tried your code:

func applicationShouldOpenUntitledFile (sender: NSApplication) -> Bool { window.makeKeyAndOrderFront (self); return false; }

but it doesn’t like “window”. It reports an error “Use of unresolved identifier ‘window’”. I looked in all the files of my program and I don’t see any identifier for the window. The Interface Builder has something they call Window (with a capital ‘W’) that I thought might be the name of the window, but that produced the same “Use of unresolved identifier ‘window’” error. So am I supposed to make a ‘window’ variable or is the name of my window hidden somewhere in the code? If I need to make a new ‘window’ variable, what is the type?

Thank you for all of your help.

Earl Staley

Out of curiosity, why doesn’t Apple include the ability to restore an application from the Dock part of the standard window behavior just like they do for the ‘Quit’, ‘Minimize’ and ‘Maximize’ buttons on the form?

When I write code for Microsoft Windows, I don’t need to write code to restore the window. The code is already included in the standard window framework.

Earl Staley

There are practical steps you can take to ease the pain:

  1. Learn the languages first:
  1. When creating a new project, choose a language - Objective-C or Swift;

3.Use Cocoa as a framework (component library); and

  1. If you feel (rightly) that Swift is a work-in-progress, then stick with Objective-C for now. Don’t just dive into Swift because everybody else is doing it. Don’t volunteer to be a crash-test dummy (you don’t get paid for it.) Once you have become comfortable with Objective-C, you can start the transition to Swift.

[quote]I tried your code:

func applicationShouldOpenUntitledFile (sender: NSApplication) -> Bool
{
   window.makeKeyAndOrderFront (self);
   return false;
}

but it doesn’t like “window”…[/quote]
You need to add an outlet and connect it to the Window object in IB.

Here is an example:

//  AppDelegate.swift
//

import Cocoa

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

    @IBOutlet weak var window: NSWindow!

    func applicationDidFinishLaunching(aNotification: NSNotification) {
        // Insert code here to initialize your application
    }

    func applicationWillTerminate(aNotification: NSNotification) {
        // Insert code here to tear down your application
    }

    func applicationShouldOpenUntitledFile (sender: NSApplication) -> Bool
    {
        print (self)
        window.makeKeyAndOrderFront (self);
        return true;
    }
}

You can reach me from (pretty-function.org) to discuss how I can help you to come up to speed.

Thank you again for all of your help. I added your code:

@IBOutlet weak var window: NSWindow!
and successfully connected it to the Window object in IB.

When I first ran the program, I got a successful build, but I got an immediate error at:

I stopped and restarted Xcode and the program runs fine if I use the yellow ‘-’ button to minimize the window. However, if I use the red ‘X’ button to close the window, I get the following error when I try to restore the window:

“fatal error: unexpectedly found nil while unwrapping an Optional value”

Virtually every other program on my Mac restores successfully after using the red ‘X’ to close the window. Why is this program different and can I make the program restore successfully after using the red ‘X’ to close the window?

Earl Staley

[quote]“fatal error: unexpectedly found nil while unwrapping an Optional value”
[/quote]
That error is saying that the window outlet is floating; that is, it is not connected to the window object in the xib file.