Skip to content

Instantly share code, notes, and snippets.

@RobTrew
Last active April 12, 2023 07:26
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RobTrew/ca48a04b491449209385 to your computer and use it in GitHub Desktop.
Save RobTrew/ca48a04b491449209385 to your computer and use it in GitHub Desktop.
OS X 10.10 (Yosemite) definitions of the basic script editing functions used in iOS Drafts 4
// Ver 0.4 Rob Trew
// Library for using OSX Yosemite Script Editor scripts
// written in an idiom compatible with FoldingText, Drafts, 1Writer, TextWell
// In iOS Drafts 4 scripts, the header is simply:
// var drafts = this;
// For headers for FoldingText and other editors, see:
// See: http://support.foldingtext.com/t/writing-scripts-which-run-on-both-foldingtext-and-ios-drafts-4/652/7
// Call functions, after this header, with the prefix:
// drafts.
// e.g.
// var drafts = Library('DraftsSE');
// var dctSelnRange = drafts.getSelectedRange(),
// iFrom = dctSelnRange[0],
// iTo = iFrom + dctSelnRange[1];
// These functions are intended to be compatible with Agile Tortoise's Drafts documentation at:
// https://agiletortoise.zendesk.com/hc/en-us/articles/202465564-Script-Keys
// getText() getSelectedLineRange()
// setText(string) getSelectedRange()
// getSelectedText() setSelectedRange(start, length)
// setSelectedText(string) getClipboard()
// getTextInRange(start, length) setClipboard(string)
// setTextInRange(start, length, string)
// Installation: Save this as a compiled .scpt to ~/Library/Script Libraries/DraftsSE.scpt
// and then invoke in other scripts as above, through the JXA Library object.
var appSE = Application("Script Editor"),
lstDocs = appSE.documents(),
oDoc = lstDocs.length ? lstDocs[0] : null;
appSE.includeStandardAdditions = true;
appSE.activate();
function getText() {
if (oDoc) return oDoc.text();
}
function setText(strText) {
if (oDoc) oDoc.text = strText;
}
function getSelectedText() {
if (oDoc) return oDoc.selection.contents();
}
function setSelectedText(strText) {
if (oDoc) oDoc.selection.contents = strText;
}
function getTextInRange(iStart, iLength) {
if (oDoc) return oDoc.text().substring(iStart, iStart + iLength);
}
function setTextInRange(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) {}
}
}
function getSelectedLineRange() {
// works, but let me know if you see a shorter route :-)
var 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] === '\n') break;
iFrom += 1;
iTo = dct.y - 1;
while (iTo++ < lngChars)
if (strFull[iTo] === '\n') break;
return [iFrom, (iTo - iFrom)];
}
}
function getSelectedRange() {
var dct, iFrom, iTo;
if (oDoc) {
dct = oDoc.selection.characterRange();
}
iFrom = dct.x;
iTo = dct.y;
return [iFrom, iTo - iFrom];
}
function setSelectedRange(iStart, iLength) {
if (oDoc) {
oDoc.selection = oDoc.text.characters.slice(iStart, iStart + iLength);
}
}
function getClipboard() {
return appSE.theClipboard();
}
function setClipboard(strText) {
appSE.setTheClipboardTo(strText);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment