Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Extendscript for creating font samples in Indesign
// sample movie: https://user-images.githubusercontent.com/5406126/49324964-f631aa80-f57c-11e8-9199-0ab51231422c.gif
main();
function main() {
// 関数定義
function applyFontToStory(targetFont) {
for (var i = 0, txLen = doc.textFrames.length; i < txLen; i++) {
doc.textFrames[i].parentStory.appliedFont = targetFont;
}
}
function extractFontsFromName(wantToExtractFontName) {
var result = [];
if (undefined == wantToExtractFontName) {
return app.fonts;
}
var regexp = new RegExp(wantToExtractFontName, 'i');
for (var i = 0, len = app.fonts.length; i < len; i++) {
var font = app.fonts[i];
if (!isExpectFont(font)) {
continue;
}
try {
if (regexp.test(font.fullNameNative)) {
result.push(font);
}
} catch (e) {
if (regexp.test(font.fullName)) {
result.push(font);
}
}
}
return result.length > 0 ? result : undefined;
}
function exportPDFX4(pdfName) {
doc.exportFile(ExportFormat.pdfType, File(saveObj + '/' + pdfName + '.pdf'), false, '[PDF/X-4:2008 (日本)]');
}
function exportJPEG(jpegName) {
doc.exportFile(ExportFormat.JPG, File(saveObj + '/' + jpegName + '.jpg'), false);
}
function isExpectFont(targetFont) {
try {
if (targetFont.fontType == FontTypes.BITMAP) {
return false;
}
if (targetFont.fontType == FontTypes.OCF) {
return false;
}
if (targetFont.fontType == FontTypes.TYPE_1) {
return false;
}
if (!targetFont.allowPDFEmbedding) {
return false;
}
} catch (e) {
return false;
}
return true;
}
// ここから処理開始
var wantToExtractFontName = prompt('検索したいフォント名を入力してください', '', 'Font search');
if (null == wantToExtractFontName) {
return;
}
var saveObj = Folder.selectDialog("PDFを保存するフォルダを選択してください");
if (undefined == saveObj) {
return;
}
var preference = app.jpegExportPreferences;
preference.antiAlias = true;
preference.exportResolution = 72 * 4;
preference.jpegColorSpace = JpegColorSpaceEnum.RGB;
preference.jpegQuality = JPEGOptionsQuality.MAXIMUM;
var doc = app.activeDocument;
var extractedFonts = extractFontsFromName(wantToExtractFontName);
if (undefined == extractedFonts) {
alert('フォントが見つかりません');
return;
}
for (var i = 0, len = extractedFonts.length; i < len; i++) {
applyFontToStory(extractedFonts[i]);
var fontName = '';
try {
fontName = extractedFonts[i].fullNameNative;
} catch (e) {
fontName = extractedFonts[i].fullName;
}
exportPDFX4(fontName);
exportJPEG(fontName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.