
【ATS-FontValidator】JSONに出力する、同時にPLISTも出力する
【Safari・FireFox用Script Editorで開く】 |
| ソース | |
|---|---|
| 001 | #!/usr/bin/env osascript |
| 002 | #coding: utf-8 |
| 003 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 004 | (* |
| 005 | |
| 006 | /System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Support/FontValidator |
| 007 | [-help -version -local -quiet -ios_only -pedantic |
| 008 | [-report -report_output OUTPUT_FILE_PATH -relative_path] |
| 009 | [FONT_FILE_OR_DIR_PATH]] |
| 010 | |
| 011 | Validates font files are adequate to use with the OS. |
| 012 | If a directory is specified, it recursively examines font files found |
| 013 | |
| 014 | -local Performs all validation in-process. Default validation is performed by a separate process |
| 015 | -quiet Suppress progress information and errors |
| 016 | -ios_only Fonts not supported by iOS are treated as errors |
| 017 | -duplicates check for duplicates |
| 018 | -dynamic perform dynamic validation (runtime checks against ATS/CT APIs) |
| 019 | -only_fonts Any file that isn't a font file will be reported as an error |
| 020 | -pedantic Reports non-font files as errors |
| 021 | -showSuccesses Report shows names of successful tests |
| 022 | -showWarnings Report shows warnings |
| 023 | -warningAsError Warning presence indicates validation failure. (-showWarnings implied) |
| 024 | -report_output Specify a file to hold the report. If not specified stdout will be used |
| 025 | -relative_path File paths in report are relative to the base path passed in. |
| 026 | Warning: this may yield similar paths if 2 input directorys contain similar directory structures |
| 027 | |
| 028 | |
| 029 | |
| 030 | com.cocolog-nifty.quicktimer.icefloe *) |
| 031 | ----+----1----+----2----+-----3----+----4----+----5----+----6----+----7-- |
| 032 | use AppleScript version "2.8" |
| 033 | use framework "Foundation" |
| 034 | use framework "AppKit" |
| 035 | use framework "UniformTypeIdentifiers" |
| 036 | use scripting additions |
| 037 | |
| 038 | property refMe : a reference to current application |
| 039 | |
| 040 | ############################# |
| 041 | # |
| 042 | set strBinPath to ("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Support/FontValidator") as text |
| 043 | ############################# |
| 044 | #ダイアログ |
| 045 | set appFileManager to refMe's NSFileManager's defaultManager() |
| 046 | set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask)) |
| 047 | set ocidDesktopDirPathURL to ocidURLsArray's firstObject() |
| 048 | set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias |
| 049 | # |
| 050 | set listUTI to {"public.font"} as list |
| 051 | set strMes to ("フォントを選択してください") as text |
| 052 | set strPrompt to ("フォントを選択してください") as text |
| 053 | try |
| 054 | tell application "SystemUIServer" |
| 055 | activate |
| 056 | set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasDefaultLocation of type listUTI with invisibles, showing package contents and multiple selections allowed) as list |
| 057 | end tell |
| 058 | on error strErrMes number numErrNo |
| 059 | log strErrMes & numErrNo |
| 060 | return false |
| 061 | end try |
| 062 | if listAliasFilePath is {} then |
| 063 | return false |
| 064 | end if |
| 065 | |
| 066 | ############################# |
| 067 | #パスArrayにする |
| 068 | set strPathList to ("") as text |
| 069 | # |
| 070 | repeat with itemAliasFilePath in listAliasFilePath |
| 071 | set aliasFilePath to itemAliasFilePath as alias |
| 072 | set strFilePath to (POSIX path of aliasFilePath) as text |
| 073 | set strFilePath to doPathEscape(strFilePath) |
| 074 | set strPathList to ("" & strPathList & " \\\"" & strFilePath & "\\\"") as text |
| 075 | end repeat |
| 076 | #ソート |
| 077 | |
| 078 | |
| 079 | ############################# |
| 080 | # |
| 081 | set strFileExtension to ("json") as text |
| 082 | set strSaveFileName to ("FontValidator." & strFileExtension & "") as text |
| 083 | # |
| 084 | set strMsg to ("保存先を指定してください") as text |
| 085 | set strPrompt to ("保存先を指定してください") as text |
| 086 | try |
| 087 | tell application "SystemUIServer" |
| 088 | activate |
| 089 | set aliasSaveFilePath to (choose file name strMsg default location aliasDefaultLocation default name strSaveFileName with prompt strPrompt) as «class furl» |
| 090 | end tell |
| 091 | on error strErrMes number numErrNo |
| 092 | return strErrMes & numErrNo |
| 093 | end try |
| 094 | set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text |
| 095 | set ocidSaveFilePath to refMe's NSString's stringWithString:(strSaveFilePath) |
| 096 | set ocidSaveFilePathURL to refMe's NSURL's fileURLWithPath:(ocidSaveFilePath) |
| 097 | set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text |
| 098 | #ダイアログで拡張子を取っちゃった時対策 |
| 099 | if strFileExtensionName is not strFileExtension then |
| 100 | set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:(strFileExtension) |
| 101 | end if |
| 102 | set ocidSaveFilePath to ocidSaveFilePathURL's |path|() |
| 103 | |
| 104 | ############################# |
| 105 | #エスケープ |
| 106 | set strBinPath to doPathEscape(strBinPath) |
| 107 | set ocidSaveFilePath to doPathEscape(ocidSaveFilePath) |
| 108 | #コマンド整形 |
| 109 | set strCmd to ("\\\"" & strBinPath & "\\\" -dynamic -local -showSuccesses -showWarnings " & strPathList & " -report_output \\\"" & ocidSaveFilePath & "\\\"") as text |
| 110 | log (return & strCmd & return) |
| 111 | set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text |
| 112 | try |
| 113 | set strResponse to (do shell script strExec) as text |
| 114 | on error |
| 115 | log "osascript でエラーしました" |
| 116 | return false |
| 117 | end try |
| 118 | ############################# |
| 119 | # |
| 120 | set ocidJsonFileName to ocidSaveFilePathURL's lastPathComponent() |
| 121 | set ocidJsonFileName to ocidJsonFileName's decomposedStringWithCanonicalMapping() |
| 122 | set ocidJsonBaseFileName to ocidJsonFileName's stringByDeletingPathExtension() |
| 123 | set strPlistFileName to (ocidJsonBaseFileName's stringByAppendingPathExtension:("plist")) as text |
| 124 | set ocidSaveDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent() |
| 125 | set ocidPlistFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strPlistFileName) isDirectory:(false) |
| 126 | |
| 127 | ############################# |
| 128 | # |
| 129 | set ocidOption to (refMe's NSDataReadingMappedIfSafe) |
| 130 | set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference) |
| 131 | if (item 2 of listResponse) = (missing value) then |
| 132 | log "A" |
| 133 | set ocidReadData to (item 1 of listResponse) |
| 134 | else if (item 2 of listResponse) ≠ (missing value) then |
| 135 | set strErrorNO to (item 2 of listResponse)'s code() as text |
| 136 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
| 137 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 138 | return "initWithContentsOfURL エラーしました" & strErrorNO & strErrorMes |
| 139 | end if |
| 140 | #JSON ルートがDICT |
| 141 | set ocidOption to (refMe's NSJSONReadingJSON5Allowed) |
| 142 | set listResponse to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidReadData) options:(ocidOption) |error|:(reference)) |
| 143 | if (item 2 of listResponse) = (missing value) then |
| 144 | log "B" |
| 145 | set ocidJsonDict to (item 1 of listResponse) |
| 146 | else if (item 2 of listResponse) ≠ (missing value) then |
| 147 | set strErrorNO to (item 2 of listResponse)'s code() as text |
| 148 | set strErrorMes to (item 2 of listResponse)'s localizedDescription() as text |
| 149 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 150 | return "JSONObjectWithData エラーしました" & strErrorNO & strErrorMes |
| 151 | end if |
| 152 | #PLIST2NSDATA(MutableContainersAndLeaves) |
| 153 | set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0) |
| 154 | set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidJsonDict) format:(ocidFormat) options:0 |error|:(reference) |
| 155 | if (first item of listResponse) is not false then |
| 156 | log "dataWithPropertyList 正常処理" |
| 157 | set ocidPlistData to (first item of listResponse) |
| 158 | else if (second item of listResponse) ≠ (missing value) then |
| 159 | set strErrorNO to (second item of listResponse)'s code() as text |
| 160 | set strErrorMes to (second item of listResponse)'s localizedDescription() as text |
| 161 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 162 | return "dataWithPropertyList エラーしました" & strErrorNO & strErrorMes |
| 163 | end if |
| 164 | #NSDATA |
| 165 | set ocidOption to (refMe's NSDataWritingAtomic) |
| 166 | set listDone to ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference) |
| 167 | if (item 1 of listDone) is true then |
| 168 | log "writeToURL 正常処理" |
| 169 | else if (second item of listDone) ≠ (missing value) then |
| 170 | set strErrorNO to (second item of listDone)'s code() as text |
| 171 | set strErrorMes to (second item of listDone)'s localizedDescription() as text |
| 172 | refMe's NSLog("■:" & strErrorNO & strErrorMes) |
| 173 | return "writeToURL エラーしました" & strErrorNO & strErrorMes |
| 174 | end if |
| 175 | |
| 176 | |
| 177 | |
| 178 | return strResponse |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | ######################## |
| 189 | #パスのエスケープ |
| 190 | to doPathEscape(strFilePath) |
| 191 | set strFilePath to strFilePath as text |
| 192 | set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath) |
| 193 | set ocidFilePath to ocidFilePathStr's stringByStandardizingPath() |
| 194 | set ocidFilePathURL to refMe's NSURL's fileURLWithPath:(ocidFilePath) isDirectory:(false) |
| 195 | set listEscChar to {"\\", "\"", "$", "`"} as list |
| 196 | repeat with itemEscChar in listEscChar |
| 197 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:(itemEscChar) withString:("\\\\\\" & itemEscChar & "")) |
| 198 | end repeat |
| 199 | set ocidFilePath to (ocidFilePath's stringByReplacingOccurrencesOfString:("!") withString:("\\\"'!'\\\"")) |
| 200 | |
| 201 | return ocidFilePath |
| 202 | end doPathEscape |
| 203 | |
| 204 | |
| AppleScriptで生成しました | |
