home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 2000 July / macformat-092.iso / Fireworks 3 / Settings / Batch Code / BatchGen.jst next >
Encoding:
Text File  |  1999-11-19  |  4.3 KB  |  178 lines

  1.  
  2. // Macromedia Fireworks Batch Script Generator
  3. // Copyright (c) 1998, 1999 Macromedia. All rights reserved.
  4.  
  5.     var errorString = null;
  6.     var srcFile = null;
  7.     var dstFile = null;
  8.     
  9.     do {
  10.         
  11.         if (Files.exists(theScriptFilePath) == false) {
  12.             if (!Files.createFile(theScriptFilePath, App.appMacJsfFileType, App.appMacCreator)) {
  13.                 errorString = Files.getLastErrorString();
  14.                 break;
  15.             }
  16.         }
  17.         
  18.         var dstDir = Files.getDirectory(theScriptFilePath);
  19.         var dstFilePath = Files.getTempFilePath(dstDir);
  20.         if (!Files.createFile(dstFilePath, App.appMacJsfFileType, App.appMacCreator)) {
  21.             errorString = Files.getLastErrorString();
  22.             break;
  23.         }
  24.  
  25.         var srcFilePath = Files.makePathFromDirAndFile(App.appBatchCodeDir, App.getPref("BatchTemplateName"));
  26.         if (!Files.exists(srcFilePath)) {
  27.             errorString = Files.getLastErrorString();
  28.             break;
  29.         }
  30.         
  31.         var srcFile = Files.open(srcFilePath, false);
  32.         if (srcFile == null) {
  33.             errorString = Files.getLastErrorString();
  34.             break;
  35.         }
  36.  
  37.         var dstFile = Files.open(dstFilePath, true);
  38.         if (dstFile == null) {
  39.             errorString = Files.getLastErrorString();
  40.             break;
  41.         }
  42.         
  43.         while (true) {
  44.  
  45.             var curline = srcFile.readline();
  46.             if (curline == null)
  47.                 break;
  48.  
  49.             WriteSubstituteInfo(dstFile, curline);
  50.             dstFile.write("\n");
  51.             
  52.         }
  53.  
  54.         if (srcFile != null)
  55.             srcFile.close();
  56.         srcFile = null;
  57.         if (dstFile != null)
  58.             dstFile.close();
  59.         dstFile = null;
  60.         
  61.         if (!Files.swap(dstFilePath, theScriptFilePath)) {
  62.             errorString = Files.getLastErrorString();
  63.             break;
  64.         }
  65.         
  66.         if (!Files.deleteFileIfExisting(dstFilePath)) {
  67.             errorString = Files.getLastErrorString();
  68.             break;
  69.         }            
  70.  
  71.     } while (0);
  72.     
  73.     // it's safe to close the file multiple times.
  74.     if (srcFile != null)
  75.         srcFile.close();
  76.     srcFile = null;
  77.     if (dstFile != null)
  78.         dstFile.close();
  79.     dstFile = null;
  80.  
  81.     if (dstFile != null)
  82.         dstFile.close();
  83.  
  84.     if (errorString != null)
  85.         alert(errorString);
  86.  
  87.     function WriteTabs(dstFile, num)
  88.     {
  89.         for (var i = 0; i < num; i++)
  90.             dstFile.write("\t");
  91.     }
  92.  
  93.     function MustWriteAsUnicode(tmp)
  94.     {
  95.         var len = tmp.length;
  96.         for (var i = 0; i < len; i++) {
  97.             var cc = tmp.charCodeAt(i);
  98.             // If there are any characters outside ASCII, we write the entire 
  99.             // string as a concatenation of Unicode values. This reduces weirdness 
  100.             // on non-Roman systems, or when transferring files across platforms.
  101.             // If you find this inconvenient or unnecessary, modify this routine
  102.             // to always return false.
  103.             if (cc < 32 || cc > 127) {
  104.                 return true;
  105.             }
  106.         }
  107.         return false;
  108.     }
  109.  
  110.     function WriteAsUnicode(dstFile, tmp, depth)
  111.     {
  112.         dstFile.write("String.fromCharCode(");
  113.         var len = tmp.length;
  114.         for (var i = 0; i < len; i++) {
  115.             if (i > 0)
  116.                 dstFile.write(",");
  117.             dstFile.write(tmp.charCodeAt(i));
  118.         }
  119.         dstFile.write(")");
  120.     }
  121.  
  122.     function WriteObjectInitializerString(dstFile, someObject, depth)
  123.     {
  124.         if (someObject == null) {
  125.             dstFile.write("null");
  126.         } else if (typeof(someObject) == "number") {
  127.             dstFile.write(someObject);
  128.         } else if (typeof(someObject) == "string") {
  129.             var tmp = someObject.toString();
  130.             if (MustWriteAsUnicode(tmp) == true) {
  131.                 WriteAsUnicode(dstFile, tmp, depth + 1);
  132.             } else {
  133.                 dstFile.write('"');
  134.                 // Note that even if we can write it as straight characters,
  135.                 // we must replace all backslashes with backslash-backslash,
  136.                 // and all doublequotes with backslash-doublequote.
  137.                 tmp = tmp.replace(/\\/g, "\\\\");
  138.                 tmp = tmp.replace(/\"/g, "\\\"");
  139.                 dstFile.write(tmp);
  140.                 dstFile.write('"');
  141.             }
  142.         } else if (typeof(someObject) == "object") {
  143.             var dummyArray = [];
  144.             var isArray = (someObject.constructor == dummyArray.constructor);
  145.             var first = true;
  146.             dstFile.write(isArray ? "[" : "{");
  147.             for (var prop in someObject) {
  148.                 if (first)
  149.                     dstFile.write("\n");
  150.                 else
  151.                     dstFile.write(",\n");
  152.                 WriteTabs(dstFile, depth + 1);
  153.                 if (!isArray) {
  154.                     dstFile.write(prop);
  155.                     dstFile.write(":");
  156.                 }
  157.                 WriteObjectInitializerString(dstFile, someObject[prop], depth + 1);
  158.                 first = false;
  159.             }
  160.             if (!first) {
  161.                 dstFile.write("\n");
  162.                 WriteTabs(dstFile, depth);
  163.             }
  164.             dstFile.write(isArray ? "]" : "}");
  165.  
  166.         } else {
  167.             dstFile.write(someObject);
  168.         }
  169.     }
  170.  
  171.     function WriteSubstituteInfo(dstFile, curline)
  172.     {
  173.         if (curline == "#BATCH_SETTINGS#")
  174.             WriteObjectInitializerString(dstFile, batch, 0);
  175.         else
  176.             dstFile.write(curline);
  177.     }
  178.