Download: CityDesk Keyword Link Generator Script

Thursday, September 12, 2002

file name file size (kb)
keywordlist.zip 2

This small JavaScript script gives one example of what you might do with the bdCityDeskAPI.  It generates an alphabetical listing of your keywords and, under each keyword, provides links to the articles that have them.  It's an easily adaptable script.

The script is packaged as a Zip file so your browser and anti-virus software won't throw a fit when they see you downloading an executable file with a .JS extension.  Better yet, here is the entire contents of the file which you can simply copy and paste where you want:

 


/* Bill Dawson
 * http://www.BillDawson.com
 * bill@BillDawson.com
 *
 * 12.Sept.2002
 * Use the bdCityDeskAPI to generate an alphabetical list of keywords used in your CityDesk site
 * and provide links to the articles using them.
 * Uses the bdCityDeskAPI (go to http://www.BillDawson.com and click on Downloads).*/


/*******************************************************
   Global "Constants"
********************************************************/
var PROGID = "bdCityDeskAPI.Application";
var KEYWORD_DELIM = "{space}";
var CDFILE = "d:\\mydocs\\citydeskSites\\billdawson.cty";
var CRLF = String.fromCharCode(13,10);
var OUTFILE = "d:\\mydocs\\CityDeskSites\\keywords.htm";

/*******************************************************
   Global variables
********************************************************/
var cdapi = null;   // The bdCityDeskAPI obj
var outstream = null;  // the output TextStream


/*******************************************************
   Supporting Functions
********************************************************/

function engineVersion() {
   return ScriptEngineMajorVersion() + "." + ScriptEngineMinorVersion() + "." + ScriptEngineBuildVersion();
}

function verboseException(e) {
   return e + "; #" + e.number + "; " + e.description;
}

function openCityDeskDB() {
   var sMsg = "";
   
   try {
      cdapi = new ActiveXObject(PROGID);
   } catch (e) {
      throw "Could not instantiate bdCityDeskAPI: " + verboseException(e);
   }
   
   try {
      if (!cdapi.PrecheckDBFile(CDFILE,sMsg))
         throw "Could not access CityDesk file specified: " + CDFILE + ".  Error msgs, if any: " + sMsg;
   } catch (e) {
      throw "Error while trying to pre-check CityDesk file " + CDFILE + verboseException(e);
   }
   
   try {
      cdapi.init("dbfile=" + CDFILE + CRLF + "keywordDelimiter=" + KEYWORD_DELIM); // see the API help file for this syntax      
   } catch (e) {
      throw "Error initializing the bdCityDeskAPI using CityDesk file " + CDFILE + verboseException(e);
   }
}
   

function initOutputStream() {
   
   try {
      var fso = new ActiveXObject("Scripting.FileSystemObject");
      outstream = fso.CreateTextFile(OUTFILE,true);
   } catch (e) {
      throw "Error trying to initialize output file stream.  " + verboseException(e);
   }
}
      

function getKeywords() {
   // returns a JavaScript Array
   try {
      var kws = cdapi.GetAllKeywords();
      var kwsvb = new VBArray(kws);
      kws = kwsvb.toArray();
      if (!Array.prototype.isPrototypeOf(kws))
         throw "Expected keywords array was not created.";
      return kws;   
   } catch(e) {
      throw "Error attempting to get keywords.  " + verboseException(e);
   }
}


/*******************************************************
   Main function
********************************************************/
function main() {
   var i = 0;
   var j = 0;
   var articleSets = null;
   var running = "";
   
   try {
      running="DB Open";   
      openCityDeskDB();
   
      running="GetKeywords";
      var kws = getKeywords();
      
      if (kws.length>0) {         
         running="InitOutput";
         initOutputStream();
         for (i=0; i < kws.length; i++) {
            if (kws[i]!=" " && kws[i]!="") {
               running="Article Subset";
               articleSets = cdapi.ArticleSetsCollection.SubsetByKeyword(kws[i]);      
               running = "Subset Count";
               if (articleSets.Count() > 0) {
                  running = "Write to stream";
                  outstream.WriteLine("<h2>" + kws[i] + "</h2>");            
                  for (j = 1; j <= articleSets.Count(); j++) {
                     outstream.Write("<a href=\"" + articleSets(j).MagicName + "\">");
                     outstream.Write(articleSets(j).StructureName + "</a><br>" + CRLF);
                  }
               }
            }
         }
      }
   } catch(e) {
      WScript.Echo(verboseException(e) + CRLF + "(" + running + ")" + CRLF + "Script Engine: " + engineVersion());
   } finally {
      // Make sure the text stream is closed.  Ignore any exceptions, as 
      // maybe the textstream isn't even open.
      try {textstream.Close();} catch(e) {}
   }
}


/*******************************************************
   Entry Point
********************************************************/
main();
WScript.Echo("Script Complete.");