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

Komodo projects look like file managers, but don't always offer the same functionality directly. Here's a macro that makes a copy of the current selected file in a project. To use it, create a new macro in the Komodo toolbox, copy this code, paste it in the macro, and save it. Then you can select a file, double-click the macro icon (or assign it to a keybinding), and Komodo will create a copy of the file called "Copy of <basename>.<extension>".

JavaScript, 33 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
try {
    var pv = document.getElementById('projectview');
    var part = pv.getSelectedItem();
    if (!part) {
        ko.dialogs.alert("No file selected");
        return;
    } else if (part.type != 'file') {
        ko.dialogs.alert("Only files can be copied.  Current item is: " +
                         part.type);
        return;
    }
    var osPathSvc = Components.classes["@activestate.com/koOsPath;1"].getService(Components.interfaces.koIOsPath);
    var fileObj = part.getFile();
    var dirName = fileObj.dirName;
    var baseName = fileObj.baseName;
    var copyPath = osPathSvc.join(dirName, "Copy of " + baseName);
    if (osPathSvc.exists(copyPath)) {
        ko.dialogs.alert("File '" + copyPath + "' already exists, not copying");
        return;
    }
    var shUtilSvc = Components.classes["@activestate.com/koShUtil;1"].getService(Components.interfaces.koIShUtil);
    shUtilSvc.copyfile(fileObj.path, copyPath);
    var p = ko.projects.manager.getSelectedProject();
    if (!p.live) {
        ko.dialogs.alert("Project "
                         + p.name
                         + " isn't live, so you'll have to manually add the copied file");
    } else {
        ko.projects.manager.refreshView();
    }
} catch(ex) {
    ko.dialogs.alert("Error: " + ex);
}

This works with files only, not directories and other project parts. It also points out that new files in non-live projects have to be explicitly added.

To use this macro, create a new macro in your toolbox (I creatively call mine "duplicate file"), paste this code into the macro, select a file, and run the macro.

2 comments

Andy Wright 13 years, 2 months ago  # | flag

Doesn't seem to work in Komodo IDE 6

Created by Eric Promislow on Thu, 4 Mar 2010 (MIT)
JavaScript recipes (69)
Eric Promislow's recipes (11)
Komodo Macros (15)

Required Modules

  • (none specified)

Other Information and Tasks