Macro to perform backup of files


I’m a new komodo user since I switched to linux and would like to have backup of files each time I’m saving a file.

I saw this old topic but even when replacing komodo.document by komodo.koDoc it stil does not work.

Here is my macro.
Can anyone help me to update it for running in Komodo 8 ?

Thanks :smiley:

if (komodo.koDoc.file.scheme != "file") {
var file = Components.classes[";1"]


var backupFile = Components.classes[";1"]

backupFile.initWithPath(komodo.koDoc.file.path + ".bak");

if (backupFile.exists()) {

file.copyTo(backupFile.parent, backupFile.leafName);

In Macro Preferences, in tab Triggers you can set trigger “After file save” to call your macro when you save your file (Captain obvious).
Also, I think “komodo.koDoc” is not right. Try to use ko.views.manager.currentView.koDoc or insert

komodo = ko.views.manager.currentView;

in top of your macro.
More info in Macro API Reference
I try to make this.

1 Like

You may be interested in this File Revisions macro I wrote ages ago -

Other relevant resources:

All of this not working :smile:
I’m almost finished my macro. Today I’m upload it.
But I don’t know, how to create file from macro.
In koIOs I can found mkdir, but can’t found “touch” or something to create file.

var os = Components.classes[";1"].getService(Components.interfaces.koIOs);

My bad, works normal.


var osPath = Components.classes[";1"].getService(Components.interfaces.koIOsPath);
var os = Components.classes[";1"].getService(Components.interfaces.koIOs);
var basename = ko.views.manager.currentView.koDoc.file.baseName;
var curdir = ko.views.manager.currentView.koDoc.file.dirName;
var scimoz =  ko.views.manager.currentView.scimoz;
var content = scimoz.text;
var date     = new Date();
var Year     = date.getFullYear();
var Month    = date.getMonth();
var Day      = date.getDate();
var Hour     = date.getHours();
var Minute   = date.getMinutes();
var Second   = date.getSeconds();
if(Month<10) Month   = "0" + Month;
if(Day<10) Day       = "0" + Day;
if(Hour<10) Hour     = "0" + Hour;
if(Minute<10) Minute = "0" + Minute;
if(Second<10) Second = "0" + Second;
var timestamp = Year+"-"+Month+"-"+Day+":"+Hour+"-"+Minute+"-"+Second;
var backup_fld = curdir+"_bak/";
var backup =backup_fld+basename+timestamp;

if (!osPath.exists(backup_fld)) {
    ko.statusBar.AddMessage('Create folder '+backup_fld, 'editor', 5000, true);
os.writefile(backup, content);
ko.statusBar.AddMessage('Save '+basename+" to "+backup_fld, 'editor', 5000, true);

All components, interfaces and available functions I watch here
In Macro preferences:

Thank you very much.

In the meantime, I figured out another way to do it. here it is :

//window.openDialog('chrome://global/content/console.xul', '_blank');

var doc = ko.views.manager.currentView.koDoc;
var bakname =  doc.displayPath + ".bak";

if (komodo.koDoc.file.scheme != "file") {

if (! doc.isDirty) {


var file = Components.classes[";1"].createInstance(Components.interfaces.nsILocalFile);
var backupFile = Components.classes[";1"].createInstance(Components.interfaces.nsILocalFile);
var dir = Components.classes[";1"].createInstance(Components.interfaces.nsILocalFile);


if (backupFile.exists()) {

file.copyTo(dir,  doc.baseName + ".bak");

// save current file
if (komodo.view) { komodo.view.setFocus(); }

Hmm, your macro uses components… In Komodo sources I don’t find this and use only Komodo components :smile:
And your macro just copy:

  1. My macro. (It’s not bad, but I think create macros with same functions is break “up to date” for macro).
    2. File that you want to backup. But what happens if you don’t have permissions to copy file that you want to backup? I think copy content of file (because to copy content you just must have permissions to read file) is more logical than copy file. Or not?
    My bad. To copy files just need have permissions to read files. Sorry.

Komodo is based on Mozilla, you absolutely can use their Components :slight_smile: Being Mozilla based is one of the major pros of Komodo.