Create a Menu driven app with Swift and read Xcode preferences – Part I

In this article we are going to make a swift app for the Mac OSX. This will have no window but instead have a menu item that would be used for interacting with the application. This application shall also read a settings and alter the icon and menu according to the status of that setting

Topics of Learning

  • Make a Mac OS X app with Swift
  • App without a ViewController or Window
  • App in the menu bar
  • Change the menu icon
  • Read application settings
  • Update menu accordingly

Inspiration

I must admit that the idea for this stemmed from a blog post I came across, where the author wrote about the various tools they have written for their own personal use. There was no corresponding source code or idea behind how to achieve the same, so it got me to quickly replicate that functionality and make it available for you.

Making a Mac OS X App with swift

This is the easiest task, start Xcode and select the Cocoa Application template and Click Next. Type in a Product name like “MenuApp”, set the language to Swift and make sure that the Use Storyboards is checked and the Create Document-Based Application and Use Core Data are both unchecked and Click Next and save the Project.

[2015-05-30055042] -Xcode

This project when run will display a window on the screen with a menu that has all of the File, Edit, View etc menu items.

Removing the ViewController and the Window

We want to achieve something that has no Window on the screen and we will do so by removing the ViewController.swift. Add a new file from the User Interface group of type Main Menu. This adds a MainMenu.xib file to your project and a full fledged menu. Click on the MenuApp to see the project settings and select the MenuApp target, under the Deployment Info select MainMenu as Interface. Now remove the Main.storyboard and the ViewController.swift files.

[2015-05-30072225] -Xcode

Here’s what we’ve build so far

When we run the code, we can see the icon displayed in the statusbar and the menu drops down when selected and the time updated every time the menu is selected.

CompletedApp_Part1

Here we come to the end of Part I. In Part II, we shall read a setting from Xcode preferences and change the Icon based on the setting, add a custom view for About. Till next time

Note: The Source code shall be available with the next part of this article.

Views All Time
Views All Time
4349
Views Today
Views Today
3
Posted in Tutorial, Uncategorized and tagged , , , , .