home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / web_dev / files / mwjpp516.exe / %MAINDIR% / Tools / Application / runHtmlBrowser.script < prev    next >
Encoding:
Text File  |  2001-06-19  |  6.1 KB  |  241 lines

  1. !!script 
  2. // Copyright ⌐ 1997-1999 - Modelworks Software
  3. // @Modified build 273 cm19990307 - added OpenUrl(path) function
  4. // @Modified build 284 cm19990328 - encoded special characters
  5.  
  6. /**
  7. @Tool: runHtmlBrowser~launches your default HTML browser using either the current 
  8. document if it is a HTML or VRML type or a document returned by the choose file dialog. Use the 
  9. Tools/Configuration/htmlBrowser script to set or change your default HTML browser. 
  10. @EndTool: 
  11. @Summary: runHtnlBrowser~launches your default HTML browser 
  12. */
  13.  
  14. var output = getOutput();
  15.  
  16. function DoCommand()
  17. {
  18.     saveAll(false);
  19.     
  20.     var path = "";
  21.     
  22.     // If the current document is a HTML file then use that file
  23.     var editor = getActiveEditor();
  24.     if (editor)
  25.     {
  26.         var fileType = editor.getFile().fileType.toLowerCase();
  27.         if (fileType == "htm" || fileType == "html" || fileType == "wrl" || fileType == "htp")
  28.         {
  29.             path = editor.path;
  30.         }
  31.     }
  32.     
  33.     // Else ask the user to locate a HTML or VRML file
  34.     if (path == "")
  35.     {
  36.         var file = 
  37.         chooseFile("Choose a HTML or VRML file to open in your browser", 
  38.         "HTML/VRML files", "*.htm*;*.wrl;*.htp");
  39.         if (file && file.exists)
  40.         {
  41.             path = "file://" + file.urlpath;
  42.         }
  43.     }
  44.     
  45.     if (path.length > 0)
  46.     {
  47.         RunHtmlBrowser(path);
  48.     }
  49. }
  50.  
  51. function RunHtmlBrowser(path)
  52.     var helpers = getMapFile("ApplicationHelpers");  
  53.     
  54.     // Get the HTML browser file object
  55.     var browser = helpers.lookup("DefaultHtmlBrowser");
  56.  
  57.     
  58.     if (browser && browser.exists)
  59.     {
  60.         output.clear();
  61.         output.writeLine("Starting HTML Browser for " + path);
  62.         
  63.         var openHtmlInANewWindow = getMapFileValue("Preferences", "Open html files in a new browser window", false);
  64.         if (openHtmlInANewWindow)
  65.         {
  66.             shellExecute("open", path, browser.path); 
  67.         }
  68.         else if (searchInString(browser.path, "Netscape", 0, false))
  69.         {
  70.             var message = path + encodeHex(0,1) + 
  71.             ",text/html" + encodeHex(0,1) + "," + encodeHex(-1,4);
  72.             if (sendDDEMessage("NSShell","WWW_ShowFile", message))
  73.             {
  74.                 // This message is not needed for Netscape 4.x
  75.                 // sendDDEMessage("Netscape","WWW_Activate", 
  76.                 //    encodeHex(-1,4) + "," + encodeHex(0,4));
  77.             }
  78.             else if (sendDDEMessage("Netscape","WWW_ShowFile", message))
  79.             {
  80.                 // Netscape does not process this message correctly
  81.                 // sendDDEMessage("Netscape","WWW_Activate", 
  82.                 //    encodeHex(-1,4) + "," + encodeHex(0,4));
  83.             }
  84.             else
  85.             {
  86.                 shellExecute("open", path, browser.path); 
  87.             }
  88.         }
  89.         else
  90.         {
  91.             var message = "\"" + path + "\",text/html,-1,";
  92.             if (sendDDEMessage("IExplore","WWW_ShowFile", message))
  93.             {
  94.                 sendDDEMessage("IExplore","WWW_Activate", "0xFFFFFFFF,0")
  95.             }
  96.             else
  97.             {
  98.                 shellExecute("open", path, browser.path); 
  99.             }
  100.         }
  101.     }
  102.     else
  103.     {
  104.         var result = confirm("You need to run the HtmlBrowser setup script\n"
  105.         + "before you can use this command.\n" +
  106.         "Do you want to run the setup script now?");
  107.         if (result)
  108.         {
  109.             run("Configuration\\HtmlBrowser");
  110.         }
  111.     }
  112. }
  113.  
  114. function OpenUrl(url)
  115. {     
  116.     var helpers = getMapFile("ApplicationHelpers");   
  117.     
  118.     // Get the HTML browser file object
  119.     var browser = helpers.lookup("DefaultHtmlBrowser");
  120.     
  121.     if (browser && browser.exists)
  122.     {
  123.         //output.clear();
  124.         //output.writeLine("Starting HTML Browser for " + url);
  125.         
  126.         if (searchInString(browser.path, "Netscape", 0, false))
  127.         {
  128.             var message = url + encodeHex(0,1) + 
  129.             ",text/html" + encodeHex(0,1) + "," + encodeHex(-1,4);
  130.             if (sendDDEMessage("NSShell","WWW_OpenURL", message))
  131.             {
  132.                 // This message is not needed for Netscape 4.x
  133.                 // sendDDEMessage("Netscape","WWW_Activate", 
  134.                 //    encodeHex(-1,4) + "," + encodeHex(0,4));
  135.             }
  136.             else if (sendDDEMessage("Netscape","WWW_OpenURL", message)) 
  137.             {
  138.                 // Netscape does not process this message correctly
  139.                 // sendDDEMessage("Netscape","WWW_Activate", 
  140.                 //    encodeHex(-1,4) + "," + encodeHex(0,4));
  141.             }
  142.             else
  143.             {
  144.                 shellExecute("open", url, browser.path); 
  145.             }
  146.         }
  147.         else
  148.         {    
  149.             // IE does not appear to accept a <path>#<target> when using the
  150.             // WWW_ShowFile. So we create a temporary file and put the link
  151.             // <path>#<target> in the temporary file in a Refresh command.
  152.             
  153.             // Create a html file with a jump to the correct <path>#<target>
  154.             var tempPath = File.getDataPath() + "\\Temp\\runHtmlBrowserUrl.html";
  155.             var editor = newEditor(tempPath, false);
  156.             if (editor)
  157.             {
  158.                 url = EncodeSpecialCharacters(url); //cm19990328
  159.                 
  160.                 editor.removeAll();
  161.                 
  162.                 editor.append("<html> <head> <meta http-equiv=\"Refresh\" content=\"0; url='");
  163.                 editor.append(url);
  164.                 editor.append("'\"></head><body></body></html>");
  165.                 editor.close(true,false);
  166.                 
  167.                 var message = "\"" + tempPath + "\",text/html,-1,,,,,";
  168.                 if (sendDDEMessage("IExplore","WWW_ShowFile", message))
  169.                 {
  170.                     sendDDEMessage("IExplore","WWW_Activate", "0xFFFFFFFF,0")
  171.                 }
  172.                 else
  173.                 {
  174.                     shellExecute("open", url, browser.path); 
  175.                 }
  176.                 
  177.             }
  178.         }
  179.     }
  180.     else
  181.     {
  182.         var result = confirm("You need to run the HtmlBrowser setup script\n"
  183.         + "before you can use this command.\n" +
  184.         "Do you want to run the setup script now?");
  185.         if (result)
  186.         {
  187.             run("Configuration\\HtmlBrowser");
  188.         }
  189.     }
  190. }
  191.  
  192. function EncodeSpecialCharacters(url) //cm19990328
  193. {
  194.     var index = url.indexOf(' ');
  195.     while (index > -1)
  196.     {
  197.         url = replaceInString(url, index, 1, "%20");
  198.         index = url.indexOf(' ');
  199.     }
  200.     
  201.     index = url.indexOf(':');
  202.     while (index > -1)
  203.     {
  204.         url = replaceInString(url, index, 1, "%3A");
  205.         index = url.indexOf(':');
  206.     }
  207.  
  208.     index = url.indexOf('\\');
  209.     while (index > -1)
  210.     {
  211.         url = replaceInString(url, index, 1, "%5C");
  212.         index = url.indexOf('\\');
  213.     }
  214.  
  215.     index = url.indexOf(',');
  216.     while (index > -1)
  217.     {
  218.         url = replaceInString(url, index, 1, "%2C");
  219.         index = url.indexOf(',');
  220.     }
  221.     
  222.     index = url.indexOf('(');
  223.     while (index > -1)
  224.     {
  225.         url = replaceInString(url, index, 1, "%28");
  226.         index = url.indexOf('(');
  227.     }
  228.     
  229.     index = url.indexOf(')');
  230.     while (index > -1)
  231.     {
  232.         url = replaceInString(url, index, 1, "%29");
  233.         index = url.indexOf(')');
  234.     }
  235.     return url;
  236. }
  237.  
  238. !!/script
  239.  
  240.