Macro to perform backup of files

Hello,

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 http://community.activestate.com/forum-topic/does-komodo-perform-backu 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") {
    return;
}
var file = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);

file.initWithPath(komodo.koDoc.file.path);

var backupFile = Components.classes["@mozilla.org/file/local;1"]
                     .createInstance(Components.interfaces.nsILocalFile);

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

if (backupFile.exists()) {
    backupFile.remove(false);
}

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 - https://community.activestate.com/forum/file-revisions-diff

Other relevant resources:

http://community.activestate.com/forum/auto-backup-macro
http://community.activestate.com/forum/auto-backup-macro#comment-9333
http://community.activestate.com/node/10062

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["@activestate.com/koOs;1"].getService(Components.interfaces.koIOs);

My bad, works normal.

Created!!!

var osPath = Components.classes["@activestate.com/koOsPath;1"].getService(Components.interfaces.koIOsPath);
var os = Components.classes["@activestate.com/koOs;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)) {
    os.mkdir(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:
http://www.zimagez.com/zimage/-27062014-215316.php

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") {
  return;
}

if (! doc.isDirty) {
  return;
}

//print(doc.displayPath);
//print(doc.baseName);
//print(doc.file.dirName);

var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
var backupFile = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);
var dir = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);

file.initWithPath(doc.displayPath);
backupFile.initWithPath(bakname);
dir.initWithPath(doc.file.dirName);

if (backupFile.exists()) {
    backupFile.remove(false);
}

// https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIFile#copyTo%28%29
file.copyTo(dir,  doc.baseName + ".bak");


// save current file
if (komodo.view) { komodo.view.setFocus(); }
ko.commands.doCommand('cmd_save')

Hmm, your macro uses @mozilla.org 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.