#script "Roman number converter" #target indesign /* * * Római/Arab szám konvertáló szkript Adobe InDesign CS3-hoz (esetleg CS2-höz) * * Ez egy tipikus másol-beilleszt technikával készült termék ;-) * Fogtam Thesh Ooter "Roman Numeral Convertor" kódját * (http://www.javascriptkit.com/script/script2/romanconvert.shtml), * és a "Working with Text Selections" példaprogramot az * "InDesign CS3 Scripting Guide JS.pdf" dokumentációból, * kicsit módosítottam rajtuk, adtam hozzájuk némi huncutságot * és illesztettem az InDesign-hoz. * Kösz srácok! * * A szkriptet Makó Tamás szerkesztette össze * Szoftver Automatizálás * http://szoftverautomatizalas.hu/ * *** * * Roman/Integer Numeral Convertor for Adobe InDesign CS3 (maybe CS2) * * This is a typical product of copy-paste-programming ;-) * I took Thesh Ooter's Roman Numeral Convertor * (http://www.javascriptkit.com/script/script2/romanconvert.shtml) * and a "Working with Text Selections" sample code from the * "InDesign CS3 Scripting Guide JS.pdf", * I slightly modified both, merged together, added some spices * and suited for InDesign. * Thanks guys! * * This script was constructed by Tamás Makó * Software Automation * http://softwareautomation.hu/products * ***** * * Feature requests: * - change abbreviation of months' names to roman number * (e.g. jan.->I. or aug. -> VIII. etc.) * - change all "month.day" occurences in a selected text * * History: * 0.9.2 * - change new content insertion method (through lost reference exception) * 0.9.1 * - use [] syntax instead of .item() * 0.9 * - initial release * */ /* * Flag for no alerts * Useful for debugging for example */ var silentMode = true; var undefined; /* * These first arrays are used by * the functions which convert * roman numerals into integers. */ var counter = new Array(7); var romans = new Array(7); romans["I"] = 1; romans["V"] = 5; romans["X"] = 10; romans["L"] = 50; romans["C"] = 100; romans["D"] = 500; romans["M"] = 1000; var subs = new Array(4); subs["I"] = true; subs["X"] = true; subs["C"] = true; subs["M"] = true; /* * This next array stores the data * needed to convert integers into * roman numerals. */ var getChar = new Array(7); getChar[0] = "I"; getChar[1] = "V"; getChar[2] = "X"; getChar[3] = "L"; getChar[4] = "C"; getChar[5] = "D"; getChar[6] = "M"; /* * This first function is a pretty * simple and generic function used * to define and throw errors */ function createError(ErrorName, ErrorMessage) { var theError = new Error(); theError.name = ErrorName; theError.message = ErrorMessage; throw theError; } /* * These next four functions are * used to test for errors in the * input and convert roman numerals * into integers. */ function checkRom(Rcur, Rnext, lSub, n, l) { if ((romans[Rcur] == undefined) || ((romans[Rnext] == undefined) && ((n + 1) < l))) { createError("InputError", "Not a Roman Numeral"); } else if (romans[Rcur] >= lSub) { createError("InputError", "Not a Properly Formed Numeral"); } } function testSub(cR, nR, pR) { if (romans[cR] < romans[nR]) { if ((romans[pR] == romans[nR]) && (subs[nR] != true)) { createError("InputError", "Not a Properly Formed Numeral"); } else if ((subs[cR] == true) && (10*romans[cR] >= romans[nR])) { return true; } else { createError("InputError", "Not a Properly Formed Numeral"); } } return false; } function testRom(rome) { if (counter[rome] < 3) { return true; } else { createError("InputError", "Not a Properly Formed Numeral"); } return false; } function parseRomanToInt(rNumb, strict) { counter["I"] = 0; counter["V"] = 2; counter["X"] = 0; counter["L"] = 2; counter["C"] = 0; counter["D"] = 2; counter["M"] = Number.NEGATIVE_INFINITY; var intNumb = 0; var lastNumb = Number.POSITIVE_INFINITY; var thisNumb = 0; var lastSub = Number.POSITIVE_INFINITY; rNumb = rNumb.toString().toUpperCase() for (var i=0; i lastNumb) { createError("InputError", "Not a Properly Formed Numeral"); } else { intNumb += thisNumb; lastNumb = thisNumb; } } else { if ((romans[currentR] == undefined) || ((romans[nextR] == undefined) && ((i + 1) < rNumb.length))) { createError("InputError", "Not a Roman Numeral"); } else if (romans[currentR] < romans[nextR]) { thisNumb = (romans[nextR] - romans[currentR]); i++; } else { thisNumb = romans[currentR]; } intNumb += thisNumb; } } catch(e) { alertBox(e.name + ": " + e.message); intNumb = null break; } } if (intNumb != null) { intNumb = intNumb.toString(); } return intNumb; } /* * The next two functions are used * for converting an integer into a * roman Numeral. */ function ints(pos, iValue) { var charValue = ""; var s = 2*pos; if (pos > 2) { for (var i=0; i