« [mediaInfo] mediaInfoのHTML出力 | トップページ | [LSSharedFileList]サーバーへ接続のよく使うサーバーを追加 »

[python3]フォントのファミリー名のフォルダを作って中にフォント入れる(フォントファイル整理用)


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

#!/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 NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
set aliasDesktopDirPath to (ocidDesktopDirPathURL'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 aliasDesktopDirPath 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 ocidFontFilePathStr to (refMe's NSString's stringWithString:(strFontFilePath))
  set ocidFontFilePath to ocidFontFilePathStr's stringByStandardizingPath()
  #
  set ocidExtensionName to (ocidFontFilePath's pathExtension())
  set strExtensionName to (ocidExtensionName's lowercaseString) as text
  #
  #
  set strResponseText to ("") as text
  if ("ttf otf") contains strExtensionName then
log "単体フォントファイルです"
    ##【1】NameID=1; PlatformID=1; EncodinglD=1; LanguageID=11 日本語Mac
    set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTFont;print(TTFont('" & strFontFilePath & "')['name'].getName(1,1,1,11));\"") as text
    set strFontFamilyName to (do shell script strCommandText) as text
    if strFontFamilyName is "None" then
      ##【2】NameID=1; PlatformID=1; EncodinglD=1; LanguageID=1041 日本 Mac
      set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTFont;print(TTFont('" & strFontFilePath & "')['name'].getName(1,1,1,1041));\"") as text
      set strFontFamilyName to (do shell script strCommandText) as text
      if strFontFamilyName is "None" then
        ##【3】NameID=1; PlatformID=3; EncodinglD=1; LanguageID= 1041 日本語 Win
        set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTFont;print(TTFont('" & strFontFilePath & "')['name'].getName(1, 3, 1, 1041));\"") as text
        set strFontFamilyName to (do shell script strCommandText) as text
        if strFontFamilyName is "None" then
          ##【4】NameID=1; PlatformID=1; EncodinglD=0; LanguageID=0 英語 Mac
          set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTFont;print(TTFont('" & strFontFilePath & "')['name'].getName(1, 1, 0, 0));\"") as text
          set strFontFamilyName to (do shell script strCommandText) as text
          if strFontFamilyName is "None" then
##【5】NameID=1; PlatformID=3; EncodinglD=1; LanguageID=1033 英語 Win
set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTFont;print(TTFont('" & strFontFilePath & "')['name'].getName(1, 3, 1, 1033));\"") as text
set strFontFamilyName to (do shell script strCommandText) as text
          end if
        end if
      end if
    end if
    ################################
  else if ("otc ttc") contains strExtensionName then
log "コレクションフォントです"
    #1書体目のファミリー目を取得
    set itemIntNo to 0 as integer
    ##【1】NameID=1; PlatformID=1; EncodinglD=1; LanguageID=11 日本語Mac
    set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTCollection;print(TTCollection('" & strFontFilePath & "').fonts[" & itemIntNo & "]['name'].getName(1,1,1,11));\"") as text
    set strFontFamilyName to (do shell script strCommandText) as text
    if strFontFamilyName is "None" then
      ##【2】NameID=1; PlatformID=1; EncodinglD=1; LanguageID= 1041 日本 Mac
      set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTCollection;print(TTCollection('" & strFontFilePath & "').fonts[" & itemIntNo & "]['name'].getName(1, 1, 1, 1041));\"") as text
      set strFontFamilyName to (do shell script strCommandText) as text
      if strFontFamilyName is "None" then
        ##【4】NameID=1; PlatformID=3; EncodinglD=1; LanguageID= 1041 日本語 Win
        set strCommandText to ("\"" & strBinPath & "\" -c \"from fontTools.ttLib import TTCollection;print(TTCollection('" & strFontFilePath & "').fonts[" & itemIntNo & "]['name'].getName(1, 3, 1, 1041));\"") as text
        set strFontFamilyName to (do shell script strCommandText) as text
        if strFontFamilyName 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(1, 1, 0, 0));\"") as text
          set strFontFamilyName to (do shell script strCommandText) as text
          if strFontFamilyName 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(1, 3, 1, 1033));\"") as text
set strFontFamilyName to (do shell script strCommandText) as text
          end if
        end if
      end if
    end if
  end if
  
  
  ##
  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 ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
  #ファミリー名を追加したパス
  set ocidMakeDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:(strFontFamilyName))
  #↑のフォルダの中にフォントファイル入れるパス
  set ocidDistFilePathURL to (ocidMakeDirPathURL's URLByAppendingPathComponent:(ocidFileName))
  #フォルダを先に作って
  set ocidAttrDict to (refMe's NSMutableDictionary's alloc()'s initWithCapacity:0)
  # 777-->511 755-->493 700-->448 766-->502
(ocidAttrDict's setValue:(448) forKey:(refMe's NSFilePosixPermissions))
  set listDone to (appFileManager's createDirectoryAtURL:(ocidMakeDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference))
  #移動する
  set listDone to (appFileManager's moveItemAtURL:(ocidFilePathURL) toURL:(ocidDistFilePathURL) |error|:(reference))
  
end repeat





|

« [mediaInfo] mediaInfoのHTML出力 | トップページ | [LSSharedFileList]サーバーへ接続のよく使うサーバーを追加 »

AppleScript Fonts」カテゴリの記事