« [Python]PDFのクリエーター情報の削除 | トップページ | TSV行列入替 »

フォントの文字イメージを画像に出力します(少し変更した)

ダウンロード - font2imagepy2.zip


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
com.cocolog-nifty.quicktimer.icefloe
v2少し手を入れた
OSにインストールしていないフォントでも描画できます
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use framework "UniformTypeIdentifiers"
use scripting additions

property refMe : a reference to current application

on run
  ## python3のパスチェック
  if (doChkPython3()) is false then
return "python3が見つかりませんでした終了します"
  end if
  ## pythonスクリプトのパス
  set strPyFileName to ("Font2Image.py") as text
  set ocidPyFilePathURL to doGetScriptPath(strPyFileName)
  if ocidPyFilePathURL is false then
return "pythonスクリプトが見つかりませんでした処理を終了します"
  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 appFileManager to refMe's NSFileManager's defaultManager()
  # NSLocalDomainMask NSUserDomainMask NSSystemDomainMask
  set ocidDirPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSLibraryDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidDirPathURL to ocidDirPathURLArray's firstObject()
  set ocidFontsDirPathURL to ocidDirPathURL's URLByAppendingPathComponent:("Fonts")
  set aliasFontsDirPath to (ocidFontsDirPathURL's absoluteURL()) as alias
  
  set listChooseFileUTI to {"public.font"}
  set strPromptText to "fontファイルを選んでください" as text
  set listAliasFilePath to (choose file with prompt strPromptText default location (aliasFontsDirPath) of type listChooseFileUTI with invisibles and multiple selections allowed without showing package contents) as list
open listAliasFilePath
end run


on open listAliasFilePath
  ########################
  ##python3のパスチェック
  if (doChkPython3()) is false then
return "python3が見つかりませんでした終了します"
  end if
  ########################
  ## pythonスクリプトのパス
  set strPyFileName to ("Font2Image.py") as text
  set ocidPyFilePathURL to doGetScriptPath(strPyFileName)
  if ocidPyFilePathURL is false then
return "pythonスクリプトが見つかりませんでした処理を終了します"
  else
    set strPyFilePath to (ocidPyFilePathURL's |path|) as text
  end if
  ##
  set listOkUTI to {"public.opentype-font", "public.truetype-ttf-font", "public.truetype-collection-font", "public.opentype-collection-font"} as list
  set ocidUTIarray to refMe's NSArray's arrayWithArray:(listOkUTI)
  ########################
  ##ファイルチェック
  repeat with itemAliasFilePath in listAliasFilePath
    set aliasFilePath to itemAliasFilePath as alias
    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))
    set listResourceValue to (ocidFilePathURL's getResourceValue:(reference) forKey:(refMe's NSURLContentTypeKey) |error|:(reference))
    set ocidContentType to (item 2 of listResourceValue)
    set strUTI to (ocidContentType's identifier) as text
    set boolContain to (ocidUTIarray's containsObject:(strUTI))
    if boolContain is true then
log "対象フォントです"
    else
return "対象外のファイルです処理を中止します"
    end if
  end repeat
  
  ########################
  ## クリップボードの中身取り出し
  set appPasteboard to refMe's NSPasteboard's generalPasteboard()
  set ocidPastBoardTypeArray to appPasteboard's types
  ###テキストがあれば
  set boolContain to ocidPastBoardTypeArray's containsObject:"public.utf8-plain-text"
  if boolContain = true then
    ###値を格納する
    tell application "Finder"
      set strReadString to (the clipboard as text) as text
    end tell
    ###Finderでエラーしたら
  else
    set boolContain to ocidPastBoardTypeArray's containsObject:"NSStringPboardType"
    if boolContain = true then
      set ocidReadString to ocidPasteboard's readObjectsForClasses:({refMe's NSString}) options:(missing value)
      set strReadString to ocidReadString as text
    else
log "テキストなし"
      set strReadString to "ABCDEFGHIJKLMNOPQRSTUVWXYZ" as text
    end if
  end if
  ###ダイアログ
  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 aliasIconPath to (POSIX file "/System/Library/CoreServices/Tips.app/Contents/Resources/AppIcon.icns") as alias
  set strTitle to ("入力してください") as text
  set strMes to ("印字するテキストを入力半角20文字まで\r欧文フォントに日本語入れると〓になります") as text
  set recordResult to (display dialog strMes with title strTitle default answer strReadString buttons {"キャンセル", "OK"} default button "OK" cancel button "キャンセル" giving up after 30 with icon aliasIconPath without hidden answer)
  if (gave up of recordResult) is true then
return "時間切れです"
  else if (button returned of recordResult) is "キャンセル" then
return "キャンセルです"
  else
    set strReturnedText to (text returned of recordResult) as text
  end if
  ###戻り値整形
  set ocidResponseText to (refMe's NSString's stringWithString:(strReturnedText))
  ###タブと改行を除去しておく
  set ocidTextM to refMe's NSMutableString's alloc()'s initWithCapacity:(0)
ocidTextM's appendString:(ocidResponseText)
  ##改行除去
  set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\n") withString:("")
  set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\r") withString:("")
  ##タブ除去
  set ocidTextM to ocidTextM's stringByReplacingOccurrencesOfString:("\t") withString:("")
  set strReturnedText to ocidTextM as text
  ###############################
  ## 背景色
  ###############################
  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 the listRGB16bitColor to (choose color default color {65535, 55535, 0, 0}) as list
  set numRcolor16Bit to (item 1 of listRGB16bitColor) as integer
  set numGcolor16Bit to (item 2 of listRGB16bitColor) as integer
  set numBcolor16Bit to (item 3 of listRGB16bitColor) as integer
  #
  set numRcolorFloat to (numRcolor16Bit / 65535) as number
  set numGcolorFloat to (numGcolor16Bit / 65535) as number
  set numBcolorFloat to (numBcolor16Bit / 65535) as number
  #
  set numRcolor8Bit to (round of (numRcolorFloat * 255) rounding down) as integer
  set numGcolor8Bit to (round of (numGcolorFloat * 255) rounding down) as integer
  set numBcolor8Bit to (round of (numBcolorFloat * 255) rounding down) as integer
  #
  set numRHex to getHex(numRcolor8Bit) as text
  set numGHex to getHex(numGcolor8Bit) as text
  set numBHex to getHex(numBcolor8Bit) as text
  
  set strBackgroundColorHex to ("" & numRHex & numGHex & numBHex & "") as text
  ###############################
  ## 文字色
  ###############################
  set the listRGB16bitColor to (choose color default color {0, 0, 0, 0}) as list
  set numRcolor16Bit to (item 1 of listRGB16bitColor) as integer
  set numGcolor16Bit to (item 2 of listRGB16bitColor) as integer
  set numBcolor16Bit to (item 3 of listRGB16bitColor) as integer
  #
  set numRcolorFloatText to (numRcolor16Bit / 65535) as number
  set numGcolorFloatText to (numGcolor16Bit / 65535) as number
  set numBcolorFloatText to (numBcolor16Bit / 65535) as number
  
  set numRcolor8BitText to (round of (numRcolorFloatText * 255) rounding down) as integer
  set numGcolor8BitText to (round of (numGcolorFloatText * 255) rounding down) as integer
  set numBcolor8BitText to (round of (numBcolorFloatText * 255) rounding down) as integer
  #
  set numRHexText to getHex(numRcolor8BitText) as text
  set numGHexText to getHex(numGcolor8BitText) as text
  set numBHexText to getHex(numBcolor8BitText) as text
  
  set strTextColorHex to ("" & numRHexText & numGHexText & numBHexText & "") as text
  
  ########################
  set appFileManager to refMe's NSFileManager's defaultManager()
  ## 保存先
  set ocidDirPathURLArray to (appFileManager's URLsForDirectory:(refMe's NSPicturesDirectory) inDomains:(refMe's NSUserDomainMask))
  set ocidDirPathURL to ocidDirPathURLArray's firstObject()
  set ocidSaveDirPathURL to ocidDirPathURL's URLByAppendingPathComponent:("FontsImages/images")
  set strSaveDirPathURL to (ocidSaveDirPathURL's |path|) as text
  ##保存先確保
  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 listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
  
  repeat with itemAliasFilePath in listAliasFilePath
    set aliasFilePath to itemAliasFilePath as alias
    set strFontFilePath to (POSIX path of aliasFilePath) as text
    ## pyパス arg1:フォントファイルパス arg2:保存先Dir arg3:印字するテキスト
    set strCommandText to ("\"" & strPyFilePath & "\" \"" & strFontFilePath & "\" \"" & strSaveDirPathURL & "\" \"" & strReturnedText & "\" \"" & strBackgroundColorHex & "\" \"" & strTextColorHex & "\"") as text
do shell script strCommandText
    
  end repeat
  
  ####################
  ## HTMLを生成
doMakeFolderImage2HTML(ocidSaveDirPathURL)
  ##Finderで保存先を開く
  set aliasSaveDirPath to (ocidSaveDirPathURL's absoluteURL()) as alias
  tell application "Finder"
open location aliasSaveDirPath
  end tell
end open

####################
## pythonスクリプトのパス
to doGetScriptPath(argFileName)
  set strPyFileName to argFileName as text
  tell application "Finder"
    set aliasPathToMe to (path to me) as alias
    set aliasContainerDirPath to (container of aliasPathToMe) as alias
  end tell
  #
  set strContainerDirPath to (POSIX path of aliasContainerDirPath) as text
  set ocidContainerDirPathStr to refMe's NSString's stringWithString:(strContainerDirPath)
  set ocidContainerDirPath to ocidContainerDirPathStr's stringByStandardizingPath()
  set ocidContainerDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidContainerDirPath) isDirectory:true)
  #
  set strSetValue to ("bin/" & strPyFileName) as text
  set ocidPyFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:(strSetValue)
  #有無チェック
  set ocidPyFilePath to ocidPyFilePathURL's |path|
  set appFileManager to refMe's NSFileManager's defaultManager()
  set boolExists to appFileManager's fileExistsAtPath:(ocidPyFilePath) isDirectory:(false)
log boolExists
  if boolExists is true then
return ocidPyFilePathURL
  else if boolExists is false then
return false
  end if
  
end doGetScriptPath
####################
## python3のパスチェック
to doChkPython3()
  try
    set strCmdText to ("/usr/bin/which python3") as text
    set strBinPath to (do shell script strCmdText) as text
return true
  on error
return false
  end try
  
  
end doChkPython3


####################
##カラー HEX値を求める

property record8Bit2Hex : {|0|:"00", |1|:"01", |2|:"02", |3|:"03", |4|:"04", |5|:"05", |6|:"06", |7|:"07", |8|:"08", |9|:"09", |10|:"0A", |11|:"0B", |12|:"0C", |13|:"0D", |14|:"0E", |15|:"0F", |16|:"10", |17|:"11", |18|:"12", |19|:"13", |20|:"14", |21|:"15", |22|:"16", |23|:"17", |24|:"18", |25|:"19", |26|:"1A", |27|:"1B", |28|:"1C", |29|:"1D", |30|:"1E", |31|:"1F", |32|:"20", |33|:"21", |34|:"22", |35|:"23", |36|:"24", |37|:"25", |38|:"26", |39|:"27", |40|:"28", |41|:"29", |42|:"2A", |43|:"2B", |44|:"2C", |45|:"2D", |46|:"2E", |47|:"2F", |48|:"30", |49|:"31", |50|:"32", |51|:"33", |52|:"34", |53|:"35", |54|:"36", |55|:"37", |56|:"38", |57|:"39", |58|:"3A", |59|:"3B", |60|:"3C", |61|:"3D", |62|:"3E", |63|:"3F", |64|:"40", |65|:"41", |66|:"42", |67|:"43", |68|:"44", |69|:"45", |70|:"46", |71|:"47", |72|:"48", |73|:"49", |74|:"4A", |75|:"4B", |76|:"4C", |77|:"4D", |78|:"4E", |79|:"4F", |80|:"50", |81|:"51", |82|:"52", |83|:"53", |84|:"54", |85|:"55", |86|:"56", |87|:"57", |88|:"58", |89|:"59", |90|:"5A", |91|:"5B", |92|:"5C", |93|:"5D", |94|:"5E", |95|:"5F", |96|:"60", |97|:"61", |98|:"62", |99|:"63", |100|:"64", |101|:"65", |102|:"66", |103|:"67", |104|:"68", |105|:"69", |106|:"6A", |107|:"6B", |108|:"6C", |109|:"6D", |110|:"6E", |111|:"6F", |112|:"70", |113|:"71", |114|:"72", |115|:"73", |116|:"74", |117|:"75", |118|:"76", |119|:"77", |120|:"78", |121|:"79", |122|:"7A", |123|:"7B", |124|:"7C", |125|:"7D", |126|:"7E", |127|:"7F", |128|:"80", |129|:"81", |130|:"82", |131|:"83", |132|:"84", |133|:"85", |134|:"86", |135|:"87", |136|:"88", |137|:"89", |138|:"8A", |139|:"8B", |140|:"8C", |141|:"8D", |142|:"8E", |143|:"8F", |144|:"90", |145|:"91", |146|:"92", |147|:"93", |148|:"94", |149|:"95", |150|:"96", |151|:"97", |152|:"98", |153|:"99", |154|:"9A", |155|:"9B", |156|:"9C", |157|:"9D", |158|:"9E", |159|:"9F", |160|:"A0", |161|:"A1", |162|:"A2", |163|:"A3", |164|:"A4", |165|:"A5", |166|:"A6", |167|:"A7", |168|:"A8", |169|:"A9", |170|:"AA", |171|:"AB", |172|:"AC", |173|:"AD", |174|:"AE", |175|:"AF", |176|:"B0", |177|:"B1", |178|:"B2", |179|:"B3", |180|:"B4", |181|:"B5", |182|:"B6", |183|:"B7", |184|:"B8", |185|:"B9", |186|:"BA", |187|:"BB", |188|:"BC", |189|:"BD", |190|:"BE", |191|:"BF", |192|:"C0", |193|:"C1", |194|:"C2", |195|:"C3", |196|:"C4", |197|:"C5", |198|:"C6", |199|:"C7", |200|:"C8", |201|:"C9", |202|:"CA", |203|:"CB", |204|:"CC", |205|:"CD", |206|:"CE", |207|:"CF", |208|:"D0", |209|:"D1", |210|:"D2", |211|:"D3", |212|:"D4", |213|:"D5", |214|:"D6", |215|:"D7", |216|:"D8", |217|:"D9", |218|:"DA", |219|:"DB", |220|:"DC", |221|:"DD", |222|:"DE", |223|:"DF", |224|:"E0", |225|:"E1", |226|:"E2", |227|:"E3", |228|:"E4", |229|:"E5", |230|:"E6", |231|:"E7", |232|:"E8", |233|:"E9", |234|:"EA", |235|:"EB", |236|:"EC", |237|:"ED", |238|:"EE", |239|:"EF", |240|:"F0", |241|:"F1", |242|:"F2", |243|:"F3", |244|:"F4", |245|:"F5", |246|:"F6", |247|:"F7", |248|:"F8", |249|:"F9", |250|:"FA", |251|:"FB", |252|:"FC", |253|:"FD", |254|:"FE", |255|:"FF"} as record
to getHex(arg8BitNum)
  set ocid8Bit2HexDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocid8Bit2HexDict's setDictionary:(record8Bit2Hex)
  set str8BitNum to arg8BitNum as string
  set ocidValue to (ocid8Bit2HexDict's valueForKey:(str8BitNum))
  set strValue to ocidValue as text
return strValue
end getHex

to doMakeFolderImage2HTML(argDirPathURL)
  ####################################
  ####フォルタ以外は処理しない
  
  set ocidDirPathURL to argDirPathURL
  set listBoole to (ocidDirPathURL's getResourceValue:(reference) forKey:(refMe's NSURLIsDirectoryKey) |error|:(reference))
  set boolIsDir to (item 2 of listBoole) as boolean
  if boolIsDir is true then
log "フォルダなので処理開始"
  else
return "フォルダ以外は処理しない"
  end if
  
  
  ################################
  ##【本処理】フォルダの数だけ繰り返し
  ################################
  ###enumeratorAtURL用のBoolean用
  set ocidFalse to (refMe's NSNumber's numberWithBool:false)
  set ocidTrue to (refMe's NSNumber's numberWithBool:true)
  
  ###ファイルURLのみを格納するリスト
  set ocidFilePathURLAllArray to (refMe's NSMutableArray's alloc()'s initWithCapacity:0)
  ##################################
  #ファイルURLの収集
  ##プロパティ
  set ocidPropertieKeyArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
ocidPropertieKeyArray's addObject:(refMe's NSURLPathKey)
ocidPropertieKeyArray's addObject:(refMe's NSURLContentTypeKey)
  ##オプション(隠しファイルは含まない)
  set ocidOption to refMe's NSDirectoryEnumerationSkipsHiddenFiles
  ####ディレクトリのコンテツを収集(最下層まで)
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidEmuDict to (appFileManager's enumeratorAtURL:(ocidDirPathURL) includingPropertiesForKeys:(ocidPropertieKeyArray) options:(ocidOption) errorHandler:(reference))
  ###戻り値をリストに格納
  set ocidEmuFileURLArray to ocidEmuDict's allObjects()
ocidFilePathURLAllArray's addObjectsFromArray:(ocidEmuFileURLArray)
  
  ##############################
  ####並び替え並び替え compare
  (*
compare:
caseInsensitiveCompare:
localizedCompare:
localizedStandardCompare:
localizedCaseInsensitiveCompare:
*)
  #NSSortDescriptor's
  set ocidSortDescriptorsArray to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
  set ocidSortDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:"absoluteString" ascending:(true) selector:"localizedStandardCompare:")
ocidSortDescriptorsArray's addObject:(ocidSortDescriptor)
  ##
ocidFilePathURLAllArray's sortUsingDescriptors:(ocidSortDescriptorsArray)
  ##############################
  ##まずはHTMLの基本的部分を生成
  ########################################
  #headerに渡すエレメント
  set ocidH3Element to refMe's NSXMLElement's elementWithName:("h3")
(ocidH3Element's setStringValue:("イメージ一覧"))
  
  ########################################
  #footerに渡すエレメント
  set ocidFotterAElement to refMe's NSXMLElement's elementWithName:("a")
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:("https://quicktimer.cocolog-nifty.com/"))
(ocidFotterAElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidFotterAElement's addAttribute:(ocidAddNode))
  set strContents to ("AppleScriptで生成しました") as text
(ocidFotterAElement's setStringValue:(strContents))
  
  ########################################
  #articleに渡すエレメント
  set ocidSetArticleElement to refMe's NSXMLElement's elementWithName:("div")
  set ocidH6Element to refMe's NSXMLElement's elementWithName:("h6")
(ocidH6Element's setStringValue:("イメージ一覧"))
ocidSetArticleElement's addChild:(ocidH6Element)
  #テーブル部生成開始
  set ocidTableElement to refMe's NSXMLElement's elementWithName:("table")
  #####【caption】
  set ocidCaptionElement to refMe's NSXMLElement's elementWithName:("caption")
ocidCaptionElement's setStringValue:("イメージファイル一覧(クリックで新しいWINDOWで開きます)")
ocidTableElement's addChild:(ocidCaptionElement)
  #####【colgroup】
  set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup")
  #タイトル部の数だけ繰り返し
  set listColName to {"LineNo", "Image"} as list
  repeat with itemColName in listColName
    #######【col】col生成
    set ocidAddElement to (refMe's NSXMLElement's elementWithName:("col"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
(ocidColgroupElement's addChild:(ocidAddElement))
  end repeat
  #テーブルエレメントに追加
ocidTableElement's addChild:(ocidColgroupElement)
  #######【thead】
  set ocidTheadElement to refMe's NSXMLElement's elementWithName:("thead")
  #TR
  set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
  #タイトル部の数だけ繰り返し
  repeat with itemColName in listColName
    if (itemColName as text) is "LineNo" then
      #ここはTDではなくてTHを利用
      set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
      ####項番処理
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
      #
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("id") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidAddElement's addAttribute:(ocidAddNode))
      #値を入れる
(ocidAddElement's setStringValue:("\"))
      #TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
    else
      #ここはTDではなくてTHを利用
      set ocidAddElement to (refMe's NSXMLElement's elementWithName:("th"))
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
      #
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("id") stringValue:(itemColName))
(ocidAddElement's addAttribute:(ocidAddNode))
      set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidAddElement's addAttribute:(ocidAddNode))
      #値を入れる
(ocidAddElement's setStringValue:(itemColName))
      #TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
    end if
  end repeat
  #TRをTHEADにセット
ocidTheadElement's addChild:(ocidTrElement)
  #THEADをテーブルにセット
ocidTableElement's addChild:(ocidTheadElement)
  
  ##################################
  #本処理
  ######【tbody】
  set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
  set numCntRow to 1 as integer
  repeat with itemArray in ocidFilePathURLAllArray
    #TRの開始
    set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
    ####項番処理
    set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("項目番号:" & numCntRow))
(ocidThElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("headers") stringValue:("LineNo"))
(ocidThElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
(ocidThElement's addAttribute:(ocidAddNode))
(ocidThElement's setStringValue:(numCntRow as text))
(ocidTrElement's addChild:(ocidThElement))
    ##################################
    set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
    ####
    #一つ上の階層を基準とした相対パスにする
    ##コンテナ
    set ocidPreDirPathURL to itemArray's URLByDeletingLastPathComponent()
    set ocidContainerName to ocidPreDirPathURL's lastPathComponent()
    set ocidFileName to itemArray's lastPathComponent()
    
    set ocidRelativePath to (ocidContainerName's stringByAppendingPathComponent:(ocidFileName))
    ###
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:(ocidFileName))
(ocidTdElement's addAttribute:(ocidAddNode))
    # set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("headers") stringValue:(ocidFileName))
    #(ocidTdElement's addAttribute:(ocidAddNode))
    # set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("col"))
    # (ocidTdElement's addAttribute:(ocidAddNode))
    ###
    set ocidAElement to (refMe's NSXMLElement's elementWithName:("a"))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(ocidRelativePath))
(ocidAElement's addAttribute:(ocidAddNode))
    set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidAElement's addAttribute:(ocidAddNode))
    ###
    set ocidImgElement to (refMe's NSXMLElement's elementWithName:("img"))
(ocidImgElement's addAttribute:(refMe's NSXMLNode's attributeWithName:("src") stringValue:(ocidRelativePath)))
(ocidImgElement's addAttribute:(refMe's NSXMLNode's attributeWithName:("alt") stringValue:(ocidFileName)))
(ocidImgElement's addAttribute:(refMe's NSXMLNode's attributeWithName:("style") stringValue:("object-fit: cover;")))
(ocidAElement's addChild:(ocidImgElement))
    #
(ocidTdElement's addChild:(ocidAElement))
    ##
(ocidTrElement's addChild:(ocidTdElement))
(ocidTbodyElement's addChild:(ocidTrElement))
    set numCntRow to numCntRow + 1 as integer
  end repeat
  #TBODYをテーブルにセット
ocidTableElement's addChild:(ocidTbodyElement)
  
  
  ######【tfoot】 TRで
  set ocidTfootElement to refMe's NSXMLElement's elementWithName:("tfoot")
  set ocidTrElement to refMe's NSXMLElement's elementWithName:("tr")
  #項目数を取得して
  set numCntCol to (count of listColName) as integer
  #colspan指定して1行でセット
  set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("テーブルの終わり"))
(ocidThElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("colspan") stringValue:(numCntCol as text))
(ocidThElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("scope") stringValue:("row"))
(ocidThElement's addAttribute:(ocidAddNode))
  #
  set numCntContents to (count of ocidFilePathURLAllArray) as integer
  set strContents to ("項目数 : " & (numCntContents)) as text
(ocidThElement's setStringValue:(strContents))
  #THをTRにセットして
ocidTrElement's addChild:(ocidThElement)
  #TRをTFOOTにセット
ocidTfootElement's addChild:(ocidTrElement)
  #TFOOTをテーブルにセット
ocidTableElement's addChild:(ocidTfootElement)
ocidSetArticleElement's addChild:(ocidTableElement)
  ##############################
  #HTMLにする
  ##############################
  set ocidHTML to doMakeRootElement({ocidH3Element, ocidSetArticleElement, ocidFotterAElement})
  ##############################
  #保存
  ##############################
  #読み取りやすい表示
  set ocidXMLdata to ocidHTML's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
  #
  set ocidSaveDirPathURL to ocidDirPathURL's URLByDeletingLastPathComponent()
  #
  set strSaveFileName to ("index.html") as text
  set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName)
  
  set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)
  
  ####【6】ブラウザで開く
  ##デフォルトブラウザで開く場合
  (*
set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
tell application "Finder"
open location aliasFilePath
end tell
*)
  
  set aliasHTMLFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
  
  tell application "Finder"
open location aliasHTMLFilePath
  end tell
  
end doMakeFolderImage2HTML

############################################################
# 基本的なHTMLの構造
(*
doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents})
HTMLのBODY部
header
article
footerにそれぞれAddchildするデータをリストで渡す
戻り値はRootエレメントにセットされた
NSXMLDocumentを戻すので 保存すればOK
*)
############################################################
to doMakeRootElement({argHeaderContents, argArticleContents, argFooterContents})
  #XML初期化
  set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentHTMLKind)
  # DTD付与
  set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
ocidDTD's setName:("html")
ocidXMLDoc's setDTD:(ocidDTD)
  #
  set ocidRootElement to refMe's NSXMLElement's elementWithName:("html")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("lang") stringValue:("ja")
ocidRootElement's addAttribute:(ocidAddNode)
  #
  set ocidHeadElement to refMe's NSXMLElement's elementWithName:("head")
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("title")
ocidAddElement's setStringValue:("ImageList")
ocidHeadElement's addChild:(ocidAddElement)
  # http-equiv
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("http-equiv") stringValue:("Content-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/html; charset=UTF-8")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("meta")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("name") stringValue:("viewport")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("width=720")
ocidAddElement's addAttribute:(ocidAddNode)
ocidHeadElement's addChild:(ocidAddElement)
  #
  set ocidAddElement to refMe's NSXMLElement's elementWithName:("style")
ocidAddElement's setStringValue:("body {margin: 10px;background-color: #FFFFFF;}table {max-width: 1280px;border-spacing: 0;caption-side: top;font-family: system-ui;}thead th {border: solid 1px #666666;padding: .5ch 1ch;border-block-width: 1px 0;border-inline-width: 1px 0;&:first-of-type {border-start-start-radius: .5em}&:last-of-type {border-start-end-radius: .5em;border-inline-end-width: 1px}}tbody th {border-spacing: 0;width: 40px !important;border: solid 1px #666666;padding: .5ch 1ch;border-block-width: 1px 0;border-inline-width: 1px 0;}tbody td {word-wrap: break-word;width: 720px;max-width: 720px;border-spacing: 0;border: solid 1px #666666;padding: .5ch 1ch;border-block-width: 1px 0;border-inline-width: 1px 0;&:last-of-type {border-inline-end-width: 1px}}tbody tr:nth-of-type(odd) {background: #F2F2F2;}.kind_string {font-size: 0.75em;}.date_string {font-size: 0.5em;}tfoot th {border: solid 1px #666666;padding: .5ch 1ch;&:first-of-type {border-end-start-radius: .5em}&:last-of-type {border-end-end-radius: .5em;border-inline-end-width: 1px}}img {max-width: 720px;}")
ocidHeadElement's addChild:(ocidAddElement)
ocidRootElement's addChild:(ocidHeadElement)
  #
  #ボディエレメント
  set ocidBodyElement to refMe's NSXMLElement's elementWithName:("body")
  #ヘッダー
  set ocidHeaderElement to refMe's NSXMLElement's elementWithName:("header")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("header")
ocidHeaderElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_header")
ocidHeaderElement's addAttribute:(ocidAddNode)
ocidHeaderElement's addChild:(argHeaderContents)
ocidBodyElement's addChild:(ocidHeaderElement)
  #アーティクル
  set ocidArticleElement to refMe's NSXMLElement's elementWithName:("article")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("article")
ocidArticleElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_article")
ocidArticleElement's addAttribute:(ocidAddNode)
ocidArticleElement's addChild:(argArticleContents)
  
ocidBodyElement's addChild:(ocidArticleElement)
  #フッター
  set ocidFooterElement to refMe's NSXMLElement's elementWithName:("footer")
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("footer")
ocidFooterElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("class") stringValue:("body_footer")
ocidFooterElement's addAttribute:(ocidAddNode)
ocidFooterElement's addChild:(argFooterContents)
ocidBodyElement's addChild:(ocidFooterElement)
  #ボディをROOTエレメントにセット
ocidRootElement's addChild:(ocidBodyElement)
  #ROOTをXMLにセット
ocidXMLDoc's setRootElement:(ocidRootElement)
  #値を戻す
return ocidXMLDoc
end doMakeRootElement

|

« [Python]PDFのクリエーター情報の削除 | トップページ | TSV行列入替 »

AppleScript Fonts」カテゴリの記事