Stonos (owner)

Revisions

gist: 39932 Download_button fork
public
Description:
The purpose of this command is to quickly save the inputted text (can be selected text or your own text) to a file on your disk without having to launch a text editor. For more information, please visit http://mike.thedt.net/ubiquity/note/note.php .
Public Clone URL: git://gist.github.com/39932.git
Embed All Files: show embed
x #
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Version 1.2
 
// Uses code from:
// http://www.captain.at/programming/xul
// https://developer.mozilla.org/en/Writing_textual_data
// http://maloki.net/projects/pingfm-ubiquity
 
notesConf = {
    addLocation:function(location){
        if (!Application.prefs.has("notes.location")) {
            Application.prefs.setValue("notes.location", location);
        } else {
            var new_key = Application.prefs.get("notes.location");
            new_key.value = location;
            return new_key.value;
        }
    },
  
    addEncoding:function(encoding){
        if (!Application.prefs.has("notes.encoding")) {
            Application.prefs.setValue("notes.encoding", encoding);
        } else {
            var new_key = Application.prefs.get("notes.encoding");
            new_key.value = encoding;
            return new_key.value;
        }
    },
  
    getLocation:function(){
        location = Application.prefs.get("notes.location");
        if(location){
            location=location.value
            return location;
        } else {
   location="C:\\Notes\\";
            return "C:\\Notes\\";
        }
    },
  
    getEncoding:function(){
        encoding = Application.prefs.get("notes.encoding");
        if(encoding){
            return encoding.value;
        } else {
   charset="UTF-8";
            return "UTF-8";
        }
    },
  
};
 
var location = notesConf.getLocation();
var charset = notesConf.getEncoding(); // Can be any character encoding name that Mozilla supports
 
function save(WhatToSave,WhereToSave) {
    try {
        netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
    } catch (e) {
    }
    var file = Components.classes["@mozilla.org/file/local;1"]
        .createInstance(Components.interfaces.nsILocalFile);
    file.initWithPath( WhereToSave );
    if ( file.exists() == false ) {
        file.create( Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420 );
    }
    var outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
  .createInstance( Components.interfaces.nsIFileOutputStream );
    outputStream.init( file, 0x04 | 0x08 | 0x10, 420, 0 );
 
    var os = Components.classes["@mozilla.org/intl/converter-output-stream;1"]
       .createInstance(Components.interfaces.nsIConverterOutputStream);
 
    os.init(outputStream, charset, 0, 0x0000);
    os.writeString( WhatToSave + "\r\n\r\n");
    os.close();
    outputStream.close();
}
 
//note
CmdUtils.CreateCommand({
    name: "note",
    icon: "http://mike.thedt.net/ubiquity/note/Notes-16x16.png",
homepage: "http://mike.thedt.net/",
    author: { name: "Stonos", email: "stonos@gmail.com"},
    license: "MPL",
    description: "Saves inputted text",
    help: "Select the text you want to save and execute note",
    takes: {"input": noun_arb_text},
    preview: function( pblock, input ) {
        var template = "Saves <b>${selected}</b> to <b>${location}${title}.txt</b>";
        pblock.innerHTML = CmdUtils.renderTemplate(template, {"selected": input.html,"location": notesConf.getLocation(), "title": StripIllegalChars(CmdUtils.getDocument().title)});
    },
    execute: function(input) {
    save(input.text,location+StripIllegalChars(CmdUtils.getDocument().title)+".txt");
    displayMessage("Saved note!");
}
  });
 
//notes-encoding
CmdUtils.CreateCommand({
    name: "notes-encoding",
    icon: "http://mike.thedt.net/ubiquity/note/Notes-16x16.png",
    homepage: "http://mike.thedt.net/",
    author: { name: "Stonos", email: "stonos@gmail.com"},
    license: "MPL",
    description: "Changes the default encoding for saving notes",
    help: "Can be any character encoding name that Mozilla supports",
    takes: {"input": noun_arb_text},
    preview: function( pblock, input ) {
        var template = "Changes encoding from <b>${oldEnc}</b> to <b>${newEnc}</b>";
        pblock.innerHTML = CmdUtils.renderTemplate(template, {"oldEnc": charset, "newEnc": input.text});
    },
    execute: function(input) {
        notesConf.addEncoding(input.text);
        charset=input.text;
    }
});
 
//notes-location
CmdUtils.CreateCommand({
    name: "notes-location",
    icon: "http://mike.thedt.net/ubiquity/note/Notes-16x16.png",
    homepage: "http://mike.thedt.net/",
    author: { name: "Stonos", email: "stonos@gmail.com"},
    license: "MPL",
    description: "Changes the default location for saving notes",
    help: "Use double backslashes on the location!",
    takes: {"input": noun_arb_text},
    preview: function( pblock, input ) {
        var template = "Changes location from <b>${oldLoc}</b> to <b>${newLoc}</b>";
        pblock.innerHTML = CmdUtils.renderTemplate(template, {"oldLoc": location, "newLoc": input.text});
    },
    execute: function(input) {
        notesConf.addLocation(input.text);
        location=input.text;
    }
});
 
//notes-view
CmdUtils.CreateCommand({
    name: "notes-view",
    icon: "http://mike.thedt.net/ubiquity/note/Notes-16x16.png",
    homepage: "http://mike.thedt.net/",
    author: { name: "Stonos", email: "stonos@gmail.com"},
    license: "MPL",
    description: "View taken notes for this website.",
    help: "Type notes-view.",
    preview: "View notes for this website",
    execute: function(input) {
        Utils.openUrlInBrowser(location+StripIllegalChars(CmdUtils.getDocument().title)+".txt");
    }
});
      
function StripIllegalChars(str)
{
  return str.replace(/[\\/:\*?<>\|]/gi, "");
}