[FontBook]フォントコレクションの作成(スタイル別で色々いっぺんに作成)
AppleScript サンプルコード
行番号 | ソース |
---|---|
001 | #! /usr/bin/env osascript |
002 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7 |
003 | # |
004 | # フォントを追加したら再実行すればOKなやつ |
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 | set strBundleID to ("com.apple.FontBook") as text |
015 | set ocidResultsArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(strBundleID)) |
016 | set numCntArray to ocidResultsArray's |count|() as integer |
017 | repeat with itemNo from 0 to (numCntArray - 1) by 1 |
018 | set ocidRunApp to (ocidResultsArray's objectAtIndex:(itemNo)) |
019 | #通常終了を試みます |
020 | set boolDone to ocidRunApp's terminate() |
021 | if (boolDone) is true then |
022 | log strBundleID & ":正常終了" |
023 | #失敗したら |
024 | else if (boolDone) is false then |
025 | #強制終了を試みます |
026 | set boolDone to ocidRunApp's forceTerminate() |
027 | if (boolDone) is true then |
028 | log strBundleID & ":強制終了" |
029 | else if (boolDone) is false then |
030 | log strBundleID & ":終了出来ませんでした" |
031 | end if |
032 | end if |
033 | end repeat |
034 | |
035 | ##フルセットだと無い場合が多い |
036 | set recordTraits to {|Bold|:(refMe's NSBoldFontMask), |Compressed|:(refMe's NSCompressedFontMask), |Expanded|:(refMe's NSExpandedFontMask), |Condensed|:(refMe's NSCondensedFontMask), |FixedPitch|:(refMe's NSFixedPitchFontMask), |Italic|:(refMe's NSItalicFontMask), |Narrow|:(refMe's NSNarrowFontMask), |NonStandard|:(refMe's NSNonStandardCharacterSetFontMask), |Poster|:(refMe's NSPosterFontMask), |SmallCaps|:(refMe's NSSmallCapsFontMask), |Unbold|:(refMe's NSUnboldFontMask), |Unitalic|:(refMe's NSUnitalicFontMask)} as record |
037 | ##一般的なセット |
038 | # set recordTraits to {|Bold|:(refMe's NSBoldFontMask), |Compressed|:(refMe's NSCompressedFontMask), |Expanded|:(refMe's NSExpandedFontMask), |Condensed|:(refMe's NSCondensedFontMask), |FixedPitch|:(refMe's NSFixedPitchFontMask), |Italic|:(refMe's NSItalicFontMask)} as record |
039 | #DICTにして |
040 | set ocidTraitsDict to refMe's NSDictionary's alloc()'s initWithDictionary:(recordTraits) |
041 | #キー名=ファイル名 |
042 | set ocidTraitsAllKeys to ocidTraitsDict's allKeys() |
043 | #キーの数だけ繰り返し |
044 | repeat with itemTraitsAllKeys in ocidTraitsAllKeys |
045 | #キー名=ファイル名 |
046 | set strTraitsName to (itemTraitsAllKeys) as text |
047 | #TraitMask |
048 | set ocidTraitObjexct to (ocidTraitsDict's objectForKey:(itemTraitsAllKeys)) |
049 | set ocidTraits to ocidTraitObjexct |
050 | ##保存先 |
051 | set appFileManager to refMe's NSFileManager's defaultManager() |
052 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
053 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
054 | set ocidSaveDirPathURL to (ocidLibraryDirPathURL's URLByAppendingPathComponent:("FontCollections") isDirectory:(true)) |
055 | ##保存ファイル= collection |
056 | set ocidBaseFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strTraitsName) isDirectory:(false)) |
057 | set ocidSaveFilePathURL to (ocidBaseFilePathURL's URLByAppendingPathExtension:("collection")) |
058 | #ファイル名 |
059 | set ocidFileName to ocidSaveFilePathURL's lastPathComponent() |
060 | ################################ |
061 | #保存するDICTの初期化 |
062 | set ocidPlistDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0)) |
063 | ##【1】NSFontCollectionAttributes |
064 | set ocidSetCollectionDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0)) |
065 | (ocidSetCollectionDict's setValue:(strTraitsName) forKey:("NSFontCollectionName")) |
066 | (ocidSetCollectionDict's setValue:(ocidFileName) forKey:("NSFontCollectionFileName")) |
067 | (ocidPlistDict's setObject:(ocidSetCollectionDict) forKey:("NSFontCollectionAttributes")) |
068 | |
069 | ##【2】NSFontCollectionFontDescriptors |
070 | set ocidSetDescriptorsArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
071 | ##NSFontManager |
072 | set appFontManager to refMe's NSFontManager's sharedFontManager() |
073 | #Maskしたリストを取り出す |
074 | set ocidFontNameArray to (appFontManager's availableFontNamesWithTraits:(ocidTraits)) |
075 | set ocidFontFamilyArray to appFontManager's availableFontFamilies() |
076 | ##フォントの数だけ |
077 | repeat with itemFont in ocidFontNameArray |
078 | set ocidItemFont to (refMe's NSFont's fontWithName:(itemFont) |size|:(0.0)) |
079 | set ocidDisplayName to ocidItemFont's |displayName|() as text |
080 | set ocidFontName to ocidItemFont's |fontName|() as text |
081 | set ocidItemFontDiscriptor to ocidItemFont's fontDescriptor() |
082 | (ocidSetDescriptorsArray's addObject:(ocidItemFontDiscriptor)) |
083 | set ocidPostScripName to ocidItemFontDiscriptor's |postscriptName|() as text |
084 | set ocidFontName to (ocidItemFontDiscriptor's objectForKey:(refMe's NSFontNameAttribute)) as text |
085 | end repeat |
086 | (ocidPlistDict's setObject:(ocidSetDescriptorsArray) forKey:("NSFontCollectionFontDescriptors")) |
087 | ################################ |
088 | #NSKeyedArchiver初期化 |
089 | set ocidArchiverData to (refMe's NSKeyedArchiver's alloc()'s initRequiringSecureCoding:(false)) |
090 | #エンコード |
091 | (ocidArchiverData's encodeObject:(ocidPlistDict) forKey:("NSFontCollectionDictionary")) |
092 | ocidArchiverData's finishEncoding() |
093 | set ocidEncData to ocidArchiverData's encodedData() |
094 | #保存するDATA |
095 | set ocidSaveData to (refMe's NSMutableData's alloc()'s initWithCapacity:(0)) |
096 | (ocidSaveData's appendData:(ocidEncData)) |
097 | |
098 | |
099 | ################################ |
100 | #保存 |
101 | set ocidOption to (refMe's NSDataWritingAtomic) |
102 | set listDone to (ocidSaveData's writeToURL:(ocidSaveFilePathURL) options:(ocidOption) |error| :(reference)) |
103 | if (item 2 of listDone) = (missing value) then |
104 | log "writeToURL 正常処理" |
105 | else if (item 2 of listDone) ≠ (missing value) then |
106 | log (item 2 of listDone)'s code() as text |
107 | log (item 2 of listDone)'s localizedDescription() as text |
108 | return "writeToURL エラーしました" |
109 | end if |
110 | |
111 | end repeat |
112 | |
113 | delay 2 |
114 | |
115 | tell application id strBundleID to activate |
116 | |
117 | |
118 | return "Done" |
119 | |
120 | |
AppleScriptで生成しました |
| 固定リンク