Skip to content

Instantly share code, notes, and snippets.

@noellabo
Created August 7, 2018 02:34
Show Gist options
  • Save noellabo/7e952ba9c584548da337f82322958b0c to your computer and use it in GitHub Desktop.
Save noellabo/7e952ba9c584548da337f82322958b0c to your computer and use it in GitHub Desktop.
Illustratorのアートボード名を連番にリネームする(普通に数値があればそれを加算、#付けた数値があればそれを利用)
#target "illustrator"
/*
NumberingArtboards.jsx
Copyright (c) 2018 Takeshi Umeda (noellabo)
Released under the MIT license
http://opensource.org/licenses/mit-license.php
http://noellabo.jp/
ver. 0.1.0
*/
(function() {
const SCRIPT_TITLE = 'NumberingArtboards';
const SCRIPT_VERSION = '0.1.0';
// Alert Messages (Please translate to the language you use)
$.localize = true;
const DocumentNotExist = {
en: 'Can not execute, becouse document is not exist.',
ja: 'ドキュメントがないため実行できません',
};
const ExceptionMessage = {
en: 'An error occurred and processing could not be executed.\nError Message:',
ja: 'エラーが発生して処理を実行できませんでした\nエラー内容:',
};
if(app.documents.length == 0) {
alert(DocumentNotExist);
} else {
try {
var doc = app.activeDocument;
mainProcess();
} catch(e) {
alert(ExceptionMessage + e);
}
}
function mainProcess() {
var artboards = doc.artboards;
var artboards_len = artboards.length;
var format = parseArtboardNumber(artboards[0].name);
var num = format.num;
for(var index=0; index < artboards_len; index++) {
artboards[index].name = format.prefix + ((format.len == 0) ? num : zeroPadding(num, format.len)) + format.postfix;
num++;
}
}
function parseArtboardNumber(name, connect_char) {
var connect_char = typeof connect_char !== 'undefined' ? connect_char : '_';
var result = name.match(/^(.*?)#(0*)?([0-9]+)(.*)$/);
if(result == null) {
result = name.match(/^(.*?)(0*)?([0-9]+)([^0-9]*)$/);
}
if(result == null) {
return {prefix: name + connect_char, postfix: '', num: 1}
} else {
var len = (result[2].length > 0) ? result[2].length + result[3].length : 0;
return {prefix: result[1], postfix: result[4], num: parseInt(result[3]), len: len}
}
}
function zeroPadding(num, len) {
return (Array(len).join('0') + num).slice (-len);
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment