Skip to content

Instantly share code, notes, and snippets.

@konn
Last active August 29, 2015 14:17
Show Gist options
  • Select an option

  • Save konn/a4033588e1cee96cad77 to your computer and use it in GitHub Desktop.

Select an option

Save konn/a4033588e1cee96cad77 to your computer and use it in GitHub Desktop.
An attempt to automating editing works using OS X Yosemite's JXA (JavaScript for Automation)
String.prototype.tr = function (from, to) {
var str = this;
for (i in [...Array(Math.min(from.length, to.length))]) {
str = str.replace(new RegExp(from[i], "g"), to[i]);
}
return str;
}
function kanjify(str) {
return str.tr("0123456789", "〇一二三四五六七八九");
}
function changeDate(str) {
return str.replace(/(\d{4})(?:\/(\d{1,2})(?:\/(\d{1,2}))?)?/g, function(str, yyyy, mm, dd){
var tmp = kanjify(yyyy)
if (mm) tmp += "年" + kanjify(mm) + "月";
if (dd) tmp += kanjify(dd) + "日";
return tmp;
})
}
var app = Application("Adobe InDesign CS5"),
doc = app.activeDocument,
style = doc.styles;
app.includeStandardAdditions = true;
app.findGrepPreferences.findWhat = '(\\d{4})(?:\/(\\d{1,2})(?:\/(\\d{1,2}))?)?';
app.findGrepPreferences.appliedParagraphStyle = "";
doc.findGrep({reverseOrder: true}).forEach(function(txt){
var tmp = txt.contents(),
ans = changeDate(tmp);
txt.select();
txt.showText();
var go = app.displayDialog(tmp + " を " + ans + "に置換しますか?",
{buttons: ["キャンセル","スキップ","OK"],
defaultButton: "OK", cancelButton: "キャンセル"})
.buttonReturned == "OK";
if (go) {
app.findTextPreferences.findWhat = tmp;
app.changeTextPreferences.changeTo = ans;
txt.changeText();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment