Last active
May 11, 2024 05:46
-
-
Save RobTrew/10586ab15dddaca21881 to your computer and use it in GitHub Desktop.
OS X 10.10 (Yosemite) Script Editor - Wrap selected line(s) in simple timing code (for performance experiments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ROBIN TREW 2015 MIT License | |
// OSX 10.10 SCRIPT EDITOR – WRAP SELECTED LINE(S) IN SIMPLE TIMING CODE (FOR PERFORMANCE EXPERIMENTS) | |
// Add remove '// ' from before printing characters | |
// Depends on a library .scpt being saved at ~/Library/Script Libraries/DraftsSE.scpt | |
// See: See: http://support.foldingtext.com/t/writing-scripts-which-run-on-both-foldingtext-and-ios-drafts-4/652/7 | |
// INCLUDE LIBRARY OF IOS DRAFTS-COMPATIBLE EDITOR FUNCTIONS: | |
function run() { | |
// PART 1: CODE SPECIFIC TO YOSEMITE SCRIPT EDITOR: | |
// SE implmentations of the basic iOS Drafts functions | |
// (Implementations of these functions for FoldingText and other editors with .js scripting at: | |
// | |
// getText() getSelectedLineRange() | |
// setText(string) getSelectedRange() | |
// getSelectedText() setSelectedRange(start, length) | |
// setSelectedText(string) getClipboard() | |
// getTextInRange(start, length) setClipboard(string) | |
// setTextInRange(start, length, string) | |
var app = Application("Script Editor"), | |
lstDoc = app.documents(), | |
oDoc = lstDoc.length ? lstDoc[0] : null; | |
if (!oDoc) return false; | |
//either var drafts = Library('DraftsSE'); // Assuming file saved at: ~/Library/Script Libraries/DraftsSE.scpt | |
// or locally: | |
drafts = { | |
getTextInRange: function (iStart, iLength) { | |
if (oDoc) return oDoc.text().substring(iStart, iStart + iLength); | |
}, | |
setSelectedRange: function (iStart, iLength) { | |
if (oDoc) { | |
oDoc.selection = oDoc.text.characters.slice(iStart, iStart + iLength); | |
} | |
}, | |
getSelectedLineRange: function () { | |
// works, but let me know if you see a shorter route :-) | |
var rgxLFCR=/[\r\n]/, dct, strFull, iFrom, iTo, lngChars; | |
if (oDoc) { | |
dct = oDoc.selection.characterRange(); | |
strFull = oDoc.text(); | |
lngChars = strFull.length; | |
iFrom = dct.x; | |
while (iFrom--) | |
if (strFull[iFrom].match(rgxLFCR)) break; | |
iFrom += 1; | |
iTo = dct.y - 1; | |
while (iTo++ < lngChars) | |
if (strFull[iTo].match(rgxLFCR)) break; | |
return [iFrom, (iTo - iFrom)]; | |
} | |
}, | |
setTextInRange: function (iStart, iLength, strText) { | |
if (oDoc) { // record the existing selection coordinates | |
var lngDelta = (strText.length - iLength) - 2, | |
oSeln = oDoc.selection, | |
dct = oSeln.characterRange(), | |
iFrom = dct.x, | |
iTo = dct.y; | |
try { // use the selection to edit elsewhere, then restore with any adjustment | |
oDoc.selection = oDoc.text.characters.slice(iStart, iStart + iLength); | |
oSeln.contents = strText; | |
oDoc.selection = oDoc.text.characters.slice( | |
(iStart < iFrom) ? iFrom + lngDelta : iFrom, ((iStart + iLength) < iTo) ? iTo + lngDelta : iTo | |
); | |
} catch (e) {} | |
} | |
} | |
}; | |
// PART 2: GENERIC CODE | |
var lstSeln = drafts.getSelectedLineRange(), | |
iFrom = lstSeln[0], | |
lngSelnChars = lstSeln[1], | |
strCode = drafts.getTextInRange(iFrom, lngSelnChars), | |
lstPre = ["var tmStart, tmFinish, i=10; // ADD ZEROS HERE...", | |
"tmStart = new Date().getTime();", "while (i--) {"], | |
lstPost = ["}", "tmFinish = new Date().getTime();", "return [tmFinish-tmStart];"], | |
strWrapped = '\n' + lstPre.join('\n') + '\n\n' + strCode + '\n\n' + lstPost.join('\n') + '\n\n'; | |
// MAIN - WRAP SELECTED CODE LINES IN SIMPLE TIMING CODE FOR ROUGH PERFORMANCE EXPERIMENTS | |
drafts.setTextInRange(iFrom, lngSelnChars - 1, strWrapped); | |
lngLen = strWrapped.length - 1; | |
drafts.setSelectedRange(iFrom, strWrapped.length - 1); | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment