I've been trying to set up a TeXworks script that would add the in-line math mode symbol '$' around a selection. E.g., if one selected '\omega' and executed the script, the result would be '$\omega$'.
I unashamedly based myself on the two scripts that have been built in TeXworks. One finds these under 'Scripts/Latex styles'. Using a Text Editor under Mac OS X 10.10.5, I came up with the script below. The last line is the one that calls the function that would add two '$' to the selected text.
Following the TeXworks manual, I've stored it in the proper script directory and had TeXworks re-read the script list. Lo and behold, my script shows up in the Menu, with the proper key combo. But, alas, nothing happens. TeXworks acknowledges the key combination from the keyboard but doesn't do a thing to the selected text.
Any suggestion would be highly appreciated. Thank you.
// TeXworksScript
// Title: Toggle Math Mode
// Shortcut: Ctrl+Shift+M
// Description: Encloses the current selection in $$
(...)
// Script-Type: standalone
// Context: TeXDocument
function addOrRemove(prefix, suffix) {
var txt = TW.target.selection;
var len = txt.length;
var wrapped = prefix + txt + suffix;
var pos = TW.target.selectionStart;
if (pos >= prefix.length) {
TW.target.selectRange(pos - prefix.length, wrapped.length);
if (TW.target.selection == wrapped) {
TW.target.insertText(txt);
TW.target.selectRange(pos - prefix.length, len);
return;
}
TW.target.selectRange(pos, len);
}
TW.target.insertText(wrapped);
TW.target.selectRange(pos + prefix.length, len);
return;
};
addOrRemove(“$”, “$”);