« [TTML]現時点でのまとめ(自分用) | トップページ | アニメーション画像 »

[XML]XMLとしてSVGを生成する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
#
#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()


###設定
set strWidth to ("360") as text
set strHight to ("720") as text
set strText to ("美しい日本語") as text
set strFontSize to ("36") as text
#
set strTextRange to (strFontSize * (count of strText))

###【1】保存先ファイルパス
###デスクトップ
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDesktopDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDesktopDirPathURL to ocidURLsArray's firstObject()
###保存ファイルURL
set strFileName to ("test.svg") as text
set ocidSaveFilePathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:(strFileName)

###【2】XML生成
set ocidXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidXMLDoc's setVersion:("1.0")
ocidXMLDoc's setCharacterEncoding:("UTF-8")
ocidXMLDoc's setStandalone:("no")
(*
NSXMLDocumentXMLKind: XML形式
NSXMLDocumentXHTMLKind: XHTML形式の
NSXMLDocumentHTMLKind: HTML形式の
NSXMLDocumentTextKind: プレーンテキスト形式
*)
ocidXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentXMLKind)

###【3】DTD生成
set ocidDTD to refMe's NSXMLDTD's alloc()'s init()
ocidDTD's setName:("svg")
ocidDTD's setPublicID:("-//W3C//DTD SVG 1.1//EN")
ocidDTD's setSystemID:("http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd")
####【3-1】DTDをXMLにセットする
ocidXMLDoc's setDTD:(ocidDTD)

###【4】ROOTエレメントを生成
set ocidRootElement to refMe's NSXMLElement's elementWithName:("svg")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("version") stringValue:("1.1")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("xmlns") stringValue:("http://www.w3.org/2000/svg")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("xmlns:xlink") stringValue:("http://www.w3.org/1999/xlink")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("xml:space") stringValue:("preserve")
ocidRootElement's addAttribute:(ocidAddNode)
#背景色を指定する場合はここ
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("style") stringValue:("fill-rule:nonzero;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("width") stringValue:("" & strWidth & ".0px")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("height") stringValue:("" & strHight & ".0px")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("viewBox") stringValue:("0 0 " & strWidth & " " & strHight & "")
ocidRootElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("stroke-miterlimit") stringValue:("10")
ocidRootElement's addAttribute:(ocidAddNode)
###【4-1】defs ここは空で
set ocidDefsElement to refMe's NSXMLElement's elementWithName:("defs")
ocidRootElement's addChild:(ocidDefsElement)
###【4-2】path一番下のレイヤー
set ocidBackgroundElement to refMe's NSXMLElement's elementWithName:("path")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("d") stringValue:("M0 0L" & strWidth & " 0L" & strWidth & " " & strHight & "L0 " & strHight & "L0 0Z")
ocidBackgroundElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("fill") stringValue:("#AAAAAA")
ocidBackgroundElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("fill-rule") stringValue:("nonzero")
ocidBackgroundElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("opacity") stringValue:("1")
ocidBackgroundElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("stroke") stringValue:("none")
ocidBackgroundElement's addAttribute:(ocidAddNode)
#【4-2】pathをrootエレメントにセット
ocidRootElement's addChild:(ocidBackgroundElement)
###【4-3】g
set ocidGElement to refMe's NSXMLElement's elementWithName:("g")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("id") stringValue:("text")
ocidGElement's addAttribute:(ocidAddNode)
###【4-3-1】text
set ocidTextElement to refMe's NSXMLElement's elementWithName:("text")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("fill") stringValue:("#000000")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("font-family") stringValue:("mono")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("font-size") stringValue:("" & strFontSize & "")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("opacity") stringValue:("1")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("stroke") stringValue:("none")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("x") stringValue:("0")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("y") stringValue:("0")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("text-anchor") stringValue:("start")
ocidTextElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("transform") stringValue:("matrix(1 0 0 1 0 360)")
ocidTextElement's addAttribute:(ocidAddNode)
###【4-3-2】tspan
set ocidTSpanElement to refMe's NSXMLElement's elementWithName:("tspan")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("x") stringValue:("0")
ocidTSpanElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("y") stringValue:("0")
ocidTSpanElement's addAttribute:(ocidAddNode)
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("textLength") stringValue:(strTextRange)
ocidTSpanElement's addAttribute:(ocidAddNode)
ocidTSpanElement's setStringValue:(strText)

#######
#【4-3-2】tspan を【4-3-1】textの子要素にセット
ocidTextElement's addChild:(ocidTSpanElement)
#【4-3-1】textを 【4-3】gの子要素にセット
ocidGElement's addChild:(ocidTextElement)
#【4-3】gをrootエレメントの子要素としてセット
ocidRootElement's addChild:(ocidGElement)
#【4-3】ROOTエレメントXMLにセットする
ocidXMLDoc's setRootElement:(ocidRootElement)
###【5】XMLデータにして(ここでテキスト形式のXMLに)
set ocidXMLdata to ocidXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
###【6】ファイルに保存
set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)


return


|

« [TTML]現時点でのまとめ(自分用) | トップページ | アニメーション画像 »

AppleScript XML」カテゴリの記事

AppleScript XML SVG」カテゴリの記事