Commands and MenusThe API for commands allows you a great deal of control over how commands work in the menus. Although all commands appear in the Commands menu by default with names determined by their filenames, you can override these defaults and place the commands wherever in the menu system you like. Using the receiveArguments() function, you can also make a given command document behave differently by passing it various parameters. The following sections describe how all this works. Working with menus.xmlThe functionality of Dreamweaver menus is determined by another file in the Configuration folder, menus.xml. This file determines what menus appear, what items each menu contains, and what will happen when a user chooses any of these items. This includes menus in the main application menu bar and, for Dreamweaver/Windows, those in the Site window menu bar. It also includes all contextual menus (those that appear when a user right-clicks/Ctrl-clicks) and all panel options menus (those that appear when a user clicks on the popup options menu icon in the upper-right corner of the panels). menus.xml in the Menus FolderThe menus.xml file is located in the Configuration/Menus folder, along with various other menu- related files (see Figure 5.20). Note that menus.xml is a user-customizable file, which means that if your OS supports multiuser configurations, the copy of it stored in the Dreamweaver main Configuration folder may not be the active copy. Instead, you may find another copy of menus.xml in your user-specific Configuration folder. This is the active copy of the file that you should use in your development work. Figure 5.20. The Configuration/Menus folder, showing menus.xml.
note Windows users, look for your menus.xml in c:\Documents and Settings\username\Application Data\ Macromedia\Dreamweaver MX\Configuration\Menus\. Mac OS X users, look in /Users/username/ Library/Application Support/Macromedia/Dreamweaver MX/Configuration/Menus/. (Substitute your user name for username.) For more on extending Dreamweaver in a multiuser environment, see the section on "User-Specific Configuration Files" in Chapter 1. Another item of interest in the Menus folder is the Custom Sets folder. This folder stores the custom shortcut key definitions that appear when a user accesses the Dreamweaver Edit > Edit Shortcut Keys command. (Again, if you're working in a multiuser environment, the active copy of the Custom Sets folder is stored in your user-specific Configuration folder.) The Structure of menus.xmlBefore opening the menus file, quit Dreamweaver. It's not good to open the menus file while the application is still running because, unlike most other extension files, the menus file needs to be continually accessed. note menus.xml is a long, possibly confusing file with long lines of markup code. Turn off softwrapping in your text editor to get a better sense of how the file structure works. After you quit Dreamweaver, go to your text editor and open menus.xml. Snoop around in there until you get a handle on what's stored in there. As an XML file, menus.xml is built from a series of custom tags that describe menus and menu items. In fact, the entries in this file create the menus and menu items in Dreamweaver, and assign each entry its functionality. Figure 5.21 shows a diagram of the main elements of the file; Table 5.3 shows a list of attributes used in the <menuitem/> tag. Figure 5.21. Diagram of entries in menus.xml.
Table 5.3. Elements of <menuitem> entries in menus.xml
note XML is a more rigidly structured language than HTML. Its syntax must be followed carefully , and it is case-sensitive.
Controlling a Command's Appearance in the MenusAlthough newly added commands appear by default at the bottom of the Commands menu, you can override those defaults by tweaking the menus file and the command file. This is a two-step process:
Passing Parameters to Commands Using receiveArguments()After a command has an entry in the menus file, you can pass parameters to the command file to govern exactly how it works. The Import Word HTML and Clean Up Word HTML commands, for instance, both use the same command file document, Clean Up Word HTML.htm, with only a difference in passed parameters to distinguish them. This process requires two steps: adding an argument to the menus file and receiving the argument in the command file. Because these steps are a bit more complicated than those for controlling a command's appearance in the menus, each step is discussed separately in the following sections. Menu File: Adding the Arguments ParameterWithin the menus file, you pass parameters to a command by adding an arguments property to the <menuitem/> tag (see Table 5.3 for details). In the menus.xml entries for the two Word HTML commands, for instance, the first one passes an argument, and the second one doesn't: <menuitem name="Import _Word HTML..." file="Commands/Clean Up Word HTML.htm" arguments="'import'" id="DWMenu_File_Import_WordHTML" /> <menuitem name="Clean _Up Word HTML..." file="Commands/Clean Up Word HTML.htm" id="DWMenu_Commands_CleanUpWordHTML" /> Command File: Using the receiveArguments() FunctionWithin the command file, you receive the passed arguments and process them by using the receiveArguments() function. This function is called automatically as part of the API procedure for commands (refer back to Figure 5.5). Items specified as arguments in the menus file are passed to this function as an array called arguments . Within the receiveArguments() function, you can call on the elements in the arguments array to set global variables for the main command function to act on, or you can cause actions to happen independently of the command function. In the Clean Up Word HTML command file, for instance, the main command function cleans up Word HTML. But if arguments[0] == "import" is passed then, before the main function executes, the receiveArguments() function creates a new document and copies the contents of the specified Word file into it, as dictated by the function's code: var curDOM, newDOM; // Select the word file to be imported, don't show images, supress not in root warnings. var HTMLfileTypes = new Array("Word HTML Files (*.htm; *.html)*.htm;*. htmlTEXT"); var fileName = browseForFileURL("open", MSG_Word_Import, false, true, HTMLfileTypes); //returns a local filename if (fileName) { // Check for name may not exist. curDOM = dw.getDocumentDOM(fileName); if (curDOM) { newDOM = dw.createDocument(); if (newDOM) { newDOM.documentElement.outerHTML = curDOM.documentElement.outerHTML; } } } |