
Beginning Mac OS X Tiger Dashboard Widget Development (2006)
.pdf
A
Answers to Exercises
Chapter 1
1.Cheetah and Puma, respectively. Give yourself bonus points if you loaded the Wikipedia widget from Apple’s Dashboard website and opened Dashboard to retrieve this information.
2.The widget installer moves the copy you double-click into your widget folder. This means you will have to move it out to make changes. You may want to copy your widget from your development area to the desktop and install that copy.
3.Old Unix hands write paths that start at the root level of your personal account with a tilde, as in ~/Library/Widgets/. This is much shorter than typing the full path in terminal: /Users/<yourlogin>/Library/Widgets/. Why is this important? Since widgets can call shell scripts, you need to remember this distinction in your HTML and JavaScript files. More on paths in a latter chapter.
Chapter 2
1.As your high school English teacher might have told you, there is no wrong answer to this question. Or more accurately, there are any number of right answers. GraphicConverter, for example, has a Caliper Rule tool that lets you take measurements within your graphics.
2.The FlightTracker widget has an AllAirports.js file. This should have been a pretty easy guess that you could then confirm with Show Package Contents. If you tried to use Spotlight to find the file, you may have noticed that you can’t search inside of bundles.
3.You can see this kind of folder organization if you look inside of the Weather widget. The folder hierarchy is:
/Weather/Images/
/Weather/Images/Icons/
/Weather/Images/Icons/moonphases/
/Weather/Images/Minis/

Appendix A
The images inside of the directories are referenced with a relative path, that is, the path is given relative to the current directory.
Images/Icons/
Images/Icons/moonphases/
Images/Minis/
In the case of a widget, the paths are given relative to the mainHTML file, Weather.html. If you open the Weather.html, Weather.js, and Weather.css files, you see absolute path references starting at the root level of the Weather folder similar to the listing above.
The opposite of a relative path is an absolute path, which starts at the root level of your hard drive. The path to the Weather.html file in an absolute form is /Macintosh HD/Library/Widgets/Weather.wdgt/ Weather.html.
Chapter 3
1.You enter a property in the Info.plist file that points to it.
2.No, because the widget object isn’t supported in Safari.
3.Use BBEdit’s Open Hidden File feature or copy the widget out to a folder where you have write permissions and do the editing there.
Chapter 4
1.UTF-8 is a variable-length character-encoding scheme for Unicode. The name stands for 8-bit Unicode Transformation Format.
2.No. If you do use an ID selector with the value “radar” more than once in your widget, the getElementByID() will not work correctly.
3.You can use the <div> tag to associate the style with the back side and preferences of your widget.
Chapter 5
1.Dashboard intercepts any alert messages and sends them to console.log.
2.PID stands for Process ID. Every program on an OS X machine has an entry in the system’s process list and is assigned a unique process ID. A smaller ID number indicates that the process started running close to boot time. You can see the running processes in Activity Monitor or, using Terminal, in top.
3.Step through code, one line at a time, and examine the variable’s values.
Chapter 6
1.Use the black info button graphics in the AppleClasses folder. Whenever possible, use the graphics in WidgetResources. This ensures compatibility when your widgets are installed on other users’ Macs.
292

Answers to Exercises
2.You may not have clearly delineated what is on the front side of the widget from what is on the back side. Always keep the contents of the front or back within their respective <div>s.
3.Your widget’s preferences file is an XML file that you can open with any text editor or with Apple’s Property List Editor to examine the contents.
Chapter 7
1.Leave one preference item in the file.
2.Dashboard isn’t really a process. If you get information on any of the widgets, you’ll see that they are owned by the Dock (Figure A-1).
Figure A-1
3.Clicking in the widget’s close box or removing it in the Widget Manager.
Chapter 8
1.Live resizing is also called relative resizing.
2.Relative resizing is typically used for widgets that display RSS feeds.
3.Apple’s scrollbar classes are stored in the WidgetResources directory.
Chapter 9
1.You can use the getData method with the ondrop and onpaste events only.
2.You pass the information in the event variable.
3.It doesn’t take any parameters.
293

Appendix A
Chapter 10
1.You call the cancelDefault() method.
2.The event.stopPropagation() method doesn’t take any parameters.
3.Arguably, drag and drop was introduced with the Trashcan in Macintosh OS 1.0.
Chapter 11
1.You will want to use the widget.system() method in asynchronous mode. The Unix traceroute utility shows you the Internet path from your Macintosh to the computer or network resource that you are accessing. As it finds each router on its way to that Internet computer, it writes the IP address and information about each device to stdout. It is common for traceroute to take more than a minute or two to find the network path to that computer and it may time out. For this reason, you should allow the widget to continue to run while traceroute is running, and you should specify that the handler get each line and display it in your widget.
2.You would use the AllowInternetPlugins access key.
3.You may, but it is overkill. The more appropriate access key is the AllowFileAccessOutsideOfWidget. This key provides your widget with the maximum amount of access that it needs.
Chapter 12
1.Widget plugins are part of the widget bundle. WebKit plugins reside in the /Library/Internet Plug-ins/ directory.
2.No, they can be left in the /Library/Internet Plug-ins/ directory. They are called and automatically loaded whenever Dashboard is activated.
3.They use too much memory.
294

B
Additional Dashboard
Programming Information
This appendix contains pointers to additional sources of Dashboard programming information. For the latest information about changes to Dashboard or WebKit, always check Apple’s Developers’ site. You will find most of the information that you need on Apple’s website, but you can find other sources in blogs and forums with a quick search on Google.
Guides, Tutorials, and Specifications
Apple maintains a site for OS X developers at http://developer.apple.com where you can find the Dashboard, Safari, and WebKit programming documents listed below. Most of these documents can be downloaded as PDF files. In addition to Dashboard, Safari, and WebKit-related documents and technical notes, you’ll also find information about Human Interface Guidelines and creating universal binaries for the new Intel Macs as well as other programming guides.
Apple Human Interface Guidelines (HIG)
To learn more about the human interface, read Apple’s Human Interface Guidelines.
http://developer.apple.com/documentation/UserExperience/Conceptual/OSXHIGuide
lines/index.html?http://developer.apple.com/documentation/UserExperience/
Conceptual/OSXHIGuidelines/XHIGUsingTechnologies/chapter_8_section_6.html
The HIG describes how to create an application with a consistent user interface. Be sure to read the “High Level Design Guidelines for Widgets” section.

Appendix B
Dashboard Reference
The Dashboard reference contains information not included in the tutorial.
http://developer.apple.com/documentation/AppleApplications/Reference/Dashboard_Ref/
index.html?http://developer.apple.com/documentation/AppleApplications/Reference/
Dashboard_Ref/DashboardRef/chapter_1_section_1.html
This reference describes the JavaScript objects available to widgets as well as the interfaces for configuring and extending a widget.
Dashboard Tutorial
The Dashboard tutorial is the main source of information for widgets.
http://developer.apple.com/documentation/AppleApplications/Conceptual/Dashboard_
Tutorial/index.html?http://developer.apple.com/documentation/AppleApplications/
Conceptual/Dashboard_Tutorial/Introduction/chapter_1_section_1.html
The tutorial walks you through the basics of creating widgets to more advanced topics. It describes all of the components, events, and access keys for creating widgets.
Debugging Dashboard Widgets
If your widget has bugs, you may want to refer to this guide.
http://developer.apple.com/technotes/tn2005/tn2139.html
This technical note (TN2139) discusses debugging strategies, tips, and tricks for Dashboard widgets.
Developing Dashboard Widgets
For a brief overview of developing widgets, refer to this page at Apple’s website.
http://developer.apple.com/macosx/dashboard.html
This information is scattered throughout the tutorial and other technical notes, but this is a good starting point.
Dynamic HTML and XML: The XMLHttpRequest Object
If you are creating a widget that works with RSS feeds, this document is your best source of information.
http://developer.apple.com/internet/webcontent/xmlhttpreq.html
This note describes how to use the XMLHttpRequest Object to connect your widget directly to an XML source to update your widget’s information in the background without reloading the page.
296

Additional Dashboard Programming Information
Safari JavaScript Reference
For information about using JavaScript in Safari, refer to this reference.
http://developer.apple.com/documentation/AppleApplications/Reference/SafariJSRef/
index.html#//apple_ref/doc/uid/TP40001482
This reference describes Apple’s JavaScript extensions to Safari and the WebKit.
Safari Document Object Model Overview
This guide provides a brief introduction to the Document Object Model.
http://developer.apple.com/documentation/Cocoa/Conceptual/WebKit_DOM/index
.html?http://developer.apple.com/documentation/Cocoa/Conceptual/WebKit_DOM/
01introduction.html
This is a 30,000-foot introduction to Apple’s implementation of the WebKit Document Object Model. It primarily provides links to Apple and W3C DOM specifications and documents.
Safari JavaScript Programming Topics
If your widget makes use of the Document Object Model, you may want to refer to this guide.
http://developer.apple.com/documentation/AppleApplications/Conceptual/SafariJS
ProgTopics/index.html?http://developer.apple.com/documentation/AppleApplications/
Conceptual/SafariJSProgTopics/Tasks/DOM.html
This collection of programming topics describes how to use the DOM from JavaScript. In particular, it talks about drawing to the canvas, using the Pasteboard from JavaScript, and using Objective-C from JavaScript.
Safari HTML Reference
Because Dashboard and Safari share the WebKit HTML additions, you may want to refer to this guide.
http://developer.apple.com/documentation/AppleApplications/Reference/SafariHTMLRef/
index.html?http://developer.apple.com/documentation/AppleApplications/Reference/
SafariHTMLRef/Introduction.html
This reference describes every HTML tag and property supported by Safari and WebKit.
Universal Binaries Programming Guidelines
The Universal Binaries Programming Guidelines document the coding changes that need to be made to applications so they can run on the new Intel-based Macintosh computers.
297

Appendix B
http://developer.apple.com/documentation/MacOSX/Conceptual/universal_binary/index
.html?http://developer.apple.com/documentation/MacOSX/Conceptual/universal_binary/
universal_binary_intro/chapter_1_section_1.html
Because widgets are mainly composed of HTML, CSS, and JavaScript, they will be able to run on the new Intel-based Macs. If your widget contains a plugin, however, you need to compile your plugin as a universal binary. This guide describes how to build or modify Mac OS X applications so they will run on either PowerPC or Intel-based Macs.
Introduction to WebKit Plugin Programming Topics
For information about WebKit plugins, refer to this document.
http://developer.apple.com/documentation/InternetWeb/Conceptual/WebKit_PluginProg
Topic/index.html?http://developer.apple.com/documentation/InternetWeb/Conceptual/
WebKit_PluginProgTopic/WebKitPluginTopics.html
This collection of programming topics primarily describes the WebKit plugin architecture and explains how compiled plugins work inside of WebKit applications like Safari.
Mailing Lists
Mailing lists are the best source of current information about programming topics and assistance when you are having problems.
http://lists.apple.com/mailman/listinfo/dashboard-dev
Currently only one mailing list is devoted to Dashboard widgets. Apple hosts the Dashboard Developers mailing list and you can sign up at this address. The list routinely discusses Dashboard programming problems and bugs and is frequented by one of the Apple Dashboard engineers.
Other Resources
Because widgets are mostly based on open standards technologies, you will be able to find many non- Mac-specific sources of information for HTML, XHTML, CSS, and JavaScript.
MacTech Magazine
MacTech magazine may be the last Macintosh programming magazine around. It has been publishing articles for over 20 years and has always been on the cutting edge of Macintosh programming trends. The articles are aimed at the beginning to intermediate programmer and cover everything from AppleScript, Carbon, and Cocoa development to web scripting languages and Macintosh technologies. Go to their website (www.mactech.com/) for more information.
298

Additional Dashboard Programming Information
Websites
Any listing of websites is sure to be made obsolete by paper publication timelines, so it is better just to point to the main Dashboard websites that are the most active.
Apple Dashboard Downloads
This website (www.apple.com/downloads/dashboard/) is the main source for the latest widgets. If you’re wondering whether someone has created the widget that you are thinking about writing, this is the first place to stop. As these pages are being written, there are close to 2,000 widgets.
Dashboard Lineup
Dashboard Lineup (www.dashboardlineup.com/) is a combination blog, widget repository, and community forums.
Dashboard Widgets
Dashboard Widgets (www.dashboardwidgets.com/) is a regularly updated blog that also has forums.
MacDevCenter
MacDevCenter (www.macdevcenter.com/) is part of the O’Reilly network. It contains a collection of regular articles and blogs that occasionally include Dashboard articles or programming topics.
Mac OS X Hints
This website (www.macosxhints.com) is the best source of hints for OS X with daily postings. It has a number of hints about Dashboard and widgets.
MacTech
MacTech is the online presence of the paper magazine. The website (www.mactech.com/) contains loads of information for the Macintosh programmer including news, articles, and sample code. Besides providing programming information and code, MacTech will keep you informed about programming directions.
299
