Skip to content

Instantly share code, notes, and snippets.

@scripting
Created September 28, 2017 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scripting/b5212bd2062a089af403894f944700c7 to your computer and use it in GitHub Desktop.
Save scripting/b5212bd2062a089af403894f944700c7 to your computer and use it in GitHub Desktop.
function runUserScript (s, scriptName) {
var now = new Date ();
function msg (s) {
var package = {
text: s,
path: scriptName
};
bullMancuso.send ("callback", "msg", utils.jsonStringify (package));
}
function persist (objectName) {
if (localStorage [objectName] === undefined) {
localStorage [objectName] = new Object ();
}
return (localStorage [objectName]);
}
var number = {
random: function (lower, upper) {
return (utils.random (lower, upper));
}
}
var file = {
readWholeFile: function (path) {
return (fs.readFileSync (path).toString ());
},
writeWholeFile: function (path, data) {
return (fs.writeFileSync (path, data));
},
exists: function (path) {
return (fs.existsSync (path));
},
getDatePath: function (theDate, flLastSeparator) {
return (utils.getDatePath (theDate, flLastSeparator));
},
sureFilePath: function (path) {
return (utils.sureFilePathSync (path));
},
copy: function (source, dest) {
return (file.writeWholeFile (dest, file.readWholeFile (source)));
},
isFolder: function (path) {
return (fs.statSync (path).isDirectory ());
},
getFileList: function (folderpath) {
return (fs.readdirSync (folderpath));
},
getPathChar: function () {
return ("/"); //must be made cross-platform -- 2/17/17 by DW
},
fileFromPath: function (f) {
return (string.lastField (f, file.getPathChar ()));
},
newFolder: function (path) {
return (fs.mkdirSync (path));
},
folderFromPath: function (f) {
var pc = file.getPathChar ();
return (string.popLastField (f, pc) + pc);
},
getUserDataFolder: function () {
return (appConsts.userDataFolder + "/");
}
getCurrentFilePath: function () {
var tab = appPrefs.myTabs [appPrefs.ixCurrentTab];
return (appConsts.outlinesPath + tab.fname);
},
getCurrentFileTitle: function () {
var tab = appPrefs.myTabs [appPrefs.ixCurrentTab];
return (tab.name);
},
getCurrentFileInfo: function () { //1/15/17 by DW
return (appPrefs.myTabs [appPrefs.ixCurrentTab]);
},
viewCurrentFileInReader: function () { //7/28/13 by DW
file.makeFileViewable (function (theUrl) {
if (theUrl === undefined) {
dialog.alert ("Can't open the file in the reader, possibly because it's not public.");
}
else {
webBrowser.openUrl (theUrl);
}
})
openInTacoPie ();
},
saveCurrentFileAsMarkdown: function () { //7/28/13 by DW
fileSaveAsMarkdownCommand ();
},
makeFilePublic: function (callback) { //1/15/17 by DW
makeFilePublic (callback);
},
getPublicUrl: function () { //1/17/17 by DW
var theTab = appPrefs.myTabs [appPrefs.ixCurrentTab];
return (theTab.urlPublic);
},
makeFileViewable: function (callback) { //1/15/17 by DW
makeFileViewable (callback);
},
getFileList: function (callback) { //1/16/17 by DW
twGetUserFiles (true, function (privateFiles) {
twGetUserFiles (false, function (publicFiles) {
function addFiles (whichFiles) {
for (var i = 0; i < whichFiles.length; i++) {
var theFile = whichFiles [i];
theFiles [theFiles.length] = theFile.path;
}
}
var theFiles = new Array ();
addFiles (publicFiles);
addFiles (privateFiles);
if (callback !== undefined) {
callback (theFiles);
}
});
});
}
}
var http = {
readUrl: function (url, callback) {
request (url, function (err, response, body) {
if (callback !== undefined) {
if (err) {
callback (undefined);
}
else {
callback (body.toString ());
}
}
});
}
}
var s3 = {
newObject: function (path, text, type, callback) {
var s = text.toString ();
s3Lib.newObject (path, s, type, undefined, function (err, data) {
if (callback !== undefined) {
callback (err, data);
}
});
},
getObject: function (path, callback) {
s3Lib.getObject (path, function (err, data) {
if (callback !== undefined) {
if (err) {
callback (undefined, err);
}
else {
callback (data);
}
}
});
}
}
var string = {
beginsWith: utils.beginsWith,
contains: utils.stringContains,
countFields: utils.stringCountFields,
dayOfWeekToString: utils.dayOfWeekToString,
delete: utils.stringDelete,
endsWith: utils.endsWith,
filledString: utils.filledString,
getRandomPassword: utils.getRandomPassword,
getRandomSnarkySlogan: utils.getRandomSnarkySlogan,
hashMD5: function (s) {
return (crypto.createHash ("md5").update (s).digest ("hex"));
},
innerCaseName: utils.innerCaseName,
insert: function (source, dest, ix) {
ix--; //our version is 1-based
return (dest.substr (0, ix) + source + dest.substr (ix));
},
isAlpha: utils.isAlpha,
isNumeric: utils.isNumeric,
lastField: utils.stringLastField,
lower: utils.stringLower,
mid: utils.stringMid,
monthToString: utils.monthToString, //January, February etc.
multipleReplaceAll: utils.multipleReplaceAll, //1/18/17 by DW
nthField: utils.stringNthField,
padWithZeros: utils.padWithZeros,
popExtension: utils.stringPopExtension, //1/18/17 by DW
popLastField: utils.stringPopLastField,
popTrailing: function (s, ch) { //11/25/13 by DW
while (s.length > 0) {
if (s [s.length - 1] != ch) {
break;
}
s = string.delete (s, s.length, 1);
}
return (s);
},
replaceAll: utils.replaceAll,
stripMarkup: utils.stripMarkup,
trimLeading: function (s, ch) {
if (ch == undefined) {
ch = " ";
}
return (utils.trimLeading (s, ch));
},
trimTrailing: function (s, ch) {
if (ch == undefined) {
ch = " ";
}
return (utils.trimTrailing (s, ch));
},
trimWhitespace: utils.trimWhitespace,
upper: utils.stringUpper
}
var dialog = {
alert: function (s) {
var package = {
text: s,
path: scriptName
};
bullMancuso.send ("callback", "alertDialog", utils.jsonStringify (package));
},
ask: function (prompt, defaultValue, placeholder, callback) {
bullMancuso.send ("askDialog", prompt, defaultValue, placeholder, callback);
return (askDialog (prompt, defaultValue, placeholder, callback));
}
viewText: function (prompt, s) {
showViewTextDialog (prompt, s);
},
confirm: function (prompt, callback) {
confirmDialog (prompt, callback);
},
about: function (urlOpml) {
aboutDialogShow (urlOpml);
}
}
var date = {
netStandardString: function (theDate) { //12/17/13 by DW
return (theDate.toUTCString ());
},
secondsSince: utils.secondsSince
}
var clock = {
now: function () {
return (new Date ());
},
waitSeconds: function (ctsecs) {
return (sleep (ctsecs * 1000));
var ctloops = 0;
for (var whenStart = new Date (); utils.secondsSince (whenStart) < ctsecs; ctloops) {
debugMessage (utils.secondsSince (whenStart));
ctloops++;
}
return (ctloops);
}
}
var speaker = {
beep: function () {
debugMessage ("runUserScript: speaker.beep ()");
bullMancuso.send ("callback", "speakerBeep", utils.jsonStringify ({}));
}
}
var webBrowser = {
openUrl: function (url) {
electron.shell.openExternal (url); //5/10/17 by DW
}
}
var twitter = {
getMyName: function (callback) {
return (twitterLib.getScreenName (callback));
},
tweet: function (theTweet, callback) {
return (twitterLib.tweet (theTweet, undefined, debugMessage, callback));
}
getUserTweets: function (userId, callback) {
var idLastSeen = undefined;
twGetUserInfo (userId, function (userinfo) {
twGetUserTweets (userinfo.id_str, idLastSeen, function (theTweets) {
if (callback !== undefined) {
callback (theTweets);
}
});
});
},
post: function (status, callback) {
twTweet (status, 0, function (data) {
if (callback !== undefined) {
callback (data);
}
});
}
}
var fargo = { //11/29/13 by DW
version: function () {
return (appConsts.version);
},
productname: function () {
return (appConsts.productname);
},
productnameForDisplay: function () {
return (appConsts.productnameForDisplay);
}
}
var cms = {
version: function () {
return (cmsVersion);
},
renderPage: function (path) {
var tab = getActiveTab ();
var xstruct = getTabXstruct (tab);
return (cmsRenderPage (tab, xstruct, path));
}
}
var wordPress = {
updatePost: function (title, msgbody, idpost, callback, wpBlogUrl, wpUsername, wpPassword) {
var s;
if (wpBlogUrl == undefined) {
wpBlogUrl = appPrefs.wordpressBlogUrl;
}
if (wpUsername == undefined) {
wpUsername = appPrefs.wordpressUsername;
}
if (wpPassword == undefined) {
wpPassword = appPrefs.wordpressPassword;
}
if (idpost == undefined) {
idpost = 0;
}
if (msgbody == undefined) {
msgbody = "";
}
s = "postBody=" + encodeURIComponent (msgbody) + "&weblogUrl=" + encodeURIComponent (wpBlogUrl) + "&idPost=" + encodeURIComponent (idpost) + "&postTitle=" + encodeURIComponent (title) + "&blogUsername=" + encodeURIComponent (wpUsername) + "&blogPassword=" + encodeURIComponent (wpPassword);
debugMessage ("wordPress.updatePost: " + s);
var jxhr = $.ajax ({
url: updateBlogpostUrl,
data: s,
type: "POST",
dataType: "jsonp",
timeout: 30000,
jsonpCallback : "getData"
})
.success (function (data, status) {
if (callback != undefined) {
callback (data);
}
})
.error (function (status) {
});
},
postCursor: function (callback) {
var idpost = op.attributes.getOne ("idpost"), bodytext = "";
if (idpost == undefined) {
idpost = 0;
}
op.visitSubs ( //get bodytext
function (headline, levelnum) {
var pretext = "<li>", posttext = "</li>";
if (levelnum == 0) {
pretext = "<p>";
posttext = "</p>";
}
bodytext += string.filledString ("\t", levelnum) + pretext + headline.getLineText () + posttext + "\r\n";
},
function (levelnum) {
bodytext += string.filledString ("\t", levelnum) + "<ul>\r\n";
},
function (levelnum) {
bodytext += string.filledString ("\t", levelnum + 1) + "</ul>\r\n";
}
);
wordPress.updatePost (op.getLineText (), bodytext, idpost, function (data) {
if (data.error != undefined) {
speaker.beep ();
dialog.alert (data.error);
return;
}
if ((op.attributes.getOne ("idpost") == undefined) && (data.idpost != undefined)) {
op.attributes.setOne ("idpost", data.idpost);
}
if ((op.attributes.getOne ("url") == undefined) && (data.link != undefined)) {
op.attributes.setOne ("url", data.link);
}
if (op.attributes.getOne ("type") != "metaWeblogPost") {
op.attributes.setOne ("type", "metaWeblogPost");
}
//bump ctsaves att on blogpost headline
var ctsaves = op.attributes.getOne ("ctSaves");
if (ctsaves == undefined) {
ctsaves = 0;
}
op.attributes.setOne ("ctSaves", ++ctsaves);
if (callback != undefined) {
callback (data);
}
});
}
}
var op = {
expand: function () {
var package = {
verb: "expand",
ctLevels: 1
};
bullMancuso.send ("op", utils.jsonStringify (package));
return (true);
},
expandAllLevels: function () {
return ($(opGetActiveOutliner ()).concord ().op.expandAllLevels ());
},
expandEverything: function () {
return ($(opGetActiveOutliner ()).concord ().op.fullExpand ());
},
expandTo: function (headline) {
expandToCursor (headline);
return (setCursorActive (headline.getCursor ()));
},
collapse: function () {
return ($(opGetActiveOutliner ()).concord ().op.collapse ());
},
collapseEverything: function () {
return ($(opGetActiveOutliner ()).concord ().op.fullCollapse ());
},
go: function (dir, ct) {
if (dir == right) {
op.expand ();
}
return ($(opGetActiveOutliner ()).concord ().op.go (dir, ct));
},
firstSummit: function () {
opFirstSummit ();
return (true);
},
countSubs: function () {
return ($(opGetActiveOutliner ()).concord().op.countSubs ());
},
hasSubs: function () {
return (op.countSubs () > 0);
},
getLineText: function () {
return ($(opGetActiveOutliner ()).concord ().op.getLineText ());
},
setLineText: function (s) { //8/7/13 by DW
return ($(opGetActiveOutliner ()).concord ().op.setLineText (s));
},
insert: function (s, direction) {
return ($(opGetActiveOutliner ()).concord ().op.insert (s, direction));
},
reorg: function (dir, ct) {
if (ct == undefined) {
ct = 1;
}
return ($(opGetActiveOutliner ()).concord ().op.reorg (dir, ct));
},
promote: function () {
return ($(opGetActiveOutliner ()).concord ().op.promote ());
},
demote: function () {
return ($(opGetActiveOutliner ()).concord ().op.demote ());
},
deleteSubs: function () {
return ($(opGetActiveOutliner ()).concord ().op.deleteSubs ());
},
getCursorOpml: function () {
return ($(opGetActiveOutliner ()).concord ().op.cursorToXml ());
},
insertOpml: function (opmltext, dir) {
if (dir == undefined) {
dir = down;
}
return ($(opGetActiveOutliner ()).concord ().op.insertXml (opmltext, dir));
},
bold: function () {
return ($(opGetActiveOutliner ()).concord ().op.bold ());
},
italic: function () {
return ($(opGetActiveOutliner ()).concord ().op.italic ());
},
strikethrough: function () {
return ($(opGetActiveOutliner ()).concord ().op.strikethrough ());
},
link: function () {
return ($(opGetActiveOutliner ()).concord ().op.link ());
},
isComment: function () {
var isComment = op.attributes.getOne ("isComment")
if ((isComment == undefined) || (isComment == "false")) {
return (false);
}
else {
return (true);
}
return ($(opGetActiveOutliner ()).concord ().script.isComment ());
},
unComment: function () {
op.attributes.deleteOne ("isComment");
return ($(opGetActiveOutliner ()).concord ().script.unComment ());
},
makeComment: function () {
op.attributes.setOne ("isComment", "true");
return ($(opGetActiveOutliner ()).concord ().script.makeComment ());
},
toggleComment: function () {
if (op.isComment ()) {
op.unComment ();
}
else {
op.makeComment ();
}
},
setRenderMode: function (flrendermode) { //7/28/13 by DW
$(opGetActiveOutliner ()).concord ().op.setRenderMode (flrendermode);
},
getRenderMode: function () { //7/28/13 by DW
return ($(opGetActiveOutliner ()).concord ().op.getRenderMode ());
},
toggleRenderMode: function () { //7/28/13 by DW
op.setRenderMode (!op.getRenderMode ());
},
getCursor: function () {
return ($(opGetActiveOutliner ()).concord ().op.getCursorRef ());
},
getCursorUrl: function () {
var parent = undefined;
op.visitToSummit (function (headline) {
var type = headline.attributes.getOne ("type");
if (type != undefined) {
parent = headline;
return (false);
}
return (true);
});
return (getTrexUrl (opGetActiveOutliner (), parent, true));
},
runSelection: function () {
var value = eval (op.getLineText ());
op.deleteSubs ();
op.insert (value, right);
op.go (left, 1);
},
setModified: function () {
return ($(opGetActiveOutliner ()).concord ().op.markChanged ());
},
getModified: function () {
return ($(opGetActiveOutliner ()).concord ().op.changed ());
},
setTextMode: function (fltextmode) {
$(opGetActiveOutliner ()).concord ().op.setTextMode (fltextmode);
},
visitSubs: function (lineCallback, indentCallback, outdentCallback) {
var levelnum = 0;
var visitSub = function (sub) {
lineCallback (sub, levelnum);
if (sub.countSubs () > 0) {
if (indentCallback != undefined) {
indentCallback (levelnum);
}
levelnum++;
sub.visitLevel (visitSub);
levelnum--;
if (outdentCallback != undefined) {
outdentCallback (levelnum);
}
}
return (true);
};
op.getCursor ().visitLevel (visitSub);
},
visitAll: function (callback) {
return ($(opGetActiveOutliner ()).concord ().op.visitAll (callback));
},
visitToSummit: function (callback) {
return ($(opGetActiveOutliner ()).concord ().op.visitToSummit (callback));
},
attributes: {
getAll: function () {
return ($(opGetActiveOutliner ()).concord ().op.attributes.getAll ());
},
getOne: function (name) {
return $(opGetActiveOutliner ()).concord ().op.attributes.getOne (name);
},
setOne: function (name, value) {
return $(opGetActiveOutliner ()).concord ().op.attributes.setOne (name, value);
},
addGroup: function (atts) {
return $(opGetActiveOutliner ()).concord ().op.attributes.setGroup (atts);
},
deleteOne: function (name) {
var atts = op.attributes.getAll ();
if (atts [name] != undefined) {
delete atts [name];
}
op.attributes.addGroup (atts);
},
makeEmpty: function () {
var atts = new Object ();
op.attributes.addGroup (atts);
}
}
}
var script = {
isComment: function () {
var isComment = op.attributes.getOne ("isComment")
if ((isComment == undefined) || (isComment == "false")) {
return (false);
}
else {
return (true);
}
return ($(opGetActiveOutliner ()).concord ().script.isComment ());
},
unComment: function () {
op.attributes.deleteOne ("isComment");
return ($(opGetActiveOutliner ()).concord ().script.unComment ());
},
makeComment: function () {
op.attributes.setOne ("isComment", "true");
return ($(opGetActiveOutliner ()).concord ().script.makeComment ());
},
toggleComment: function () {
if (script.isComment ()) {
script.unComment ();
}
else {
script.makeComment ();
}
},
}
stats.ctScriptRuns++;
stats.whenLastScriptRun = now;
statsChanged ();
with (localStorage) {
return (eval (s));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment