Skip to content

Instantly share code, notes, and snippets.

@UskeS
Created September 3, 2020 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save UskeS/6f8a527b6fa15dce8fb606592444e649 to your computer and use it in GitHub Desktop.
Save UskeS/6f8a527b6fa15dce8fb606592444e649 to your computer and use it in GitHub Desktop.
[InDesign] [Illustrator] [ExtendScript] 選択したISBNコード/JANコード/雑誌コードのチェックディジットが正しいかどうかをチェックします
!function() {
if (app.documents.length === 0) {
return;
}
if (app.name === "Adobe InDesign" && app.activeDocument.selection.length === 1) {
var result = checkDigit(app.activeDocument.selection[0]);
if (result === null) {
return;
} else if (result === true) {
alert("正しいチェックディジットです");
return;
} else {
alert("正しいチェックディジットは" + result + "です");
return;
}
}
if (app.name === "Adobe Illustrator" && app.activeDocument.selection.length === 13) {
var result = checkDigit(selection);
if (result === null) {
return;
} else if (result === true) {
alert("正しいチェックディジットです");
return;
} else {
alert("正しいチェックディジットは" + result + "です");
return;
}
}
}();
function checkDigit (tgt) {
if (!tgt.hasOwnProperty("contents")) {
alert("テキストが選択されていません");
return null;
}
tgt = tgt.contents.replace(/\-/g, "");
if (!/[0-9]+/.test(tgt)) {
alert("テキストに半角数字以外が含まれています");
return null;
}
if (tgt.length !== 13) {
alert("数字が13桁ではありません");
return null;
}
tgt = tgt.split("");
var lastDigit = tgt[tgt.length - 1];
var res = [0, 0];
for (var i=0; i<tgt.length-1; i++) {
if (i === 0 || (i % 2) === 0) {
res[1] += tgt[i] * 1;
} else {
res[0] += tgt[i] * 3;
}
}
res = res[0] + res[1];
if (res % 10) {
res = 10 - (res % 10);
} else {
res = 0;
}
if (res == lastDigit) {
return true;
} else {
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment