home *** CD-ROM | disk | FTP | other *** search
/ Internet Magazine 2003 July / INTERNET105.ISO / pc / software / windows / building / homesite / actionscripts / editlinkedfile.js < prev    next >
Encoding:
JavaScript  |  1998-10-29  |  2.8 KB  |  95 lines

  1. /*    This script created by Christopher Bradford, Alive! Online.
  2.  
  3.     You may distribute this freely, but please keep the headers intact.
  4.     
  5.     This script requires HomeSite/CF Studio 4 RC4 and above.
  6.  
  7.     Installation:
  8.     Create a custom button for the .js file 
  9.  
  10.     Copyright 1998
  11.      Christopher Bradford
  12.     bradford@aliveonline.com
  13. */
  14.  
  15. // MessageBox constants
  16. var hsOK = 0;
  17. var hsExclamation = 48;
  18. var hsInformation = 64;
  19.  
  20. var app=Application;
  21. var active = app.ActiveDocument;
  22.  
  23. function Main() {
  24.  
  25. // first, get the current cursor position and selection to restore it later
  26. var startHere = active.SelStart;
  27. var intSelLength = active.SelLength;
  28.  
  29. // now, select the current tag with a workaround
  30. active.BeginUpdate();
  31. active.GotoPreviousStartTag(false);
  32. active.GotoNextStartTag(true);
  33. var selectedText = active.SelText;
  34.  
  35. // restore the original selection
  36. active.SelStart = startHere;
  37. active.SelLength = intSelLength;
  38. active.EndUpdate();
  39.  
  40. var strHREF = app.HTMLGetAttribute(selectedText, "HREF");
  41.  
  42. var ftpRE = /ftp:\/\//i;
  43. var httpRE = /http:\/\//i;
  44.  
  45. if (strHREF.search(ftpRE) != -1) {    // it's an FTP link
  46.     app.MessageBox("This script can't open FTP links yet.  Watch for an updated version.", "Not supported", hsOK);
  47. }
  48. else if (strHREF.search(httpRE) != -1) {    // it's an absolute link
  49.     var linkedFile = strHREF; 
  50.     var strURLFile = app.GetURL(strHREF);
  51.     if (strURLFile != "") {
  52.         app.NewDocument(false);
  53.         active.Text = strURLFile;
  54.     }
  55.     else { app.MessageBox("Couldn't open file!", "Failure", hsOK + hsExclamation); }
  56. }
  57.  
  58. else {    // it's a relative link
  59.     if (active.Filename == "") {    // file has not yet been saved
  60.         app.MessageBox("You must save the file before HomeSite can resolve relative links.", "Save file", hsOK + hsInformation);
  61.     }
  62.     else {    // we can calculate relative paths
  63.         var linkedFile = resolvedPath(strHREF);
  64.         var success = app.OpenFile(linkedFile);
  65.         if (!success) { app.MessageBox("Couldn't open file!", "Failure", hsOK + hsExclamation); }
  66.     }
  67. }
  68.  
  69. }    // end Main
  70.  
  71.  
  72.     
  73. function resolvedPath(strRelativePath) {
  74.     var strCurrFilePathSlash = app.ExtractFilePath(active.Filename);
  75.     var strCurrFilePath = strCurrFilePathSlash.slice(0, strCurrFilePathSlash.length-1);
  76.     var arrAbsDirs = strCurrFilePath.split("\\");
  77.     var arrRelDirs = strRelativePath.split("/");
  78.     var arrNewRelDirs = new Array();
  79.     var arrNewAbsDirs = new Array();
  80.     var relPathUp = "..";
  81.     var relFirstPart = arrRelDirs[0];
  82.     while (relFirstPart == relPathUp) {
  83.             arrNewAbsDirs = arrAbsDirs.slice(0, arrAbsDirs.length-1);
  84.             arrNewRelDirs = arrRelDirs.slice(1, arrRelDirs.length);
  85.             arrAbsDirs = arrNewAbsDirs;
  86.             arrRelDirs = arrNewRelDirs;
  87.             relFirstPart = arrRelDirs[0];
  88.     }    // end while
  89.     strCurrFilePath = arrAbsDirs.join("\\");
  90.     strRelativePath = arrRelDirs.join("\\");
  91.     var strResolvedPath = strCurrFilePath + "\\" + strRelativePath;
  92.     return strResolvedPath;
  93. }
  94.  
  95.