« [python3]フォント情報をHTMLで表示する | トップページ | QRバーコードデコード (QRコードファイルの内容をテキストにする) ちょっと修正 »

[Font]postscript名を取得する(python利用)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# python3を利用します
# fontToolsがインストールされていない場合インストールします
# インストールしたくない人は実行しないでください
#com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "UniformTypeIdentifiers"
use framework "AppKit"
use scripting additions

property refMe : a reference to current application

set appFileManager to refMe's NSFileManager's defaultManager()

###python3のパスチェック
try
  set strCmdText to ("/usr/bin/which python3") as text
  set strBinPath to (do shell script strCmdText) as text
on error
return "python3が見つかりませんでした終了します"
end try
###fonttoolのインストールチェック
set strCmdText to ("\"" & strBinPath & "\" -m pip show fontTools") as text
try
  set strPipDone to (do shell script strCmdText) as text
on error
  try
    set strCmdText to ("/bin/mkdir -p $HOME/Library/Caches/pip") as text
do shell script strCmdText
    set strCmdText to ("/bin/chmod 777 $HOME/Library/Caches/pip") as text
do shell script strCmdText
    set strCmdText to ("/bin/mkdir -p $HOME/Library/Python/3.9/lib/python/site-packages") as text
do shell script strCmdText
    set strCmdText to ("/bin/chmod 755 $HOME/Library/Python/3.9/lib/python/site-packages") as text
do shell script strCmdText
  end try
  ##インストールされていなければインストールする
  set strCmdText to ("\"" & strBinPath & "\" -m pip install --user fontTools") as text
  set strPipDone to (do shell script strCmdText) as text
end try

###ダイアログを前面に出す
set strName to (name of current application) as text
if strName is "osascript" then
  tell application "Finder" to activate
else
  tell current application to activate
end if
# デフォルトロケーション
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidLibraryDirPathURL to ocidURLsArray's firstObject()
set ocidFontsDirPathURL to ocidLibraryDirPathURL's URLByAppendingPathComponent:("Fonts")
set aliasFontsDirPath to (ocidFontsDirPathURL's absoluteURL()) as alias
#UTIリスト
set listUTI to {"public.opentype-font", "public.truetype-ttf-font", "public.truetype-collection-font", "public.opentype-collection-font"} as list
#メッセージ
set strMes to ("フォント・ファイルを選んでください") as text
set strPrompt to ("フォント・ファイルを選んでください") as text
try
  #ファイル選択
  set listAliasFilePath to (choose file strMes with prompt strPrompt default location aliasFontsDirPath of type listUTI with invisibles, multiple selections allowed and showing package contents) as list
on error
log "エラーしました"
return "エラーしました"
end try
if listAliasFilePath is {} then
return "選んでください"
end if


repeat with itemAliasFilePath in listAliasFilePath
  set strFontFilePath to (POSIX path of itemAliasFilePath) as text
  ##
  set ocidFilePathStr to (refMe's NSString's stringWithString:(strFontFilePath))
  set ocidFilePath to ocidFilePathStr's stringByStandardizingPath()
  set ocidFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFilePath) isDirectory:false)
  set ocidFileName to ocidFilePathURL's lastPathComponent()
  set ocidExtensionName to ocidFilePathURL's pathExtension()
  set strExtensionName to (ocidExtensionName's lowercaseString) as text
  #
  set strDefaultAnswer to ("") as text
  if ("ttf otf") contains strExtensionName then
log "単体フォントファイルです"
    ##【2】NameID=6
    set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTFont;print(TTFont('" & strFontFilePath & "')['name'].getDebugName(6));\"") as text
    set strPostScriptName to (do shell script strCommandText) as text
    if strPostScriptName is "None" then
      ##【2】NameID=6; PlatformID=1; EncodinglD=0; LanguageID=0 英語 Mac
      set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTFont;print(TTFont('" & strFontFilePath & "')['name'].getName(6, 1, 0, 0));\"") as text
      set strPostScriptName to (do shell script strCommandText) as text
    end if
    if strPostScriptName is "None" then
      ##【3】NameID=6; PlatformID=3; EncodinglD=1; LanguageID=1033 英語 Win
      set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTFont;print(TTFont('" & strFontFilePath & "')['name'].getName(6, 3, 1, 1033));\"") as text
      set strPostScriptName to (do shell script strCommandText) as text
    end if
    if strPostScriptName is "None" then
      set strDefaultAnswer to "取得に失敗しました"
    else
      set strDefaultAnswer to (strPostScriptName) as text
    end if
    ################################
  else if ("otc ttc") contains strExtensionName then
log "コレクションフォントです"
    ##同封されている書体数を数えて
    set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTCollection;print(len(TTCollection('" & strFontFilePath & "').fonts));\"") as text
    set numCntFont to (do shell script strCommandText) as integer
    
    repeat with itemIntNo from 0 to (numCntFont - 1) by 1
      ##【1】getDebugName
      set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTCollection;print(TTCollection('" & strFontFilePath & "').fonts[" & itemIntNo & "]['name'].getDebugName(6));\"") as text
      set strPostScriptName to (do shell script strCommandText) as text
      if strPostScriptName is "None" then
        ##【3】NameID=1; PlatformID=1; EncodinglD=0; LanguageID=0 英語 Mac
        set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTCollection;print(TTCollection('" & strFontFilePath & "').fonts[" & itemIntNo & "]['name'].getName(6, 1, 0, 0));\"") as text
        set strPostScriptName to (do shell script strCommandText) as text
      end if
      if strPostScriptName is "None" then
        ##【5】NameID=1; PlatformID=3; EncodinglD=1; LanguageID=1033 英語 Win
        set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTCollection;print(TTCollection('" & strFontFilePath & "').fonts[" & itemIntNo & "]['name'].getName(6, 3, 1, 1033));\"") as text
        set strPostScriptName to (do shell script strCommandText) as text
      end if
      if strPostScriptName is "None" then
        set strPostScriptName to "取得に失敗しました"
      end if
      set strDefaultAnswer to (strDefaultAnswer & strPostScriptName & "\n") as text
    end repeat
    
    
  end if
  ###ダイアログ
  #####ダイアログを前面に
  tell current application
    set strName to name as text
  end tell
  ####スクリプトメニューから実行したら
  if strName is "osascript" then
    tell application "Finder" to activate
  else
    tell current application to activate
  end if
  set strSetValue to ("ID=6 PostScript Name名 戻り値です\n" & strFontFilePath) as text
  set aliasIconPath to (POSIX file "/System/Applications/Font Book.app/Contents/Resources/AppIcon.icns") as alias
  set recordResult to (display dialog strSetValue with title "PostScriptName" default answer strDefaultAnswer buttons {"クリップボードにコピー", "キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 20 with icon aliasIconPath without hidden answer)
  ###クリップボードコピー
  if button returned of recordResult is "クリップボードにコピー" then
    set strText to text returned of recordResult as text
    ####ペーストボード宣言
    set appPasteboard to refMe's NSPasteboard's generalPasteboard()
    set ocidText to (refMe's NSString's stringWithString:(strText))
appPasteboard's clearContents()
(appPasteboard's setString:(ocidText) forType:(refMe's NSPasteboardTypeString))
  end if
  
  
end repeat










|

« [python3]フォント情報をHTMLで表示する | トップページ | QRバーコードデコード (QRコードファイルの内容をテキストにする) ちょっと修正 »

AppleScript Python」カテゴリの記事

AppleScript Fonts」カテゴリの記事