20260610

【ATS-FontValidator】JSONに出力する、同時にPLISTも出力する

【ATS-FontValidator】JSONに出力する、同時にPLISTも出力する

NOTE記事一覧ですnote.com
 

【Safari・FireFox用Script Editorで開く】 |

FontValidator2Plist.scpt.scpt
ソース
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
011Validates font files are adequate to use with the OS. 
012If 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. 
026Warning: this may yield similar paths if 2 input directorys contain similar directory structures
027
028
029
030com.cocolog-nifty.quicktimer.icefloe *)
031----+----1----+----2----+-----3----+----4----+----5----+----6----+----7--
032use AppleScript version "2.8"
033use framework "Foundation"
034use framework "AppKit"
035use framework "UniformTypeIdentifiers"
036use scripting additions
037
038property refMe : a reference to current application
039
040#############################
041#
042set strBinPath to ("/System/Library/Frameworks/ApplicationServices.framework/Versions/A/Frameworks/ATS.framework/Versions/A/Support/FontValidator") as text
043#############################
044#ダイアログ
045set appFileManager to refMe's NSFileManager's defaultManager()
046set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
047set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
048set aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
049#
050set listUTI to {"public.font"} as list
051set strMes to ("フォントを選択してください") as text
052set strPrompt to ("フォントを選択してください") as text
053try
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
058on error strErrMes number numErrNo
059   log strErrMes & numErrNo
060   return false
061end try
062if listAliasFilePath is {} then
063   return false
064end if
065
066#############################
067#パスArrayにする
068set strPathList to ("") as text
069#
070repeat 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
075end repeat
076#ソート
077
078
079#############################
080#
081set strFileExtension to ("json") as text
082set strSaveFileName to ("FontValidator." & strFileExtension & "") as text
083#
084set strMsg to ("保存先を指定してください") as text
085set strPrompt to ("保存先を指定してください") as text
086try
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
091on error strErrMes number numErrNo
092   return strErrMes & numErrNo
093end try
094set strSaveFilePath to (POSIX path of aliasSaveFilePath) as text
095set ocidSaveFilePath to refMe's NSString's stringWithString:(strSaveFilePath)
096set ocidSaveFilePathURL to refMe's NSURL's fileURLWithPath:(ocidSaveFilePath)
097set strFileExtensionName to ocidSaveFilePathURL's pathExtension() as text
098#ダイアログで拡張子を取っちゃった時対策
099if strFileExtensionName is not strFileExtension then
100   set ocidSaveFilePathURL to ocidSaveFilePathURL's URLByAppendingPathExtension:(strFileExtension)
101end if
102set ocidSaveFilePath to ocidSaveFilePathURL's |path|()
103
104#############################
105#エスケープ
106set strBinPath to doPathEscape(strBinPath)
107set ocidSaveFilePath to doPathEscape(ocidSaveFilePath)
108#コマンド整形
109set strCmd to ("\\\"" & strBinPath & "\\\" -dynamic  -local -showSuccesses -showWarnings " & strPathList & "  -report_output \\\"" & ocidSaveFilePath & "\\\"") as text
110log (return & strCmd & return)
111set strExec to ("/bin/zsh -c \"" & strCmd & "\"") as text
112try
113   set strResponse to (do shell script strExec) as text
114on error
115   log "osascript でエラーしました"
116   return false
117end try
118#############################
119#
120set ocidJsonFileName to ocidSaveFilePathURL's lastPathComponent()
121set ocidJsonFileName to ocidJsonFileName's decomposedStringWithCanonicalMapping()
122set ocidJsonBaseFileName to ocidJsonFileName's stringByDeletingPathExtension()
123set strPlistFileName to (ocidJsonBaseFileName's stringByAppendingPathExtension:("plist")) as text
124set ocidSaveDirPathURL to ocidSaveFilePathURL's URLByDeletingLastPathComponent()
125set ocidPlistFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strPlistFileName) isDirectory:(false)
126
127#############################
128#
129set ocidOption to (refMe's NSDataReadingMappedIfSafe)
130set listResponse to refMe's NSData's alloc()'s initWithContentsOfURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference)
131if (item 2 of listResponse) = (missing value) then
132   log "A"
133   set ocidReadData to (item 1 of listResponse)
134else 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
139end if
140#JSON ルートがDICT
141set ocidOption to (refMe's NSJSONReadingJSON5Allowed)
142set listResponse to (refMe's NSJSONSerialization's JSONObjectWithData:(ocidReadData) options:(ocidOption) |error|:(reference))
143if (item 2 of listResponse) = (missing value) then
144   log "B"
145   set ocidJsonDict to (item 1 of listResponse)
146else 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
151end if
152#PLIST2NSDATA(MutableContainersAndLeaves)
153set ocidFormat to (refMe's NSPropertyListXMLFormat_v1_0)
154set listResponse to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidJsonDict) format:(ocidFormat) options:0 |error|:(reference)
155if (first item of listResponse) is not false then
156   log "dataWithPropertyList 正常処理"
157   set ocidPlistData to (first item of listResponse)
158else 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
163end if
164#NSDATA
165set ocidOption to (refMe's NSDataWritingAtomic)
166set listDone to ocidPlistData's writeToURL:(ocidPlistFilePathURL) options:(ocidOption) |error|:(reference)
167if (item 1 of listDone) is true then
168   log "writeToURL 正常処理"
169else 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
174end if
175
176
177
178return strResponse
179
180
181
182
183
184
185
186
187
188########################
189#パスのエスケープ
190to 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
202end doPathEscape
203
204
AppleScriptで生成しました