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

Komodo JavaScript macro that shows a click-able list of all bookmarks in the Command Output tab.

JavaScript, 64 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Komodo macro - shows all bookmarks in the Command Output tab
function get_all_bookmarks() {
  var bm = [];
  var views = ko.views.manager.getAllViews('editor');
  var bmask = 1 << ko.markers.MARKNUM_BOOKMARK;
  views.map(function(v) {
      var s = v.scimoz;
      var i = 0;
      while (true) {
        var res = s.markerNext(i, bmask);
        if (res > -1) {
            bm.push([v, res]);
            i = res + 1;
        } else {
            break;
        }
      }
  });
  var data = (bm.length ? bm.map(function(x) x[0].koDoc.displayPath + ":" + (x[1]+1)).join("\n") : "No bookmarks");
  return data;
}
  
function write_to_file(data) {
  var tmpFile = Components.classes["@mozilla.org/file/directory_service;1"].
                     getService(Components.interfaces.nsIProperties).
                     get("TmpD", Components.interfaces.nsIFile);
  tmpFile.append("koBookmarks.tmp");
  
  var file = Components.classes["@mozilla.org/file/local;1"].
                       createInstance(Components.interfaces.nsILocalFile);
  file.initWithPath(tmpFile.path);
  var foStream = Components.classes["@mozilla.org/network/file-output-stream;1"].
                           createInstance(Components.interfaces.nsIFileOutputStream);
  foStream.init(file, 0x02 | 0x08 | 0x20, 0666, 0); 
  var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
                            createInstance(Components.interfaces.nsIConverterOutputStream);
  converter.init(foStream, "UTF-8", 0, 0);
  converter.writeString(data);
  converter.close();
  var dataFile = tmpFile.path;
  return dataFile;
}

function parse_list(dataFile) {
  var osString = Components.classes["@mozilla.org/xre/app-info;1"]  
               .getService(Components.interfaces.nsIXULRuntime).OS;
  if (osString == "WINNT") {
    var echoCmd = "type ";
  }
  else {
    var echoCmd = "cat ";
  }
  
  ko.run.runEncodedCommand(window, echoCmd + dataFile +
    ' {"parseRegex": u"^(?P\<file\>.+?):(?P\<line\>\\d+)(?P\<content\>)$", \
    "showParsedOutputList": True, \
    "parseOutput": True}');
}

try {
  parse_list(write_to_file(get_all_bookmarks()));
} catch(ex) {
    alert("get_all_bookmarks failed: " + ex + "\n");
}

It grabs the list of bookmarks, writes it to a file, then runs 'cat' or 'type' on the file, parses the output with a regex, and displays it as a click-able list in the Command Output tab. I didn't know how else to get the "parseOutput" behavior, but there's got to be a better way.

Thanks to EricP for the get_all_bookmarks() function.

4 comments

Michael 9 years, 9 months ago  # | flag

Doesn't work in Komodo 7

JACQ Fabien 8 years, 7 months ago  # | flag

On line 19, edit

var data = (bm.length ? bm.map(function(x) x[0].document.displayPath + ":" + (x[1]+1)).join("\n") : "No bookmarks");

to

var data = (bm.length ? bm.map(function(x) x[0].koDoc.displayPath + ":" + (x[1]+1)).join("\n") : "No bookmarks");

and remove line 59.

It should work now

JACQ Fabien 8 years, 7 months ago  # | flag

Sorry, It was line 58 ...

Troy Topnik (author) 8 years, 7 months ago  # | flag

Edited and tested in Komodo 8. Thanks JACQ Fabien!

Created by Troy Topnik on Tue, 22 Jun 2010 (MIT)
JavaScript recipes (69)
Troy Topnik's recipes (2)
Komodo Macros (15)

Required Modules

Other Information and Tasks