以前にKeynote用に作っておいたAppleScriptを、Pages用に書き直しました。
Pages書類、主に奥付けのテキストなどで、「項目名」「:」(セパレータ)「内容」みたいに列挙している箇所を行頭からセパレータの場所の前まで太字の書体に変更します。ヒラギノ角ゴシックでチェックしており、欧文フォントは考慮していません。
Pages v14.2+macOS 15.2Betaで実験しています。
AppleScript名:フォントを記号の前まで太らせる.scptd |
— – Created by: Takaaki Naganoya – Created on: 2024/11/01 — – Copyright © 2024 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use framework "AppKit" use scripting additions property NSFont : a reference to current application’s NSFont property NSFontManager : a reference to current application’s NSFontManager –セパレータリスト、表記ゆらぎ対応(ゆらぎ表記個数は可変) property separatorList : {{":", ":"}, {"mm", "㎜"}, {"cm", "㎝"}} tell application "Pages" tell front document set aSel to selection –Keynote上の選択中のオブジェクトでループ repeat with i in aSel set j to contents of i set tmpClass to class of j –選択中のオブジェクトがテキストアイテムの場合に……. if tmpClass = shape then set objText to object text of j set fontName to font of object text of j set fontSize to size of object text of j –フォントを太らせる(ウェイトを上げる) set fFamilyCount to countFontsInItsFamily(fontName) of me if fFamilyCount = 2 then –ヒラギノ角ゴProN W3 → ヒラギノ角ゴProN W6 set newFont to incrementFontWeight(fontName, 1) of me else if fFamilyCount > 4 then –ヒラギノ角ゴシック Wn のウェイトを上げ set newFont to incrementFontWeight(fontName, 4) of me end if set aCount to 1 set tList to splitByLInes(objText) of me –行ごとにParseした行ごとのテキストでループ repeat with ii in tList set jj to contents of ii set anOffset to 0 –セパレータでループ repeat with iii in separatorList –セパレータの「ゆらぎ」表記を考慮してループ repeat with iiii in iii set jjjj to contents of iiii set anOffset to offset of jjjj in jj if anOffset is not equal to 0 then exit repeat end if end repeat if anOffset is not equal to 0 then exit repeat end repeat if anOffset is not equal to 0 then try set font of characters 1 thru (anOffset – 1) of paragraph aCount of object text of j to newFont end try end if set aCount to aCount + 1 end repeat end if end repeat end tell end tell –テキストを行ごとにParse on splitByLInes(someText) — free to a good home set theString to current application’s NSString’s stringWithString:someText set theList to theString’s componentsSeparatedByCharactersInSet:(current application’s NSCharacterSet’s newlineCharacterSet()) return theList as list end splitByLInes –フォントを太らせる。欧文フォントは考慮していない(別の方法で行う) on incrementFontWeight(psFontName, incNum) set aFont to current application’s NSFont’s fontWithName:psFontName |size|:9.0 –> (NSCTFont) "HiraginoSans-W0 9.00 pt. P [] (0x12870af00) fobj=0x11b1e90d0, spc=1.98" set fontM to current application’s NSFontManager’s sharedFontManager() repeat incNum times set aFont to fontM’s convertWeight:true ofFont:aFont end repeat return (aFont’s fontName()) as string end incrementFontWeight –指定フォントのファミリーに属するフォント数を取得 on countFontsInItsFamily(aPSName) set aFont to current application’s NSFont’s fontWithName:(aPSName) |size|:9.0 set aFamily to aFont’s familyName() set fMan to current application’s NSFontManager’s sharedFontManager() set fList to fMan’s availableMembersOfFontFamily:aFamily return length of (fList as list) end countFontsInItsFamily |