ごんれのラボ

iOS、Android、Adobe系ソフトの自動化スクリプトのことを書き連ねています。

Illustratorドキュメントで使用しているフォント名をテキストに書き出す

概要

昔書いた記事が読みづらかったので、リライト。
Illustratorファイルで使用しているフォント名を、ざっくりでいいので取得できないかなーと思って書いたAppleScript。

仕様

  • フォント名とIllustratorドキュメントのフルパスをファイルに書き出す
  • フォント名は、フォントファミリーとフォントフェイスを結合して生成
  • フォントファミリーとフォントフェイスが同数じゃなかったらログファイルにはエラーを書き込む

サンプル

適当なドキュメントを作成して、キャプチャしたもの。

f:id:macneko-ayu:20171019002107j:plain
Illustratorドキュメントで確認した使用フォント

f:id:macneko-ayu:20171019002111j:plain
出力されたフォントリスト

使い方

最初にやること

  1. この記事の最後にあるダウンロードリンクからzipをダウンロード
  2. zipを解凍する
  3. IllustratorFontSearch.txt をテキストエディタで開いて内容をコピー
  4. 「スクリプトエディタ」を起動して、メニューから 新規 を選択し、開いたドキュメントにペーストボードの内容をペースト
  5. メニューから 保存 を選択
  6. 保存ダイアログのファイルフォーマットを アプリケーション にして、ダウンロードしたフォルダに保存

フォントリストを書き出したいとき

  1. Illustratorドキュメントを 最初にやること で作成したアプリケーション(特に変更しなければ IllustratorFontSearch.app のはず)にドラッグ&ドロップする
  2. デスクトップに フォントリスト.txt が作成される

ソースコード

AppleScript

on open thisList
    tell application "Finder"
        --Rubyのバージョンチェック
        if exists ((home as text) & ".bash_profile") then
            set ruby_version to do shell script "source ~/.bash_profile; ruby -e 'print \"ERROR\" if RUBY_VERSION <= \"1.8.1\"'"
        else
            set ruby_version to do shell script "ruby -e 'print \"ERROR\" if RUBY_VERSION <= \"1.8.1\"'"
        end if
        if ruby_version is "ERROR" then
            display dialog "このスクリプトはRubyバージョンが1.8.2以上で動作します"
            error number -128
        end if
        
        --変数初期化        
        set dPath to desktop as text --デスクトップのパス取得
        set textFile to dPath & "フォントリスト.txt" --ログ書き出し 用のテキストファイルの名前
        set processLog to "" --エラーが出たファイルのパスを入れる箱
        set skipFile to 0 --CS2以下のファイルのカウント用
        
        --Rubyのファイルを取得
        set my_script_name to "IllustratorFontSearch.rb"
        set my_script_folder to parent of (path to me) as Unicode text
        set my_script to my_script_folder & my_script_name
        set my_script to (quoted form of POSIX path of my_script)
        
        --ここからメイン処理
        repeat with k in thisList
            
            set input_file to (quoted form of POSIX path of contents of k) as Unicode text --Unicode textに変換しないとダメ
            
            if exists ((home as text) & ".bash_profile") then
                do shell script "source ~/.bash_profile; ruby " & my_script & " " & input_file --Rubyで処理して戻り値を取得
            else
                do shell script "ruby " & my_script & " " & input_file --Rubyで処理して戻り値を取得
            end if
            set outputList to result --変数に代入
            
            set processLog to processLog & "===" & (k as string) & "===" & (ASCII character (13))
            
            if outputList is "nil" then
                set processLog to processLog & "フォントが使用されていないようです" & (ASCII character (13))
            else if outputList is "error" then
                set processLog to processLog & "FamilyとFaceの数があいません" & (ASCII character (13))
            else
                set processLog to processLog & outputList & (ASCII character (13))
            end if
        end repeat
        
        --ログの書き出し
        if processLog is not "" then
            try
                set OutPutText to open for access textFile with write permission
                set eof OutPutText to 0
                write processLog to OutPutText
                close access OutPutText
            on error errMsg
                display dialog errMsg
                close access OutPutText
            end try
        end if
    end tell
    
    display dialog "終了しました"
end open

Ruby

#! ruby -Ku

require 'CGI'
require 'kconv'

temp = ""
fontFamilyList = [] ; #フォントのファミリーの入れ物
fontFaceList = [] ; #フォントの太さの入れ物
fontList = []

#XMP情報を抽出して変数に代入
def xmpRead(fileObj, temp)
    begin
        while line = fileObj.gets 
            line = line.toutf8
            line = CGI.unescapeHTML(line)#HTMLエスケープされてるのでアンエスケープ
            
            #XMP終了時に例外を発生させる
            if /^<\/x:xmpmeta>/ =~ line then
                raise "loop stop" #raiseで例外を発生できる
            end

            if temp == ""
                temp = line
            else
                temp = temp + line
            end
        end
    rescue
        return temp #tempを返す
    end
end

def IllustratorFontSearch(temp, fontFamilyList, fontFaceList, fontList)
    if /Adobe Illustrator CS2/ =~ temp
        raise "before CS2"
    end
    
    #ファミリーとフェイスを検索して配列にする
    fontFamilyList = temp.scan(/^\s+?<stFnt:fontFamily>(.+?)<\/stFnt:fontFamily>/)
    fontFaceList = temp.scan(/^\s+?<stFnt:fontFace>(.+?)<\/stFnt:fontFace>/)
    
    #空だったら
    if fontFamilyList == [] && fontFaceList == [] 
        fontList = "nil"#nilを文字として返す
    elsif fontFamilyList.length == fontFaceList.length
        #配列の数だけ繰り替えす
        i = 0
        while i < fontFamilyList.length
            fontFamily = fontFamilyList[i][0]#配列のネストなので[0]が必要
            fontFace = fontFaceList[i][0]
            fontList.push(fontFamily + " " +fontFace)#ファミリーとフェイスを繋げてフォント名を作成
            i += 1
        end
    #数が合わないときはエラーを返す
    else
        fontList = "error"
    end 
    return fontList #fontListを返す
rescue
    
end

temp = xmpRead(ARGF, temp)
result = IllustratorFontSearch(temp, fontFamilyList, fontFaceList, fontList)
puts result

ダウンロード

https://box.yahoo.co.jp/guest/viewer?sid=box-l-qk3texvluwbqgcyxkherewskwe-1001&uniqid=0f62f3dd-0320-4b0e-b949-8271afa040bb&viewtype=detail