Dashboard Widget Tutorial

This tutorial walks you through using Dashcode to create a Dashboard widget. As you follow the steps, you learn how to choose a widget template, customize your widget’s appearance and code, and share your widget with others. Completing this tutorial is a quick and easy way to get started building Dashboard widgets in Dashcode.

This document includes two additional tutorials, “Mobile Safari Web Application Tutorial” and “Dual-Product Web Application Tutorial,” which follow this one. The remainder of the document delves more deeply into the Dashcode development environment, describing how it supports both widget and web application development. If you don’t want to learn how to create a web application, you can continue learning more about Dashcode by reading “Starting a Project.”

Before You Begin !

In this tutorial, you build a Dashboard widget that counts down to your birthday, similar to the widget shown in Figure 1-1.

Figure 1-1  The Birthday widget



Before continuing, make sure that you have Dashcode installed on your Mac (the location is /Developer/Applications/). If you don’t have Dashcode installed, read “Getting and Running Dashcode” to learn how to get and install Dashcode.


Choose a Template


To start, double-click the Dashcode icon to open it. A new project window opens and displays a dialog in which you first select the type of project you’re interested in—in this case, Dashboard—and then, the kind of widget you want to create from an assortment of templates. Templates are handy starting points for creating common types of widgets. Select a template’s icon to show a short description of what that template does.

To make the Birthday widget, this tutorial uses the Countdown template. Select its icon and click Choose. A project window opens with a new widget based on the Countdown template, as shown in Figure 1-2.

Figure 1-2  A project window showing a new Dashboard widget


Along the left side of the project window is the navigator, which you use to switch between the various tools available when you’re designing a widget. The main portion of the window is the canvas, which you use to design your widget’s interface.

At the bottom of the navigator in Figure 1-2 you can see the Workflow Steps list, which guides you through the widget development process. Each step is a milestone in creating a widget, telling you what to do and where to do it. When you complete a step, mark it as done and move on to the next step.

Note: If you don’t want to see the Workflow Steps list, you can hide it by choosing View > Steps or by clicking the button that looks like a checkbox at the bottom edge of the project window (this button is highlighted in Figure 1-2).
Alternatively, you can view a list of the widget’s files in place of the Workflow Steps list. If you want to see the Files list, choose View > Files or click the list button in the bottom edge of the project window (it’s the button that looks like a bulleted list).
Set the Target Date

The Countdown template gives you a Dashboard widget with all the elements and code needed to count down to an event. All you need to do is tell the widget the target date. To set the target date, select Widget Attributes in the navigator. The canvas is replaced by the widget attributes pane, in which you specify important values that your widget needs.

In the Properties section of the widget attributes pane, choose Date and Time in the Target Kind pop-up menu and enter the date of your next birthday, as shown in Figure 1-3.

Figure 1-3  The Countdown template’s properties





Test the Countdown

Your new Dashboard widget is already fully functional. To prove this, choose Debug > Run to run the widget. Dashcode can run a widget without opening it in Dashboard, making it a handy place to test your widget and fix any problems you encounter. After the widget loads, it starts counting down towards your next birthday.

When you’re satisfied that your widget is working as you expect, choose Debug > Stop to stop it.

Now is a good time to save your widget project. Choose File > Save to save the project. Give your project a name and select a location to save it in. Your widget is saved in a widget project that encapsulates the widget and information Dashcode needs to create the widget for you.


Customize the Widget’s Appearance

Although you now have a fully functioning Dashboard widget that’s ready to share, you might want to personalize it to make it unique. Dashcode’s design tools make it easy to customize your widget’s interface. Select the widget item in the navigator (it should display the name you gave it when you saved the project). The widget attributes pane is replaced with the canvas.

First, change your widget’s body color. Select the widget body (also called the front image or frontImg) on the canvas and then choose Window > Show Inspector. The inspector window allows you to modify a selected element’s properties, such as its appearance and behavior. Click the Fill & Stroke button at the top of the inspector window (it’s the second from the left). If it’s not already selected, click the Style tab to reveal fill, stroke, corner roundness, and opacity values. Click the color well and choose a new color in the Colors window that appears. Try different fill styles until you find a combination that you like. If you want to try changing other effects, such as the glass appearance, click the Effects tab to reveal these values, as shown in Figure 1-4.

Figure 1-4  Tweaking the front image using the Fill & Stroke inspector



When you’re finished customizing your widget’s body, add a photo of yourself from iPhoto to the widget. Your iPhoto library is available from the Library window. To show your iPhoto library, choose Window > Show Library; then click the Photos button. Find a photo and drag it to your widget on the canvas. Resize it by dragging any of the resize handles on the photo.

Finally, change the Event Label text to something like “...days until my birthday.“ You can do this by selecting the Event Label text, clicking the Attributes button in the inspector window (it’s the leftmost button), and entering the text in the Value field, or by double-clicking the text in the widget body itself and entering the new text.



Add Functionality Using Parts

Now that you have a personalized Dashboard widget that counts down to your birthday, add a button that, when clicked, shows the Apple Store (so your friends and family can buy you a birthday present!). To add a button to your widget, use a button part. Parts are controls and views used on a widget’s interface.

To find a button part, choose Window > Show Library and click Parts. You can use the search field at the bottom of the window to help you find a particular part or type of part. From the list of parts, drag the Lozenge Button part from the Library window to your widget’s body. Double-click the button to select its label text, enter the text “Buy me a gift” and press Return. You'll probably need to resize the button to fit the new label.



Write Code to Show the Apple Store

To make the button take the user to the Apple Store when it’s clicked, you need to add a behavior to the button. In the inspector window, click the Behaviors button (it’s the rightmost button). This shows the Behaviors pane, in which you assign handler functions to events on an object. Select the button on the canvas and double-click in the Handlers column next to the onclick event name. Enter the name of a new function, such as showAppleStore, and press Return. Click the arrow next to the function name you entered to reveal the source code editor below the canvas. Here you write code to add functionality to your widget. Clicking the arrow reveals the showAppleStore function Dashcode inserted in your widget’s code. Between the braces ({ .. }) enter the following line of code:

widget.openURL("http://store.apple.com/");


The code in the source code editor should look like that in Figure 1-5.



Figure 1-5  A function in the source code editor



Test your widget again by choosing Debug > Run. Click the button you added and make sure a new Safari window opens with the Apple Store website displayed. Be sure you save your project often to preserve the changes you make.




Deploy Your Widget

Congratulations! You’ve created your first complete Dashboard widget using Dashcode.

To open your widget in Dashboard, choose File > Deploy Widget. Click Install in the dialog that appears to view your widget in Dashboard.

To share your widget with the world, select Run & Share in the navigator. The pane that appears displays the widget project name you chose in “Test the Countdown,” but you can replace this with a different name if you want. You can also set the minimum version of Mac OS X your widget should run in. Click Save to Disk to save your widget. You can now email it to friends or post it on your webpage. You can use the File > Compress command in the Finder to archive it.
Tip of the Day News Mac Tips and Tricks Games ★★★★★ Tips+Tricks MacTips WebApps Tips + Tricks Widgets Downloads iPhoto Safari Tips Apple Free Apps Personal GarageBand iMovie Internet Utilities Multimedia Apple Downloads Pro Tip iWeb Dashboard iTunes Mac Dashcode iDVD DashboardClocks Support 10.8 Applications iLife News OSX Tutorials 10.7 Desktop Mac Tip of the Day AppleTV Dashboard Widgets Finder Firefox MacWidgets OS X Mountain Lion Tips and Tricks TopApps Videos 10.6 Keyboard Shortcuts iPad iPhone Mac 10.9 AirPlay Apps on Mac Dock Just Added Mac App Store MacApps MacNN OS X OS X Lion Softpedia Tricks Updates iCal iLife iLife Widgets iPhone Tips and Tricks FAQ Mail AppShopper.com AppleNews Application Tips Customizing Customizing Your Mac Dashboard Guidelines Developer Widgets Exposé Games Widgets Mac App Store - News Mac OS X 10.6 Mac OS X 10.8 Mountain Lion Mac101 MacNews MacUpdate Mac|Life OS X Daily Preview Snow Leopard Tips Terminal TextEdit Top10 Mac Tips VersionTracker Video Tutorials Windows iCloud iLife 11 iWork  Navigation Key Combos Mac 101 MacSupport OS X 10.8 Mountain Lion OS X Mavericks Search Widgets Security iDVD Widgets iPod 10 Keyboard Shortcuts for Text 9 to 5 Mac A A-Z Glossery - iPhone App ATMac Address Book App App Store - News AppShopper Apple Gazette Apple Keynotes Apple Matters Apple Pro Apple Support Tips Apple Updates AppleInsider Applelinks Apps A-Z Apps Apps Apps Apps on Mac - Dashboard Widget Asteroid Audio Automator B BindApple Blogs + Forums Widgets Business Widgets C Calculate + Convert Changing the background Check external devices Check spelling and grammar Clear Versions History + Auto-Save Cache Data Cult of Mac Cydia D Daily Tips and Tricks Dashboard - Tips+Tricks Dashboard Tips and Tricks Dashboard Widget DashboardCandy DashboardSearch DashboardWidgets.com Desktop Computers Developer Forum Developer News Developer Tips Development Tools Disable restored windows when re-opening specific apps Discussions Display a login banner Display a short message Display system stats Dual Boot OS X 10.7 Lion + OS X 10.8 Mountain Lion E Easy Mac Tips Email + Messaging Widgets Essential Expose Extract and Save Mac Application Icons F Find a MAC Address in Mac OS X Find iMessage Users + Contacts Folders Food Widgets Freeware G Get iTunes track notifications in your Dock Get quick information with widgets Google News H Hackint0sh Hacks Hongkiat How To How to disable the Java web plug-in I IT Industry Today Icons Icons + Screensavers Image Capture Information Widgets InsanelyMac Installation International Widgets J Just Added - iPhone Apps Just Added Downloads Just For Fun Widgets K Keynote Address L Launchpad for Mac OS X Snow Leopard Links Lion M Mac App Store - Business Mac App Store - Developer Tools Mac App Store - Education Mac App Store - Entertainment Mac App Store - Finance Mac App Store - Games Mac App Store - Graphics + Design Mac App Store - Health + Fitness Mac App Store - Lifestyle Mac App Store - Medical Mac App Store - Music Mac App Store - Photography Mac App Store - Productivity Mac App Store - Reference Mac App Store - Social Networking Mac App Store - Sports Mac App Store - Top 50 Mac Apps Mac App Store - Travel Mac App Store - Utilities Mac App Store - Video Mac App Store - Weather Mac Developer Tips Mac OS X Mac OS X 10.7 Lion Mac OS X Applications Mac OS X Things Mac OS X Tips Mac OS X Tips - News Mac OSX Hints Mac OSX Hints - News Mac Quick Tips Mac Tips Mac Tips Daily Mac Tips and Tricks - Desktop App Mac Tips and Tricks - Mac OS X Dashboard Widget Mac360 MacApp MacApper MacFixIt MacLion MacMusic MacOSXAudio.com MacRumors MacTech MacUpdate.com MacintoshOS.com Maciverse Macworld.com - iPhone App Reviews ManiacDev MobileMe News ModMyi.com Most Recent Movies + TV Widgets Music Widgets N Navigating + Selecting Text in Mac OS X Networking + Security Widgets New Application Tips New iPhone Apps News Widgets News on Mac Notification Center O OS X - FAQ OS X 10.7 OS X Basics OS X Mac Tips + Tricks OS X Mountain Lion - News OS X Snow Leopard OSX Basics Open source OpenDashboard P Pages Podcast Widgets Portable Computers Ports and Connectors Preferences Press Release Q R Radio + Podcasts Widgets Rampant Mac - iPhone Wallpapers Rename Files and Folders Restart an external device S SD and SDXC card slot FAQ Safari 5.1 Safari 6 Safari Videos Scheduled Startup and Shutdown Set Up Storage Devices Set up iCloud Shopping Widgets Simple Desks Snow Leopard Softonic - Mac Softonic.com Softpedia - Tips + Tricks Softpedia.com Stacks Staff picks Status Widgets Storage StorePreview Switch 101 Syncing with iTunes System Preferences T TUAW The Apple Blog The Apple Core The Mac Observer The Mac Screencast Guy The MacTips Podcast TheDashboard Time Machine Tools Top 10 Grossing Apps Top 25 Grossing Apps Top 50 Grossing Apps Top iTunes Tips Top10 Paid Apps Top10 Widgets Top50 Dashboard Widgets Translates Transportation Travel Widgets U Updating Useful iPhone Shortcuts Using iChat V W Wallpaper Want More Mac Tips ? Web Apps Webcam Widgets Widget Work X YouTube Videos Z ZDNet apple.stackexchange.com digg gSearch - iPhone App iChat iClarified iCloud + MobileMe iCloud News iCloud system requirements iDesign - iPhone Wallpaper iHackintosh iLife - iPhoto - iMovie - iWeb - GarageBand - iDVD iLife Discussions iLife Discussions - GarageBand iLife Discussions - iDVD iLife Discussions - iMovie iLife Discussions - iPhoto iLife Discussions - iTunes iLife Discussions - iWeb iLife Support iLife System Requirements iLife ’09 iMovie Widgets iOS 4 iOS Developer News iPad - News iPhone - News iPhone 4 iPhone App iPhone SDK iPhoto Widgets iPod - News iPod News iPod Tips and Tricks - iPhone App iPod touch iTunes - Latest Movie Trailers iTunes - News iTunes - Top News iTunes App Store iTunes App Store - All New Applications iTunes Keyboard Shortcuts iTunes News iTunes Plus FAQ macosxtips.co.uk switchtoamac.com thinkmac.net www.freemacware.com ...organise your mac 10 Quick Mac Tips 10.10 101 ASC Animation Slow-Mo Aperture Aperture Glossary Apple Support Communities Boot Camp Bored of your Mac? Collect RSS feed URLS from Mail Create Ringtones in iTunes Customise your desktop + screen saver Does your Mac qualify for free 10.8 upgrade? Download Download YouTube and Other Videos FTP FTP with Finder Finder Tips Gatekeeper Get Mac News Get More Mac and iOS Tips Get More Mac and iOS Tips... Get OS X Tips Get RSS Menu Extension for Safari 6 Get Windows Live Hotmail with Mail Get Yahoo Mail with Mail Get a Homepage - Mac OS X Style Glossary Google Hidden Features Hidden Features in OS X 10.8 Mountain Lion Hotmail Install Windows 8 on a Mac Mac 101 - Get One on One with your Mac... Mac OS X Dashboard Widget Mac Support Mac Tips and Tricks - Learn how to set up MacBasics MacHelp MacVideos News and Help Notification Center Tips OCD Friendly Volume Controls OS X 10.5 to 10.8 Upgrade OS X Features OS X Mavericks Tips OS X Mountain Lion Hompage OS X Mountain Lion Installation Guides OS X Tips Organize Your Dock Quick Define Quick Googling Quick Math RSS Remote Folder and Synchronization Remove Dock Icons in OS X Mountain Lion Reset specific parts of Safari ScreenCast Online Screenshots Search Search Google Search Mac Tips and Tricks Second Clipboard Security Tips Siri Siri Tips Snippets Speed Up Your Mac Stop Automatically Updates Summarize Text System Configuration Take advantage of Quick Look Taking Screenshots Text Tip Tips and Support Top Mac Tips Top Security Tips USB 3 devices on Mac - FAQ Use Smart Folders effectively Which Mac is Worth Your Investment? Widgets On Your Desktop Windows 8 Xcode Xcode Tips Yahoo YouTube iOS Support iPad Support iPhone Support iPod Support onemac.net onemac.org use and troubleshoot your Mac