|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
MXDJ TOP LINKS YOU MUST CLICK ON ! Director
Double-Clicking A Document
A professional projector makes it easier
By: Tom Rockwell
Digg This!
Thanks to the extensive range of xtras available, a Director projector can create any type of document and write it to the user's hard disk. In Windows Explorer and the Macintosh Finder, users expect that a double-click on a document icon will launch the program that created it. This article shows you how to associate files with the Director projector that created them. By the end of this article, you will have created a simple word processor application that creates its own document files; double-clicking on the icon of one of these files will open your Director application so that you can edit the document. There are many situations in which this technique will prove useful. For example, you may want to save the state of a game in progress, a customized slide show presentation, or mark up data for a video sequence. But recognizing the files created by a projector is not something that Director does naturally. By default, when the user double-clicks on a document created by a Director projector, the operating system prompts the user to select an application to open the file. If the user properly selects your projector, then your projector is launched, but the document still doesn't open. For my programs, this is unacceptable. I sell programs that are often in direct competition with titles from major software vendors. Something like the double-click may seem trivial, but the little things add up and the less my users have to think about, the more likely they are to purchase the product. I like my projectors to be as professional as possible, complete with robust menus, keyboard shortcuts, user-defined preferences, and standard user-interface elements. In the end, if the user can't tell that the program was created with Director, then I've done my job. This means finding a way to deal with double-clicking on documents. Thankfully Director has an undocumented feature - a system variable called the commandLine that corresponds to the operating system's command line. When the user double-clicks on a document, the operating system's command line gets updated with the path to the document, and Director's commandLine provides us with this information. But figuring out which document the user clicked on is only half the battle. The operating system also needs to be told to launch your projector instead of prompting the user, and then your application has to be set up to deal with what happens so the file actually opens. This article will give you step by step instructions for getting this to work for Macintosh OSX and Windows platforms. The commandLine doesn't exist on pre-OSX Macintosh operating systems because those systems don't have a command line at all. So, this technique will not work on OS8 or OS9. This article assumes you have a basic understanding of Lingo, movie scripts, frame scripts, and working with xtras. You will also need some additional tools besides Director. For Mac OSX, you will need a program called Property List Editor, which can be found on the Developer Tools CD that came with your copy of OSX. If you don't have that, you can get away with just using a text editor, but you'll need to have a basic understanding of XML. You will also need DirectorMX or DirectorMX2004 since those are the only versions of Director that can create projectors for OSX. For Windows, you will need a third-party xtra since we will be editing the Windows Registry. This article uses the BuddyAPI xtra, but any xtra that can modify the Registry will work, such as the Registry xtra and others. Setting Up Your Application Launch Director and create a new document with a white background. It can be any size, but there's no reason to make it huge, so make it fit comfortably on your screen. Next, create a field member by selecting Insert > Control > Field. The field will appear onstage with a cursor in it. Leave it blank for now and find the field cast member in the cast window. Name the cast member "content." Now select the field sprite on the stage and open the Property Inspector. Click on the Field tab and click on the Editable check mark. In the Framing dropdown menu select "Scrolling" and resize the sprite so it fills most of the stage. Leave some room at the bottom for some buttons that we'll create in a moment. Change the Property Inspector to list mode if it isn't already and set the border of the field to 1. Open the score window if it isn't already and double-click on the script channel for frame 5. This will open the script window with an exitFrame handler filled in for you. Type into the frame to make the movie loop here. We will be adding more to this script later. The next thing we have to do is set the field member to clear its content when the movie starts so the user is presented with a blank document. You don't absolutely have to do this, but it's easier than trying to remember to manually clear the field member before you publish the movie. There are a couple of ways to do this. You can write a behavior and attach it to the script to clear the cast member on beginSprite. Or, do it on a movie script on prepareMovie that runs every time the movie is run. We'll be using on prepareMovie for some other stuff later so let's use that. Open the script window and click on the + to insert a new script. Type in:
on prepareMovie
member("content").text = ""
end
Open the Script tab in the Property Inspector and make sure the Type drop down menu is set to Movie. Now if you run the movie, you'll be able to type into the field. Then, stop and restart the movie and you'll see the field get cleared, leaving you with a fresh new document. Save The Document This handler makes use of the FileIO xtra that comes with Director, which is used to read and write text files with Lingo. Any xtra that reads and writes to the user's hard drive can be used in its place, such as vList, BinaryIO, and others. The first thing the handler does is create an instance of the FileIO xtra to use. Then it sets the filter mask to only allow the user to work with documents created with our program. It does this by defining our custom document type. Our document, although it will be nothing more than a text file, will be a "MiniWord Document." We can't just use a .txt file since both Macintosh and Windows already have programs to deal with those kinds of documents. So, our documents will have a .minw extension on Windows, and a 'minW' file type on the Macintosh. (More on that in a moment.) Note that this is platform-specific code, so I have an if...else statement to branch out to whichever platform is hosting the Director movie. This also allows you to code once and publish for both platforms without having to rewrite any of your Lingo. (Syntax note: The _system.environmentproplist.platform call is the new syntax introduced in DirectorMX2004. If you're still using DirectorMX, you will need to replace this item with the platform in order to make the code work.) Next, the handler calls displaySave, which pulls up the standard OS save dialog box so the user can select where to save the file and what to name it. This function returns the full path to the document the user wants to save, or an empty string (Macintosh) or <Void> (Windows) if the user hits Cancel. The next thing we do is make sure the user didn't hit Cancel. If filePath is not an empty string then we can proceed. We then create the file, open it, store the string we got from the Content member, write to the file, and close it. On Windows, file types are specified by a file extension such as .txt for text files and .doc for Microsoft Word files. These are tracked by the Windows Registry, which associates these file extensions with the proper application and is why we set the filter mask above. If the user forgets to add .minw to the file name, the system will take care of it, ensuring that the file keeps its program association. We haven't actually edited the Windows Registry yet, so right now the extension is meaningless, but we'll take care of that soon. On the Macintosh file, types and associations are traditionally marked in the file itself in a section called the Resource Fork. The file will have a four-character identifier called a creator code. It is traditionally written in single quotes and tells the operating system which application it should use to open the document. DirectorMX2004's creator code is "MDO3". (Interesting that it's not "MD04"). You can pick any four-character code for your application. I chose "MnWd" for our MiniWord application. All ASCII characters are acceptable, including spaces and special characters. So, if you want to make your application's code '#$ !' that is perfectly fine. However, there are some rules, the most important of which is that Apple has reserved all lowercase-only codes for internal use. This is why my application code is "MnWd" and not "mnwd". And although it's not required, Apple suggests that you register your application code with them at their developer site (http://developer.apple.com/datatype/creatorcode.html). This is a good idea to make sure nobody else is already using your code. They used to require that you register your file-type code as well, but they no longer ask for that. The file-type code is the same thing - a four-character code that identifies the kind of file it is. Text files are identified with "TEXT"; JPEG files are marked as "JPEG." This is why JPEGs created with Photoshop will reopen in Photoshop when you double-click on them instead of opening in your Web browser, as they do on Windows. Our MiniWord application will create "minW" files. Run the movie, type something into the content field, and hit Save to make sure the document saves as expected. Open The Document This code is very similar to the Save As code. The main difference here is that instead of calling displaySave, we call displayOpen, which prompts the user to select a file and returns its path. And again, if the user hits Cancel, the path is an empty string. If not, we open the file specified by the user, read the whole file, store the text we just read in the Content member and close the file. That's it. Here the setFilterMask handler only displays MiniWord files for the user to select. This way your program doesn't try to open Word or Photoshop documents. The next step is to associate our file format with the application. This has to be done separately for each platform. The Mac Package
The Windows Registry Ideally, your program will only write to the user's Registry once. That means your program would need to keep track of the fact with a preferences file or some similar method. Saving preferences files is beyond the scope of this article so for now we will do all our work on prepareMovie. This will run every time the program is launched, which is not ideal but will do for the purposes of instruction. In your movie script in the on prepareMovie handler, add a call to a handler called editWindowsRegistry so the whole handler now looks like this:
on prepareMovie
member("content").text = ""
editWindowsRegistryEditWindowsRegistry()
end
Now we need to define this handler. Type in the code as shown in Figure 5. The next time your movie is run, it will modify your Registry to associate the file types with your application. At this point, double-clicking on a saved document will launch your projector but will not open it for the same reason as for the Mac - we haven't yet added the code to handle that. Opening The Files That Were Double-Clicked Not only does the program launch when the user double-clicks on a document, but if the application is already running, it is brought to the front. So our program needs to periodically check the commandLine to see if it has changed. If it has, we need to open the new document. To do this, we'll create a global variable called myCommandLine and initialize it to an empty string. So let's modify our on prepareMovie handler to accommodate this.
global myCommandline
on prepareMovie
member("content").text = ""
myCommandline = ""
editWindowsRegistry
end
If the user launched your program normally, then the commandLine will return an empty string, which matches myCommandline. So all we have to do is see if the commandLine has changed, and since we have to do this periodically, we'll just do it on the exitFrame handler we have running on frame 5. global myCommandline on exitFrame me -- Check to see if the commandLine has changed. if the commandLine <> myCommandline then -- Open the file specified by the commandLine. openThisDocument() end if go the frame end Checking the status of the commandLine of every frame is usually not necessary. And certainly, if you're authoring a game, you don't want to take a speed hit by doing an unnecessary check every frame. I'd suggest only checking on the title screen or some other relatively calm screen. The openThisDocument handler will now get called whenever the commandLine changes, which happens whenever a user double-clicks on a saved document that is associated with your application. It's structure is very similar to the code we attached to the Open button, but instead of prompting the user to select a file it simply uses the commandLine to find the file. In the movie script, type in the code from Figure 6. This code is the same as the open code, except for the section at the top that gets the path from the commandLine. The way that works is that it first saves what the current itemDelimiter is in a local variable so we can restore it later. Then, it sets the itemDelimiter to an apostrophe and reads the entire commandLine. Then, it gets the index number of the second-to-last item in the list and uses that to determine the filepath of the document that was double-clicked. Finally, it restores the itemDelimiter to its default value. This is the last step in making the program work. Publish your movie again. If you're on a Mac, be sure to place the projector in the MacOS folder in your package. Double-click a saved document and it will launch your projector and open the document. Double-click another file and it will bring your program to the front and open that one. Conclusion
LATEST FLEX STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||