« [FontBook]新規ライブラリを作成(内容は空) | トップページ | [FontBook]ライブラリにフォントを登録する(途中 現在不具合あり) »

[FontBook]ライブラリにMicrosoft Officeのフォントを登録する

Screencapture-20230612-155926

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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application


###フォントブックを終了
tell application id "com.apple.FontBook"
  quit
end tell

##############################
###ライブラリ名を指定 ダイアログ
set appFileManager to refMe's NSFileManager's defaultManager()
###FontCollectionsのフォルダパス
set ocidUserLibraryPathArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidUserLibraryPath to ocidUserLibraryPathArray's firstObject()
set ocidFontCollectionsURL to ocidUserLibraryPath's URLByAppendingPathComponent:("FontCollections") isDirectory:(true)
set aliasFontCollectionsURL to ocidFontCollectionsURL's absoluteURL() as alias
##############################
#####ダイアログを前面に
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 strDefaultName to "Microsoft Office.library" as text
set strExtension to "library"
set strPromptText to "フォントライブラリの名前を決めてください" as text
set strMesText to "フォントライブラリの名前を決めてください" as text
set aliasFilePath to (choose file name strMesText default location aliasFontCollectionsURL default name strDefaultName with prompt strPromptText) as «class furl»
######
set strFilePath to (POSIX path of aliasFilePath) as text
set ocidFilePathStr to refMe's NSString's stringWithString:(strFilePath)
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 ocidBaseFileName to ocidFileName's stringByDeletingPathExtension()
set ocidExtensionName to ocidFilePathURL's pathExtension()
###空の文字列 if文用
set ocidEmptyString to refMe's NSString's alloc()'s init()
if ocidExtensionName = ocidEmptyString then
  ###ダイアログで拡張子取っちゃった場合対策
  set ocidFilePathURL to ocidFilePathURL's URLByAppendingPathExtension:(strExtension)
end if
##############################
##### フォント選択
set strFontsDirPath to "/Applications/Microsoft Word.app/Contents/Resources/DFonts"
set ocidFontsDirPathStr to refMe's NSString's stringWithString:(strFontsDirPath)
set ocidFontsDirPath to ocidFontsDirPathStr's stringByStandardizingPath()
set ocidFontsDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFontsDirPath) isDirectory:false)
###ダイアログ
set aliasFontCollectionsURL to ocidFontsDirPathURL's absoluteURL() as alias
set listUTI to {"public.font", "public.opentype-collection-font", "public.opentype-font", "public.truetype-collection-font", "public.truetype-ttf-font"}
set listAliasFontPath to (choose file with prompt "フォントファイルを選んでください" default location aliasFontCollectionsURL of type listUTI with invisibles, multiple selections allowed and showing package contents) as list

###URL格納用(absoluteString)
set ocidFontPathArray to refMe's NSMutableArray's alloc()'s initWithCapacity:0
repeat with itemAliasFontPath in listAliasFontPath
  set aliasFontFilePath to itemAliasFontPath as alias
  set strFontFilePath to POSIX path of aliasFontFilePath as text
  set ocidFontFilePathStr to (refMe's NSString's stringWithString:(strFontFilePath))
  set ocidFontFilePath to ocidFontFilePathStr's stringByStandardizingPath()
  set ocidFontFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidFontFilePath) isDirectory:false)
(ocidFontPathArray's addObject:(ocidFontFilePathURL))
end repeat


######################
###PLISTのデータになるDict
set ocidPlistDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###bookmarks
set ocidBookmarks to refMe's NSMutableDictionary's alloc()'s init()
ocidPlistDict's setObject:(ocidBookmarks) forKey:("bookmarks")
###containerURLs
set ocidContainerURLs to refMe's NSMutableArray's alloc()'s initWithCapacity:0
###フォントのURLを追加(absoluteString形式)
repeat with itemFontPathArray in ocidFontPathArray
  set strAbsoluteStringPath to (itemFontPathArray's absoluteString())
(ocidContainerURLs's addObject:(strAbsoluteStringPath))
end repeat

ocidPlistDict's setObject:(ocidContainerURLs) forKey:("containerURLs")
###disabled
set ocidFalse to (refMe's NSNumber's numberWithBool:false)
ocidPlistDict's setValue:(ocidFalse) forKey:("disabled")
###name
ocidPlistDict's setValue:(ocidBaseFileName) forKey:("name")
###reference
set ocidReference to refMe's NSNumber's numberWithInteger:(1)
ocidPlistDict's setValue:(ocidReference) forKey:("reference")
###date
set ocidDate to (refMe's NSDate's |date|())
set ocidFormatter to refMe's NSDateFormatter's alloc()'s init()
ocidFormatter's setDateFormat:("yyyy-MM-dd'T'HH:mm:ss'Z'")
ocidFormatter's setTimeZone:(refMe's NSTimeZone's timeZoneWithAbbreviation:("UTC"))
set ocidSetDate to ocidFormatter's stringFromDate:(ocidDate)
-->string形式でセットするのでsetValueで
ocidPlistDict's setValue:(ocidSetDate) forKey:("date")
###urlAddedDates
set ocidUrlAddedDates to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
###フォントのURLを追加(absoluteString形式)
repeat with itemFontPathArray in ocidFontPathArray
  set strPath to (itemFontPathArray's |path|())
(ocidUrlAddedDates's setValue:(ocidSetDate) forKey:(strPath))
end repeat

ocidPlistDict's setObject:(ocidUrlAddedDates) forKey:("urlAddedDates")


###version
ocidFormatter's setDateFormat:("yyyyMMdd")
set ocidversion to ocidFormatter's stringFromDate:(ocidDate)
ocidPlistDict's setValue:(ocidversion) forKey:("version")

######################
###XML形式
set ocidXmlplist to refMe's NSPropertyListXMLFormat_v1_0
####書き込み用にXMLに変換
set ocidPlistEditData to refMe's NSPropertyListSerialization's dataWithPropertyList:(ocidPlistDict) format:(ocidXmlplist) options:0 |error|:(missing value)
####書き込み
ocidPlistEditData's writeToURL:(ocidFilePathURL) atomically:true


###フォントブックを起動
tell application id "com.apple.FontBook"
  launch
end tell





|

« [FontBook]新規ライブラリを作成(内容は空) | トップページ | [FontBook]ライブラリにフォントを登録する(途中 現在不具合あり) »

AppleScript Fonts」カテゴリの記事