Last active
August 29, 2015 14:17
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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