To simply add simply run this 1-liner:
app.selection.contents = ", " + app.selection.contents + ",";
However it is far away from a smart solution...
Thank you, this is ALMOST all I need, except that InDesign adds also a space BEFORE the first comma.
In the end you have:
"some text , some more text, even more text".
Any simple way to get rid of that little, insignificant, typographically awful space before the first comma?
That's why I wrote is no smart. InDesign does not add additional space - it was there before since your selection is "from 1st letter to the last", I guess.
Select text including space before and add pure "," on both sides.
To tell the true your goal is not to add something "before" and "after" selection but insert "," before 1st character on the left side of selection (if it is a space) and add "after".
… Any simple way to get rid of that little, insignificant, typographically awful space before the first comma? …
Perhaps you can try something like this:
var con = app.selection.contents; con = "," + con + ","; app.selection.contents = con; var conPar = app.selection.lines.contents.replace (" ,",","); app.selection.lines.contents = conPar;
Since you can't depend on users carefully selecting what you want, my idea would be to first insert a comma and a space at both ends of the selected text. It works from right to left because the left-end comma moves the right-end insertion point
var mySelection = app.selection;
// then clean up any resulting messiness
var myStory = mySelection.parentStory;
app.findGrepPreferences.findWhat="\\s+,"; // any number of spaces + a comma
app.changeGrepPreferences.changeTo=","; // a comma with no spaces
app.findGrepPreferences.findWhat=",\\s+"; // a comma followed by an number of spaces
app.changeGrepPreferences.changeTo=", "; // a comma followed by only one space
A real grep expert might be able to do this more efficiently