[decodeObjectOfClasses]FontBookのフォントコレクションファイルをデコード(KeyedUnarchive)する
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 | set appFileManager to refMe's NSFileManager's defaultManager() |
018 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask)) |
019 | set ocidLibraryDirPathURL to ocidURLsArray's firstObject() |
020 | set strAppendPath to ("FontCollections") as text |
021 | set ocidDefaultLocationURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:(strAppendPath) isDirectory:true |
022 | set aliasDefaultLocation to (ocidDefaultLocationURL's absoluteURL()) as alias |
023 | tell current application |
024 | set strName to name as text |
025 | end tell |
026 | ####スクリプトメニューから実行したら |
027 | if strName is "osascript" then |
028 | tell application "Finder" |
029 | activate |
030 | end tell |
031 | else |
032 | tell current application |
033 | activate |
034 | end tell |
035 | end if |
036 | ####ダイアログを出す |
037 | set listUTI to {"public.item"} as list |
038 | set aliasFilePath to (choose file with prompt "collectionファイルを選んでください" default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias |
039 | ####入力ファイルパス |
040 | set strFilePath to POSIX path of aliasFilePath |
041 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
042 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
043 | set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false) |
044 | set strExtensionName to ocidFilePathURL's pathExtension() as text |
045 | if strExtensionName is not "collection" then |
046 | tell application "Finder" |
047 | set aliasPathToMe to (path to me) as alias |
048 | end tell |
049 | return "対象外" |
050 | end if |
051 | |
052 | ################################### |
053 | #####本処理 |
054 | ################################### |
055 | # NSDataに読み込んで |
056 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
057 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error| :(reference) |
058 | if (item 2 of listResponse) = (missing value) then |
059 | log "initWithContentsOfURL 正常処理" |
060 | set ocidReadData to (item 1 of listResponse) |
061 | else if (item 2 of listResponse) ≠ (missing value) then |
062 | log (item 2 of listResponse)'s code() as text |
063 | log (item 2 of listResponse)'s localizedDescription() as text |
064 | return "initWithContentsOfURL エラーしました" |
065 | end if |
066 | |
067 | # NSKeyedUnarchiverに読み込んで |
068 | set listResponse to refMe's NSKeyedUnarchiver's alloc()'s initForReadingFromData:(ocidReadData) |error| :(reference) |
069 | if (item 2 of listResponse) = (missing value) then |
070 | log "initForReadingFromData 正常処理" |
071 | set ocidUnarchiverData to (item 1 of listResponse) |
072 | else if (item 2 of listResponse) ≠ (missing value) then |
073 | log (item 2 of listResponse)'s code() as text |
074 | log (item 2 of listResponse)'s localizedDescription() as text |
075 | return "initForReadingFromData エラーしました" |
076 | end if |
077 | # RequiresSecureCodingを指定 |
078 | ocidUnarchiverData's setRequiresSecureCoding:(true) |
079 | |
080 | #クラス |
081 | set ocidClassListArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:(0)) |
082 | (ocidClassListArray's addObject:(refMe's NSFontCollection's class)) |
083 | (ocidClassListArray's addObject:(refMe's NSFontDescriptor's class)) |
084 | (ocidClassListArray's addObject:(refMe's NSMutableDictionary's class)) |
085 | (ocidClassListArray's addObject:(refMe's NSDictionary's class)) |
086 | (ocidClassListArray's addObject:(refMe's NSMutableArray's class)) |
087 | (ocidClassListArray's addObject:(refMe's NSArray's class)) |
088 | (ocidClassListArray's addObject:(refMe's NSString's class)) |
089 | (ocidClassListArray's addObject:(refMe's NSNumber's class)) |
090 | (ocidClassListArray's addObject:(refMe's NSObject's class)) |
091 | #クラスセット |
092 | set ocidSetClass to refMe's NSSet's alloc()'s initWithArray:(ocidClassListArray) |
093 | #decodeObjectOfClassesでデコード |
094 | set ocidPlistDict to ocidUnarchiverData's decodeObjectOfClasses:(ocidSetClass) forKey:("NSFontCollectionDictionary") |
095 | ## |
096 | set ocidAttributes to (ocidPlistDict's objectForKey:("NSFontCollectionAttributes")) |
097 | log "NSFontCollectionName : " & (ocidAttributes's valueForKey:("NSFontCollectionName")) as text |
098 | log "NSFontCollectionFileName : " & (ocidAttributes's valueForKey:("NSFontCollectionFileName")) as text |
099 | ## |
100 | set ocidDescriptors to (ocidPlistDict's objectForKey:("NSFontCollectionFontDescriptors")) |
101 | repeat with itemDescript in ocidDescriptors |
102 | log "NSFontNameAttribute : " & (itemDescript's objectForKey:(refMe's NSFontNameAttribute)) as text |
103 | end repeat |
104 | |
AppleScriptで生成しました |
| 固定リンク