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.
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);
}
|
This is huge. Should be built in.
Doesn't seem to work in Komodo 7. Displays error msg.
@Michael - right, it needed an update for Komodo 7 (as currentView.document is now currentView.koDoc in Komodo 7).
Todd, many thanks for updating it. I reverted to KO6 when this didn't work and now just re-installed 7!
yes, thanks for the update, working in Komodo Edit 7
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="" />
@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
@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.
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