Dan Hulse
HomeSite/Studio Development
Macromedia, Inc.
One of the strengths of HomeSite has always been to offer
its users support when they need it, but to get out of the
way when they know what they're doing (at least when they
think they know what they're doing). For example, Tag Insight
is great when you're starting to learn HTML, or another
markup language like ColdFusion. But once you're in "fifth
gear" with your coding skills, you don't want any extra
drag, so more often than not, expert coders will turn off
Tag Insight (or at least throttle the timer up so it doesn't
kick in so fast).
With the anthem of "a little knowledge goes a long way,"
we're going to take a look at a few features in HomeSite
which can be streamlined and customized without having to
go through multiple dialogs, or wading through the depths
of the Settings window. With a little scripting magic, and
some insight as to what goes on under the hood, you'll hopefully
get some ideas about how to custom fit the development environment
to you, and not the other way around. Note that all of the
tips presented here apply equally well to ColdFusion Studio
and JRun Studio.
Instant Snippets
"Snippets" are reusable pieces of code which you can store
in an Explorer-like resource window. To create a snippet,
you have to perform the following:
- Display the Snippets resource tab
- Right-click on the Snippets window and select 'Add Snippet'
- Enter the name for the snippet
- Enter the snippet text
- Click 'OK'
Many times, however, the code you want to be contained
in a snippet is already in your document. What we want is
a more automatic way of creating a snippet from existing
text. When you think automation, think 'VTOM' (Visual Tools
Object Model). VTOM allows you to create a JScript or Visual
Basic script which runs inside of HomeSite to automate or
enhance tasks (see the article "Roll
Your Own Features in HomeSite and Studio" sited in the
References section below for an introduction to VTOM). The
examples in this article will use JScript. You can cut and
paste the following code into a blank document and save
it as SelectToSnippet.js:
/*
SelectToSnippet.js
Saves the current selection to a snippet file
*/
function Main() {
var sSnippetText;
var sSnippetName;
var sSnippetStart;
var sSnippetEnd;
var sSnippetPath = 'C:\\Program Files\\Allaire\\ColdFusion Studio 4.5\\UserData\\Snippets\\Common';
var aFileObj = new ActiveXObject("Scripting.FileSystemObject");
var aTextStream;
with (Application){
sSnippetText = ActiveDocument.SelText;
// Make sure we have some text selected
if (sSnippetText == ''){
MessageBox('No text is selected.', 'Save To Snippet', 0);
return;
}
sSnippetName = InputBox('Save To Snippet', 'Enter a name for the snippet:', '');
// Test for blank name
if (sSnippetName == ''){
MessageBoxMessageBox('Snippet cancelled', 'Save To Snippet', 0);
return;
}
// Test the folder exists
if (!aFileObj.FolderExists(sSnippetPath)){
MessageBox('Folder ' + sSnippetPath + ' does not exist', 'Save To Snippet', 0);
return;
}
// Set the names for the start and end snippet files
sSnippetStart = sSnippetPath + '\\' + sSnippetName + '.hss';
sSnippetEnd = sSnippetPath + '\\' + sSnippetName + '.hse';
// Check if we should overwrite an existing start file
if (aFileObj.FileExists(sSnippetStart)){
if (MessageBox('File ' + sSnippetStart + ' already exists, overwrite?', 'Save To Snippet', 52) == 2){
return;
}
}
else{
aFileObj.CreateTextFile(sSnippetStart, true);
}
aTextStream = aFileObj.OpenTextFile(sSnippetStart, 2);
aTextStream.Write(sSnippetText);
aTextStream.Close();
// Create the empty end snippet file
aFileObj.CreateTextFile(sSnippetEnd, true);
}
}
Let's take a quick look at the major points of this script.
First, the variable sSnippetPath needs to point to an existing
Snippets folder. The folders in the Snippets resource window
correspond directly to folders in the UserData\Snippets
directory which exists in the HomeSite or Studio application
directory (though this location can be changed, as we'll
see later).
We have a number of error checks in this script, the first
of which is to make sure we actually have some text selected.
Next we use the InputBox method to display a generic input
dialog. The third parameter in the InputBox method specifies
the default text which appears in the box. This is also
the result which is returned if the user selects Cancel,
so we use an empty string for the parameter and test for
this in the return value. If the return is an empty string,
we know the user cancelled out, so we exit out of the script.
For any file I/O, we use the built-in JScript FileSystem
object. We utilize this object to test for a non-existent
directory, and to test if the snippet file already exists.
A single snippet actually consists of two files. One file
ends in the extension .hss and is the starting text of the
snippet. The other file ending in .hse is the ending text.
Although we are not adding text to the ending snippet, we
still need to create an empty file for it.
You can assign this script to either a custom toolbar button
or a keyboard shortcut through the Customize dialog. Once
assigned, you use the script by simply highlighting some
text, pressing the appropriate toolbar button or shortcut
key, and entering in the name of the snippet. The only caveat
to this method of snippet creation is that you will need
to press F5 in the Snippets window to refresh the list of
snippets.
Swapping Settings
There may be cases when you'll need to adjust your development
environment according to the needs of a particular project,
or even based upon the language you're currently coding
in. Changing your setup via the Settings dialog can get
tiresome if you need to make changes repeatedly. Fortunately,
VTOM scripting supports changing most of the application
settings via the SetApplicationSetting method. We can use
this method to create a simple script which we can run before
a particular project. Creating multiple setup scripts for
different projects allows you to "dial in" just the right
coding environment in seconds.
As an example, let's say a particular project requires
the following setup:
- Set the current folder
- Set the default extension for new documents
- Lowercase inserted tags
- Change the location of the Snippets folders
- Set a font for the editor
Here's an example script to change these settings:
/*
SettingsDemo.js
Demonstrates the SetApplicationSetting method
*/
function Main() {
SET_DEFAULT_EXTENSION = 4;
SET_LOWERCASE_TAGS = 30;
SET_DIR_SNIPPETS_PRIVATE = 151;
SET_EDITOR_FONTNAME = 300;
with (Application){
CurrentFolder = 'C:\\INETPUB\\WWWROOT\\MAIN';
SetApplicationSetting(SET_DEFAULT_EXTENSION, 'CFM');
SetApplicationSetting(SET_LOWERCASE_TAGS, true);
SetApplicationSetting(SET_DIR_SNIPPETS_PRIVATE, 'C:\\INETPUB\\WWWROOT\\MAIN\\SNIPPETS');
SetApplicationSetting(SET_EDITOR_FONTNAME, 'Lucida Console');
}
}
The Application object's SetApplicationSetting takes two
parameters. The first is an integer which corresponds to
the particular setting you want to change. There is a list
of these values in HomeSite's online help (Scripting the
Visual Tools Object Model). For clarity, the above script
assigns the values to variables which are somewhat more
descriptive.
The second parameter passed into SetApplicationSettings
is the value for the setting. The type of this parameter
varies according the individual settings. The Application
object also provides a GetApplicationSetting method, which
returns the current value of a setting. You could use this
method to store the current settings to a text file before
modifying them.
Note that many settings need a restart of HomeSite in order
to properly take affect. Still, it's better than having
to remember to manually set each individual setting.
Creating File Sets
While a particular project might consist of many source
files, many times you'll find you are repeatedly modifying
just a subset of those files. While the Favorite Folders
and Recent Files menus are helpful, they are limited in
the number of entries they contain. We can easily create
a script which automatically loads just the files we need.
Here's an example:
/*
FileSet_Demo.js
Loads a specified set of files
*/
function Main() {
var aFiles = new Array();
aFiles[0] = 'C:\\Inetpub\\wwwroot\\Project1\\Index.html';
aFiles[1] = 'C:\\Inetpub\\wwwroot\\Project1\\Content.html';
aFiles[2] = 'C:\\Inetpub\\wwwroot\\Project1\\TodaysNews.html';
with (Application){
// Close any open files
if (!CloseAll(true)){
return
}
// Load the files
for (i = 0; i <aFiles.length; i++){ OpenFile(aFiles[i]); } // Set the first document as current DocumentIndex=0; } } /pre>
Here we simply create a new array and then populate it with the desired filenames. Note that in JScript, you need to "escape" the backslash directory delimiter, hence the '\\' characters in the filenames. We close any open files (prompting the user to save any which have been modified), and then loop through the array to load the file set. Finally, to clean up, we just make the first loaded document active. Assign this script to a toolbar button or keyboard shortcut, and you can instantly load just the files you need, without hunting through menus and directory trees.
Conclusion
No coding environment is perfect, but the best tools are the ones which are flexible enough to adapt to each user's personal preferences and abilities. By using scripting to go "under the hood" in HomeSite, you can really fine tune the program to better fit your style of coding, even if your style happens to change with every project.
Resources
Scripting the Visual Tools Object Model
HomeSite/ColdFusion Studio/JRun Studio help file.
Roll Your Own Features in HomeSite and Studio
http://www.allaire.com/handlers/index.cfm?id=21413
Microsoft Scripting Technologies
http://msdn.microsoft.com/scripting/default.htm?/scripting/start.htm
Microsoft Scripting Clinic
http://msdn.microsoft.com/voices/scripting05142001.asp
Microsoft JScript Documentation (HTML help file)
http://msdn.microsoft.com/scripting/jscript/download/jsdoc.exe
HomeSite Dev Center
http://www.allaire.com/developer/hsreferencedesk/