20260319

【cpdf】PDFドキュメント 各PDFページに埋め込まれているフォント情報を一覧出力する


【CPDF】PDFドキュメント 各PDFページに埋め込まれているフォント情報を一覧出力する


ソースのダウンロードはこちら

【スクリプトエディタで開く】 |

[cpdf]PDF FontList.scpt
ソース
001#!/usr/bin/env osascript
002#coding: utf-8
003----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
004(*
005cpdfが別途必要です
006<https://github.com/coherentgraphics/cpdf-binaries>
007 
008PDFの埋め込みフォントリストを出力します
009cpdfインストーラー同封版
010cpdfが未インストールの場合インストールします
011困る場合は実行しないでください
012cpdfインストール先は
013/Users/ユーザーID/Library/Application Support/bin/cpdf
014
015v1 初回作成
016v1.1 インストール機能をつけた
017
018com.cocolog-nifty.quicktimer.icefloe *)
019----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
020use AppleScript version "2.8"
021use framework "Foundation"
022use framework "UniformTypeIdentifiers"
023use framework "AppKit"
024use scripting additions
025property refMe : a reference to current application
026##################
027#cpdfバイナリーのパス
028set strResponse to doChkInstall("cpdf")
029if strResponse is false then
030   set strBinPath to ("~/Library/Application Support/bin/cpdf/cpdf") as text
031else
032   set strBinPath to strResponse as text
033end if
034set ocidBinPathStr to refMe's NSString's stringWithString:(strBinPath)
035set ocidBinPath to ocidBinPathStr's stringByStandardizingPath()
036set ocidBinFilePathURL to refMe's NSURL's alloc()'s initFileURLWithPath:(ocidBinPath) isDirectory:(false)
037#メタ文字
038set strLF to ("" & linefeed & "") as text
039set strCR to ("" & return & "") as text
040set strCRLF to ("" & return & linefeed & "") as text
041set strSpace to ("" & space & "") as text
042set strTab to ("" & tab & "") 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 aliasDesktopDirPath to (ocidDesktopDirPathURL's absoluteURL()) as alias
049set listUTI to {"com.adobe.pdf"} as list
050set strMes to ("PDFファイルを選んでください") as text
051set strPrompt to ("" & strCR & "PDFファイルを選んでください" & strCR & "PDFに埋め込まれているフォント名" & strCR & "サブセット名をリスト出力します" & strCR & "") as text
052try
053   tell application "SystemUIServer"
054      activate
055      set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDesktopDirPath) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
056   end tell
057on error strErrMsg number numErrNo
058   log "No: " & numErrNo & strLF & "Error:" & strErrMsg
059   return false
060end try
061##################
062#インストールチェック
063set boolExist to appFileManager's fileExistsAtPath:(ocidBinPath) isDirectory:(false)
064if boolExist is false then
065   set boolDone to doInstall()
066   if boolDone is false then
067      log "インストールに失敗しました"
068      return false
069   end if
070end if
071##################
072#入力ファイルパス
073set strFilePath to (POSIX path of aliasFilePath) as text
074set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
075set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
076set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
077set ocidFileName to ocidFilePathURL's lastPathComponent()
078##################
079#出力用テキスト
080set ocidOutputString to refMe's NSMutableString's alloc()'s init()
081ocidOutputString's appendString:("FontList")
082ocidOutputString's appendString:(strLF)
083ocidOutputString's appendString:(ocidFileName)
084ocidOutputString's appendString:(strLF)
085ocidOutputString's appendString:(ocidFilePath)
086ocidOutputString's appendString:(strLF & strLF)
087##################
088#コマンド整形
089set strFilePath to (ocidFilePathURL's |path|()) as text
090set strBinPath to (ocidBinFilePathURL's |path|()) as text
091#
092set strCmd to ("'" & strBinPath & "' -list-fonts '" & strFilePath & "'") as text
093ocidOutputString's appendString:(strCmd)
094ocidOutputString's appendString:(strLF & strLF)
095set strResponse to (do shell script strCmd) as text
096if strResponse is "" then
097   ocidOutputString's appendString:("埋め込みフォントなし")
098   ocidOutputString's appendString:(strLF & strLF)
099else
100   ocidOutputString's appendString:(strResponse)
101   ocidOutputString's appendString:(strLF & strLF)
102end if
103#
104set strCmd to ("'" & strBinPath & "' -missing-fonts '" & strFilePath & "'") as text
105ocidOutputString's appendString:(strCmd)
106ocidOutputString's appendString:(strLF & strLF)
107set strResponse to (do shell script strCmd) as text
108if strResponse is "" then
109   ocidOutputString's appendString:("エラーフォントなし")
110   ocidOutputString's appendString:(strLF & strLF)
111else
112   ocidOutputString's appendString:(strResponse)
113   ocidOutputString's appendString:(strLF & strLF)
114end if
115
116##################
117#保存ダイアログ
118set boolDone to doSaveDialog(ocidOutputString, ocidFilePathURL)
119try
120   tell application "System Events" to quit
121end try
122return true
123
124##############################
125#保存ダイアログ
126to doSaveDialog(argStringM, argFilePathURL)
127   #
128   set strLF to ("" & linefeed & "") as text
129   set strCR to ("" & return & "") as text
130   set strCRLF to ("" & return & linefeed & "") as text
131   set strSpace to ("" & space & "") as text
132   set strTab to ("" & tab & "") as text
133   #
134   set ocidFileName to argFilePathURL's lastPathComponent()
135   set ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
136   set ocidContainerDirPathURL to argFilePathURL's URLByDeletingLastPathComponent()
137   
138   
139   #ダイアログ用のテキスト取得(Text acquisition for dialogs)
140   set appFineder to refMe's NSBundle's bundleWithIdentifier:("com.apple.finder")
141   #バンドルID指定でダメだったら
142   if appFineder = (missing value) then
143      #NSURL指定でバンドルを読み出す
144      set strAppFilePath to "/System/Library/CoreServices/Finder.app" as text
145      set ocidAppFilePathStr to refMe's NSString's stringWithString:(strAppFilePath)
146      set ocidAppFilePath to ocidAppFilePathStr's stringByStandardizingPath()
147      set ocidAppFilePathURL to refMe's NSURL's fileURLWithPath:(ocidAppFilePath) isDirectory:(false)
148      set appFineder to refMe's NSBundle's bundleWithURL:(ocidAppFilePathURL)
149   end if
150   set strCancel to (appFineder's localizedStringForKey:("AL1") value:("Cancel") table:("LocalizableMerged")) as text
151   set strOK to (appFineder's localizedStringForKey:("AL4") value:("OK") table:("LocalizableMerged")) as text
152   set strAddText to (appFineder's localizedStringForKey:("BR3") value:("Add Text") table:("LocalizableMerged")) as text
153   set strClipboard to (appFineder's localizedStringForKey:("CW10") value:("Clipboard") table:("LocalizableMerged")) as text
154   set strCopy to (appFineder's localizedStringForKey:("ME2") value:("Copy") table:("LocalizableMerged")) as text
155   set strText to (appFineder's localizedStringForKey:("GROUP_TEXT") value:("Text") table:("LocalizableMerged")) as text
156   set strQuit to (appFineder's localizedStringForKey:("BN39") value:("Quit Without Saving") table:("LocalizableMerged")) as text
157   set strClose to (appFineder's localizedStringForKey:("FR26") value:("Close") table:("LocalizableMerged")) as text
158   set strFinish to (appFineder's localizedStringForKey:("FR27") value:("Done") table:("LocalizableMerged")) as text
159   set strCopyButton to ("" & strCopy & strClipboard & "") as text
160   set ocidSave to appFineder's localizedStringForKey:("AL2") value:("Save") table:("LocalizableMerged")
161   set ocidExists to appFineder's localizedStringForKey:("NE73") value:("An item with the same name already exists in this location.") table:("LocalizableMerged")
162   set ocidExtensionName to appFineder's localizedStringForKey:("RV_FILE_EXTENSION_SLICE_NAME") value:("File extension slice name") table:("LocalizableMerged")
163   #
164   set strBundlePath to ("/System/Library/ScriptingAdditions/StandardAdditions.osax") as text
165   set ocidBundlePathStr to refMe's NSString's stringWithString:(strBundlePath)
166   set ocidBundlePath to ocidBundlePathStr's stringByStandardizingPath()
167   set ocidBundlePathURL to refMe's NSURL's fileURLWithPath:(ocidBundlePath) isDirectory:(false)
168   set appBundle to refMe's NSBundle's bundleWithURL:(ocidBundlePathURL)
169   set ocidChoose to appBundle's localizedStringForKey:("Choose") value:("Choose") table:("Localizable")
170   set ocidOK to appBundle's localizedStringForKey:("OK") value:("OK") table:("Localizable")
171   set ocidOpen to appBundle's localizedStringForKey:("Open") value:("Open") table:("Localizable")
172   set ocidCancel to appBundle's localizedStringForKey:("Cancel") value:("Cancel") table:("Localizable")
173   set ocidChooseFile to appBundle's localizedStringForKey:("Choose a File") value:("Choose a File") table:("Localizable")
174   set ocidChooseFileName to appBundle's localizedStringForKey:("Choose File Name") value:("Choose File Name") table:("Localizable")
175   set ocidChooseFolder to appBundle's localizedStringForKey:("Choose a Folder") value:("Choose a Folder") table:("Localizable")
176   set ocidSaveAs to appBundle's localizedStringForKey:("Save As") value:("Save As") table:("Localizable")
177   set ocidNameAndLocation to appBundle's localizedStringForKey:("Specify new file name and location") value:("Specify new file name and location") table:("Localizable")
178   #
179   set strBundlePath to ("/System/Applications/TextEdit.app") as text
180   set ocidBundlePathStr to refMe's NSString's stringWithString:(strBundlePath)
181   set ocidBundlePath to ocidBundlePathStr's stringByStandardizingPath()
182   set ocidBundlePathURL to refMe's NSURL's fileURLWithPath:(ocidBundlePath) isDirectory:(false)
183   set appBundle to refMe's NSBundle's bundleWithURL:(ocidBundlePathURL)
184   set ocidLineNumber to appBundle's localizedStringForKey:("100016.placeholderString") value:("Line number") table:("SelectLinePanel")
185   set ocidInsert to appBundle's localizedStringForKey:("598.title") value:("Insert") table:("Edit")
186   set ocidUseNone to appBundle's localizedStringForKey:("262.title") value:("Use None") table:("Edit")
187   ####
188   set appFileManager to refMe's NSFileManager's defaultManager()
189   set aliasIconPath to (POSIX file "/System/Applications/TextEdit.app/Contents/Resources/AppIcon.icns") as alias
190   set strTitle to (strFinish) as text
191   set strMes to ("" & strFinish & strLF & strCopyButton & "") as text
192   
193   set strSaveAs to ocidSaveAs as text
194   set listButtons to {strSaveAs, strCopyButton, strQuit} as list
195   try
196      tell application "System Events"
197         activate
198         set recordResult to (display dialog strMes with title strTitle default answer (argStringM as text) buttons listButtons default button strQuit giving up after 30 with icon aliasIconPath without hidden answer)
199      end tell
200   on error strErrMsg number numErrNo
201      log "No: " & numErrNo & strLF & "Error:" & strErrMsg
202      return false
203   end try
204   if (gave up of recordResult) is true then
205      return false
206   else if (button returned of recordResult) is strQuit then
207      return true
208   else if (button returned of recordResult) is strCopyButton then
209      set strReturnedText to (text returned of recordResult) as text
210      set boolDone to doSendPasteboard(strReturnedText) as boolean
211      if boolDone is false then
212         return false
213      end if
214   else if (button returned of recordResult) is strSaveAs then
215      set strSaveFileName to ("" & ocidBaseFileName & "-FontList.txt") as text
216      set strTargetExtension to ("txt") as text
217      #デフォルトの保存先 PDFと同じ階層
218      set aliasContainerDirPath to (ocidContainerDirPathURL's absoluteURL()) as alias
219      set strMsg to ("" & ocidChooseFileName & return & ocidNameAndLocation & "") as text
220      set strPrompt to ("" & ocidChooseFileName & return & ocidNameAndLocation & "") as text
221      try
222         tell application "SystemUIServer"
223            activate
224            set aliasFilePath to (choose file name strMsg with prompt strPrompt default location aliasContainerDirPath default name strSaveFileName) as «class furl»
225         end tell
226      on error strErrMes number numErrNo
227         log strErrMes & numErrNo
228         return false
229      end try
230      #保存先
231      set strFilePath to (POSIX path of aliasFilePath) as text
232      set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
233      set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
234      set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
235      set ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
236      #拡張子
237      set strExtension to (ocidFilePathURL's pathExtension()) as text
238      #最後のアイテムがファイル名
239      set strFileName to (ocidFilePathURL's lastPathComponent()) as text
240      #拡張子のつけ忘れ対策
241      if strFileName does not contain strTargetExtension then
242         set strFileName to (strFileName & "." & strTargetExtension) as text
243         set ocidFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strFileName)
244      end if
245      set listDone to argStringM's writeToURL:(ocidFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
246   else
247      log (text returned of recordResult) as text
248   end if
249   
250   return true
251end doSaveDialog
252
253########################
254#クリップボードにコピー(Copy to clipboard)
255on doSendPasteboard(argText)
256   set appPasteboard to refMe's NSPasteboard's generalPasteboard()
257   set ocidText to (refMe's NSString's stringWithString:(argText))
258   appPasteboard's clearContents()
259   set boolDone to appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString)
260   if boolDone is false then
261      try
262         tell application "Finder"
263            set the clipboard to argText as text
264         end tell
265      on error
266         return false
267      end try
268   else if boolDone is true then
269      return true
270   end if
271end doSendPasteboard
272##############################
273#すでにインストール済みで
274#ユーザーがRC設定している場合は
275#パスが通るのでこれがエラーにならない
276to doChkInstall(argBinName)
277   try
278      set strCmd to ("/bin/zsh -c 'where " & argBinName & "'")
279      set strResponse to (do shell script strCmd) as text
280      return strResponse
281   on error strErrMsg number numErrNo
282      return false
283   end try
284end doChkInstall
285##############################
286to doQuitSelf()
287   try
288      tell application "System Events" to quit
289   end try
290   set strOutputString to (missing value)
291   set listAliasFilePath to (missing value)
292   set listBundleID to {"com.apple.automator.xpc.runner", "com.apple.automator.xpc.workflowServiceRunner"} as list
293   repeat with itemBundleID in listBundleID
294      set ocidAppArray to (refMe's NSRunningApplication's runningApplicationsWithBundleIdentifier:(itemBundleID))
295      repeat with itemApp in ocidAppArray
296         try
297            set boolDone to itemApp's terminate()
298         end try
299      end repeat
300   end repeat
301   return true
302end doQuitSelf
303
304##############################
305#インストールサプ
306to doInstall()
307   ##############################
308   #設定項目
309   #ダウンロードURL
310   set strURL to ("https://codeload.github.com/coherentgraphics/cpdf-binaries/zip/refs/heads/master") as text
311   #インストール先
312   set strSaveDir to ("~/Library/Application Support/bin/cpdf") as text
313   ##############################
314   #メタ文字
315   set strLF to ("" & linefeed & "") as text
316   set strCR to ("" & return & "") as text
317   set strCRLF to ("" & return & linefeed & "") as text
318   set strSpace to ("" & space & "") as text
319   set strTab to ("" & tab & "") as text
320   ##############################
321   #ダウンロード先(起動時に削除される項目)
322   set appFileManager to refMe's NSFileManager's defaultManager()
323   set ocidTempDirURL to appFileManager's temporaryDirectory()
324   set ocidUUID to refMe's NSUUID's alloc()'s init()
325   set ocidUUIDString to ocidUUID's UUIDString
326   set ocidDownloadDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
327   set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
328   ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
329   set listDone to appFileManager's createDirectoryAtURL:(ocidDownloadDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
330   set ocidDownloadFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("master.zip") isDirectory:(false)
331   set strDownloadFilePath to (ocidDownloadFilePathURL's |path|()) as text
332   set strExpandDirPath to (ocidDownloadDirPathURL's |path|()) as text
333   ##############################
334   #保存先 最終的なインストール先
335   set ocidSaveDirPathStr to refMe's NSString's stringWithString:(strSaveDir)
336   set ocidSaveDirPath to ocidSaveDirPathStr's stringByStandardizingPath()
337   set ocidSaveDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidSaveDirPath) isDirectory:false)
338   
339   ##############################
340   #今あるものをゴミ箱へ
341   set boolDirExists to appFileManager's fileExistsAtPath:(ocidSaveDirPath) isDirectory:(true)
342   if (boolDirExists as boolean) is true then
343      set appFileManager to refMe's NSFileManager's defaultManager()
344      set listDone to (appFileManager's trashItemAtURL:(ocidSaveDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference))
345   end if
346   
347   ##############################
348   #新たにフォルダを作り直す
349   ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions)
350   set listDone to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
351   ##############################
352   #リダイレクト先
353   set strCmd to ("/usr/bin/curl -s -L -I -o /dev/null -w '%{url_effective}' \"" & strURL & "\"") as text
354   try
355      set strRedirectURL to (do shell script strCmd) as text
356   on error strErrMsg number numErrNo
357      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
358      return false
359   end try
360   ##############################
361   #ダウンロード
362   set strCmd to ("/usr/bin/curl -L -o \"" & strDownloadFilePath & "\" \"" & strRedirectURL & "\" --connect-timeout 20") as text
363   try
364      set strResponse to (do shell script strCmd) as text
365   on error strErrMsg number numErrNo
366      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
367      return false
368   end try
369   ##############################
370   #解凍
371   set strCmd to ("/usr/bin/bsdtar -xzf \"" & strDownloadFilePath & "\" -C \"" & strExpandDirPath & "\"") as text
372   try
373      set strResponse to (do shell script strCmd) as text
374   on error strErrMsg number numErrNo
375      log "No: " & numErrNo & linefeed & "Error:" & strErrMsg
376      return false
377   end try
378   ##############################
379   #解凍結果
380   set reocdSystemInfot to (system info) as record
381   set strCPU to (CPU type of reocdSystemInfot) as text
382   #CPUで分岐
383   if strCPU contains "ARM" then
384      #ARM
385      set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-ARM/cpdf") isDirectory:(false)
386      
387   else
388      #INTEL
389      set ocidExpandBinFilePathURL to ocidDownloadDirPathURL's URLByAppendingPathComponent:("cpdf-binaries-master/OSX-Intel/cpdf") isDirectory:(false)
390      
391   end if
392   
393   #最終的なバイナリーの移動先
394   set ocidDistBinFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:("cpdf") isDirectory:(false)
395   #移動
396   set listDone to (appFileManager's moveItemAtURL:(ocidExpandBinFilePathURL) toURL:(ocidDistBinFilePathURL) |error|:(reference))
397   ##############################
398   #解凍前のファイルをゴミ箱へ
399   set appFileManager to refMe's NSFileManager's defaultManager()
400   set listDone to (appFileManager's trashItemAtURL:(ocidDownloadDirPathURL) resultingItemURL:(ocidDownloadDirPathURL) |error|:(reference))
401   
402   return true
403   
404end doInstall
AppleScriptで生成しました