Fontのpostscript名を取得する(訂正)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #!/usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # |
005 | # com.cocolog-nifty.quicktimer.icefloe |
006 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
007 | use AppleScript version "2.8" |
008 | use framework "Foundation" |
009 | use framework "AppKit" |
010 | use scripting additions |
011 | property refMe : a reference to current application |
012 | |
013 | |
014 | ################################### |
015 | ##### フォント収集 |
016 | ################################### |
017 | ###フォント初期化 |
018 | set appFontManager to (refMe's NSFontManager) |
019 | set appSharedMaanager to appFontManager's sharedFontManager() |
020 | ###利用可能なフォントリスト |
021 | set ocidFontList to appSharedMaanager's availableFonts() |
022 | ###格納用のレコード |
023 | set ocidFontNameeDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0 |
024 | |
025 | ###フォントの数だけ繰り返し |
026 | repeat with itemFontList in ocidFontList |
027 | ###PS名でフォントオブジェクトにして |
028 | set ocidTempFont to (refMe's NSFont's fontWithName:(itemFontList) matrix:(missing value)) |
029 | ###収録文字数を調べ |
030 | set intCharCnt to ocidTempFont's numberOfGlyphs as integer |
031 | ###5000文字以上なら日本語もあるだろって事で |
032 | if intCharCnt > 5000 then |
033 | set strDisplayName to ocidTempFont's displayName as text |
034 | ###レコードに追加キー名を表示名で 値はPS名 |
035 | (ocidFontNameeDict's setObject:(itemFontList) forKey:(strDisplayName)) |
036 | end if |
037 | end repeat |
038 | |
039 | |
040 | ################################### |
041 | ##### ダイアログ |
042 | ################################### |
043 | ###ダイアログを前面に |
044 | tell current application |
045 | set strName to name as text |
046 | end tell |
047 | ####スクリプトメニューから実行したら |
048 | if strName is "osascript" then |
049 | tell application "Finder" |
050 | activate |
051 | end tell |
052 | else |
053 | tell current application |
054 | activate |
055 | end tell |
056 | end if |
057 | |
058 | ###格納用の可変リスト |
059 | set ocidFontNameeArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0) |
060 | ###ディレクトリのallKeysで値のみのリスト=表示名のリスト |
061 | ocidFontNameeArray's addObjectsFromArray:(ocidFontNameeDict's allKeys()) |
062 | ###並び替え |
063 | set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(true) selector:"localizedStandardCompare:") |
064 | (ocidFontNameeArray's sortUsingDescriptors:{ocidSortDescriptor}) |
065 | ####デフォルト選択値 |
066 | set strDefailtsFont to "Osaka−等幅" as text |
067 | ##JSOC番号なので0スタート値なので1足す |
068 | set intDefaultsFont to (ocidFontNameeArray's indexOfObject:(strDefailtsFont)) + 1 as integer |
069 | ###ASリストにする |
070 | set listFontList to ocidFontNameeArray as list |
071 | ###ダイアログ |
072 | try |
073 | set listResponse to (choose from list listFontList with title "選んでください" with prompt "フォントを選んでください" default items (item intDefaultsFont of listFontList) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list |
074 | on error |
075 | log "エラーしました" |
076 | return "エラーしました" |
077 | end try |
078 | if (item 1 of listResponse) is false then |
079 | return "キャンセルしました" |
080 | end if |
081 | # |
082 | set strFontName to (item 1 of listResponse) |
083 | |
084 | |
085 | #この記述でも戻り値はpostscriptNameですが |
086 | log (ocidFontNameeDict's valueForKey:(strFontName)) as text |
087 | #### |
088 | #正しくはこの方法 |
089 | set ocidChooseFont to refMe's NSFont's fontWithName:(strFontName) |size|:(72) |
090 | set ocidFontDiscriptor to ocidChooseFont's fontDescriptor() |
091 | set strPostScriptName to ocidFontDiscriptor's postscriptName() as text |
092 | #フォントのPostScript名称 |
093 | log strPostScriptName |
094 | |
AppleScriptで生成しました |
| 固定リンク
「NSFontManager」カテゴリの記事
- [NSFontManager]フォント名の取得(修正)(2024.12.24)
- [NSFont]フォント名の取得(フォント名=FULLNAME 英語 日本語ローカライズ名 postscript名)(2024.12.11)
- Fontのpostscript名を取得する(訂正)(2024.09.24)
- [NSFontManager]フォントのリストを取得する(基本編)(2024.06.18)
- [font book]フォントライブラリを作成する(2023.06.19)