
印刷をする際など、手持ちのPDFの判型名を知りたくなることがあります。判型名とはA4やB5等、紙の大きさや仕上がりの寸法をもとに決まっている規格の名前のことです。
通常だとAcrobatやPreview・IllustratorなどのアプリでPDFを開き、幅と高さを確認して判断することでしょう。しかし、わざわざファイルを開く手間が無駄に思えます。また、サイズから判型を判断する部分は自分の記憶か資料を参照する必要があります。もっと簡単にしたいですね。
そこで今回は、Finderで選択しているPDFファイルの判型名をダイアログで表示するJXAスクリプト を紹介します。JXA(JavaScript for Automation)でできているためmacOS専用です。Windowsのかたはすみません。
あらましを教えて
Finder用スクリプト(JXA)です。選択したPDFの1ページ目のサイズをもとに判型を推測し、ダイアログで結果を通知します。
こちらからダウンロードしてください。
使いかたは?
FinderでPDFファイルを選択し、スクリプトを実行するだけです。実行はscptファイルをスクリプトメニューに登録するか、Keyboard MaestroのExecute JavaScript For Automationアクションで呼び出すと便利です。

実行するとダイアログに結果が出てきます。

これでまた少し仕事が速くなりました。今日もさっさと仕事を切り上げて好きなことをしましょう!
作者に感謝を伝えたい!
Buy me a coffeeは、クレジットカード払いなどでクリエイターにコーヒーをおごれるサービスです。ツール・情報が役に立った! 感謝の気持ちを表現したい! というかた、おごっていただけましたら嬉しいです☕️
コードはこちら。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
/** * @file Finderで選択しているPDFファイルの判型を推測してダイアログで表示する * @version 1.0.1 * @author sttk3.com * @copyright © 2025 sttk3.com */ ObjC.import('Quartz') ; function run() { const finderApp = Application('Finder') ; finderApp.includeStandardAdditions = true ; const sel = finderApp.selection().map((item) => {return getPath(item)}) ; const targetPDFs = sel.filter((path) => {return /\.pdf$/i.test(path)}) ; if(targetPDFs.length <= 0) {return ;} const targetPath = targetPDFs[0] ; const filename = targetPath.match(/[^/]+$/)[0] ; const pageSize = getPDFPageSize(targetPath, PDFCrop.TrimBox) ; const pageSizeFixed = {width: fixed(pageSize.width), height: fixed(pageSize.height)} ; const sizeName = getSizeName(pageSizeFixed) ; const message = `判型: ${sizeName}\n幅: ${pageSizeFixed.width}\n高さ: ${pageSizeFixed.height}` ; const buttons = ['OK'] ; finderApp.displayDialog(message, {withTitle: filename, buttons: buttons, defaultButton: buttons[0]}) ; } /** * FinderItemからPOSIXパスを取得する * @param {FinderItem} item FinderのFile/Folder * @returns {string} */ function getPath(item) { return decodeURIComponent(item.url().slice(7)) ; } /** * @typedef {Object} PageSize - PDFのページの寸法 * @property {number} width - 幅(pt) * @property {number} height - 高さ(pt) */ /** * PDFDisplayBox * @typedef {Object} PDFCrop * @property {number} MediaBox - 印刷可能な最大の領域 * @property {number} CropBox - ページの表示と印刷時に使用される領域。初期値はMediaBoxと同じ * @property {number} BleedBox - 塗りたし(ブリード)を含む領域 * @property {number} TrimBox - ページの仕上がりサイズ領域。初期値はCropBoxと同じ * @property {number} ArtBox - アートワークの領域。ページに描画されるコンテンツの範囲を表す */ /** * PDFDisplayBox * @type {PDFCrop} */ const PDFCrop = { 'MediaBox': $.kPDFDisplayBoxMediaBox, 'CropBox': $.kPDFDisplayBoxCropBox, 'BleedBox': $.kPDFDisplayBoxBleedBox, 'TrimBox': $.kPDFDisplayBoxTrimBox, 'ArtBox': $.kPDFDisplayBoxArtBox, } ; /** * PDFファイルの1ページ目の寸法を返す * @param {string} filepath 対象のPDFファイルのパス * @param {PDFCrop} pdfCrop 取得するPDFのサイズ領域 * @return {PageSize} */ function getPDFPageSize(filepath, pdfCrop = PDFCrop.TrimBox) { const pdfURL = $.NSURL.fileURLWithPath(filepath) ; const pdfDoc = $.PDFDocument.alloc.initWithURL(pdfURL) ; if(!pdfDoc) { throw new Error('PDFファイルを読み込めませんでした') ; } const firstPage = pdfDoc.pageAtIndex(0) ; const mediaBox = firstPage.boundsForBox(pdfCrop) ; const pageSize = { width: mediaBox.size.width, height: mediaBox.size.height, } ; return pageSize ; } /** * @typedef {Object.<string, string>} LookupTable * @example - {'841.89_595.28': 'A4'} */ /** * 数値を小数点以下一定の品質に変更する * @param {number} num target number * @returns {string} */ function fixed(num) { return num.toFixed(2) ; } /** * サイズから判型を検索するためのテーブルを生成する * @return {LookupTable} */ function generateLookupTable() { const docSizes = [ {name: 'A0', fixedAspectRatio: false, width: 2383.937, height: 3370.394}, {name: 'A1', fixedAspectRatio: false, width: 1683.780, height: 2383.937}, {name: 'A2', fixedAspectRatio: false, width: 1190.551, height: 1683.780}, {name: 'A3', fixedAspectRatio: false, width: 841.890, height: 1190.551}, {name: 'A4', fixedAspectRatio: false, width: 595.276, height: 841.890}, {name: 'A5', fixedAspectRatio: false, width: 419.528, height: 595.276}, {name: 'A6', fixedAspectRatio: false, width: 297.638, height: 419.528}, {name: 'A7', fixedAspectRatio: false, width: 209.764, height: 297.638}, {name: 'A8', fixedAspectRatio: false, width: 147.402, height: 209.764}, {name: 'A9', fixedAspectRatio: false, width: 104.882, height: 147.402}, {name: 'A10', fixedAspectRatio: false, width: 73.701, height: 104.882}, {name: 'B0', fixedAspectRatio: false, width: 2919.685, height: 4127.244}, {name: 'B1', fixedAspectRatio: false, width: 2063.622, height: 2919.685}, {name: 'B2', fixedAspectRatio: false, width: 1459.843, height: 2063.622}, {name: 'B3', fixedAspectRatio: false, width: 1031.811, height: 1459.843}, {name: 'B4', fixedAspectRatio: false, width: 728.504, height: 1031.811}, {name: 'B5', fixedAspectRatio: false, width: 515.906, height: 728.504}, {name: 'B6', fixedAspectRatio: false, width: 362.835, height: 515.906}, {name: 'B7', fixedAspectRatio: false, width: 257.953, height: 362.835}, {name: 'B8', fixedAspectRatio: false, width: 181.417, height: 257.953}, {name: 'B9', fixedAspectRatio: false, width: 127.559, height: 181.417}, {name: 'B10', fixedAspectRatio: false, width: 90.709, height: 127.559}, {name: 'ハガキ', fixedAspectRatio: false, width: 283.465, height: 419.528}, {name: '往復ハガキ', fixedAspectRatio: false, width: 566.929, height: 419.528}, {name: '長3封筒', fixedAspectRatio: true, width: 340.157, height: 666.142}, {name: '角A4封筒', fixedAspectRatio: true, width: 646.299, height: 884.409}, {name: '角2封筒', fixedAspectRatio: true, width: 680.315, height: 941.102}, {name: '名刺', fixedAspectRatio: false, width: 155.906, height: 257.953}, {name: 'カード', fixedAspectRatio: false, width: 153.000, height: 242.640}, {name: 'Letter', fixedAspectRatio: false, width: 612.000, height: 792.000}, {name: 'Legal', fixedAspectRatio: false, width: 612.000, height: 1008.000}, {name: 'Tabloid', fixedAspectRatio: true, width: 792.000, height: 1224.000}, {name: 'Ledger', fixedAspectRatio: true, width: 1224.000, height: 792.000}, ] ; const res = {} ; for(let docType of docSizes) { const fixedWidth = fixed(docType.width) ; const fixedHeight = fixed(docType.height) ; const key1 = `${fixedWidth}_${fixedHeight}` ; const key2 = `${fixedHeight}_${fixedWidth}` ; // width_heightでindexを生成する res[key1] = docType.name ; // 縦横が完全に指定サイズで回転できないもの以外は、height_widthのindexも生成する if(!docType.fixedAspectRatio) { res[key2] = docType.name ; } } return res ; } /** * サイズから'A4'などの判型名を生成して返す * @param {PageSize} pageSize {width: number, height: number} * @return {string} */ function getSizeName(pageSize) { // サイズ名一覧を生成する const lookupTable = generateLookupTable() ; // `width_height`でサイズ名を取得する。縦長横長を気にする必要はない。両方定義してある const key = `${pageSize.width}_${pageSize.height}` ; const res = lookupTable[key] || 'Custom Size' ; return res ; } |
このサイトで配布しているスクリプトやその他のファイルを、無断で転載・配布・販売することを禁じます。
それらの使用により生じたあらゆる損害について、私どもは責任を負いません。
スクリプトやファイルのダウンロードを行った時点で、上記の規定に同意したとみなします。