Welcome, guest | Sign In | My Account | Store | Cart

A Komodo JavaScript macro that can be used to generate a relative path from the current editor file location, which works great for HTML when linking to relative style sheets, images, etc...

Upon execution of this macro, you will be prompted to browse and select the target file, then upon selecting the target path, the relative path location will be inserted into the editor at the current position.

To best use this macro - assign a custom key-binding to the macro and then trigger the macro at will.

JavaScript, 20 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function relativePath(fromPath, toPath)
{
    var nsFileFrom = Components.classes["@mozilla.org/file/local;1"]
                          .createInstance(Components.interfaces.nsILocalFile);
    nsFileFrom.initWithPath(fromPath);
    var nsFileTo = Components.classes["@mozilla.org/file/local;1"]
                          .createInstance(Components.interfaces.nsILocalFile);
    nsFileTo.initWithPath(toPath);
    return nsFileTo.getRelativeDescriptor(nsFileFrom);
}

var currentView = ko.views.manager.currentView;
var doc = currentView.koDoc || currentView.document;  // Support both K6- and K7+
var cwd = doc.file.dirName;
var path = ko.filepicker.openFile(cwd);
if (path) {
    var relpath = relativePath(cwd, path);
    var editor = currentView.scimoz;
    editor.insertText(editor.currentPos, relpath);
}

9 comments

Skye Giordano 10 years ago  # | flag

This is huge. Should be built in.

Michael 9 years, 9 months ago  # | flag

Doesn't seem to work in Komodo 7. Displays error msg.

Todd Whiteman (author) 9 years, 9 months ago  # | flag

@Michael - right, it needed an update for Komodo 7 (as currentView.document is now currentView.koDoc in Komodo 7).

Skye Giordano 9 years, 9 months ago  # | flag

Todd, many thanks for updating it. I reverted to KO6 when this didn't work and now just re-installed 7!

Mike 9 years, 8 months ago  # | flag

yes, thanks for the update, working in Komodo Edit 7

Peter Weil 9 years, 8 months ago  # | flag

This macro which is based on Todd's, inserts an image tag with a presumed file path of Site_Root/images/, and also inserts and fills in the height and width attributes. It's no thing of beauty, and could probably be done a lot better, but it works (for KO7). The resulting output looks this this:

<img src="/images/nav_bg.jpg" width="10" height="25" alt="" />

function relativePath(fromPath, toPath)
{
var nsFileFrom = Components.classes["@mozilla.org/file/local;1"]
                      .createInstance(Components.interfaces.nsILocalFile);
nsFileFrom.initWithPath(fromPath);
var nsFileTo = Components.classes["@mozilla.org/file/local;1"]
                      .createInstance(Components.interfaces.nsILocalFile);
nsFileTo.initWithPath(toPath);
return nsFileTo.getRelativeDescriptor(nsFileFrom);
}
var cwd = ko.views.manager.currentView.koDoc.file.dirName;
var path = ko.filepicker.openFile(cwd);

var relpath = relativePath(cwd, path);
var relpath2 = relpath.split('../').join('');
var editor = ko.views.manager.currentView.scimoz;

var urlPrefix = 'file://';

var img = new Image();
img.src =  urlPrefix + path;
var i = 0;
var lim = 10;

var writeTag = function(i) {

if (i >= lim) {
       ko.dialogs.alert("Sorry, can't find the height and width of the image");
    } else if (!img.height || !img.width) {
        ko.statusBar.AddMessage("No img info at attempt " + i, "editor", 500, true);
       setTimeout(writeTag, 100, i + 1);
 } else {

editor.insertText(editor.currentPos, '<img src="/' + relpath2 + '" width="' + img.width + '"' + ' height="' + img.height +'"' + ' alt="" />' );

    }
};
writeTag(0);
ludo 9 years, 8 months ago  # | flag

@peter well >> how to install this macro because I have an error message :

on the dialog I create a new marco and copy this code but when I ru it I have a message like that : (imgsearch is the name of the macro)

Error while running in macro imgsearch:

TypeError: ko.views.manager.currentView.koDoc.file is null

exception[fileName] = chrome://komodo/content/project/peMacro.js exception[lineNumber] = 730

thanks

Peter Weil 9 years, 8 months ago  # | flag

@ludo

I'm not sure what the problem is, to be honest. Do you have a file open and is the cursor someplace within the editing window when you run the macro? That section of the script is just getting your current working directory.

Dave Stewart 9 years, 6 months ago  # | flag

Check the latest AutoCode extension:

http://community.activestate.com/node/7280/

It allows you to ALT+Click files in the Places panel and will insert not just the path or URL, but additional custom code (as defined in Snippets) if you like, and is highly customizable per file type and language.

More info here:

www.xjsfl.com/support/setup/komodo/autocode

Cheers, Dave

Created by Todd Whiteman on Wed, 14 Jul 2010 (MIT)
JavaScript recipes (69)
Todd Whiteman's recipes (13)
Komodo Macros (15)

Required Modules

  • (none specified)

Other Information and Tasks