« [qlmanage]SVG to PNG | トップページ | 【CURL】基本的な事項 »

[SVG]データを元にSVGを大量生成する

XMLデータが内包されていますので
試したい方はこちらをどうぞ

ダウンロード - copic.zip



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

#!/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 scripting additions
property refMe : a reference to current application

###このファイルのパス
set aliasPathToMe to (path to me) as alias
tell application "Finder"
  ###コンテナディレクトリ
  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()
###XMLのパス
set ocidXmlFilePath to ocidContainerDirPath's stringByAppendingPathComponent:("data/CopicColors.xml")
###URLに
set ocidXmlFilePathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidXmlFilePath) isDirectory:false)
###ファイル読み込み
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to (refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidXmlFilePathURL) options:(ocidOption) |error|:(reference))
###XMLドキュメント
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###ROOTエレメント
set ocidRootElement to ocidReadXMLDoc's rootElement()
set numChild to (count of ocidRootElement's children) as integer
repeat with numCntChild from 0 to (numChild - 1)
  ###チルドレン構造のアトリビュートの値をキー値から取得
  set ocidChild to (ocidRootElement's childAtIndex:numCntChild)
  set strColorID to (ocidChild's attributeForName:("Number"))'s stringValue() as text
  set strColorName to (ocidChild's attributeForName:("Name"))'s stringValue() as text
  set strR to (ocidChild's attributeForName:("R"))'s stringValue() as text
  set strG to (ocidChild's attributeForName:("G"))'s stringValue() as text
  set strB to (ocidChild's attributeForName:("B"))'s stringValue() as text
  ####SVG化する
  set strSVG to ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\"><svg height=\"800.0pt\" stroke-miterlimit=\"10\" style=\"fill-rule:nonzero;clip-rule:evenodd;stroke-linecap:round;stroke-linejoin:round;\" version=\"1.1\" viewBox=\"0 0 800 800\" width=\"800.0pt\" xml:space=\"preserve\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"><defs/><g id=\"COPIC\"><path fill=\"rgb(" & strR & ", " & strG & ", " & strB & ")\" d=\"M40 184C40 104.471 104.471 40 184 40L616 40C695.529 40 760 104.471 760 184L760 616C760 695.529 695.529 760 616 760L184 760C104.471 760 40 695.529 40 616L40 184Z\" fill-rule=\"nonzero\" opacity=\"1\" stroke=\"none\"/><text fill=\"#000000\" font-family=\"Helvetica-Bold\" font-size=\"144\" opacity=\"1\" stroke=\"none\" text-anchor=\"start\" transform=\"matrix(1 0 0 1 0 120)\" x=\"0\" y=\"0\"><tspan textLength=\"520\" x=\"144\" y=\"140\">" & strColorID & "</tspan></text><text fill=\"#000000\" font-family=\"Helvetica-Bold\" font-size=\"52\" opacity=\"1\" stroke=\"none\" text-anchor=\"start\" transform=\"matrix(1 0 0 1 0 290)\" x=\"0\" y=\"0\"><tspan textLength=\"390\" x=\"200\" y=\"70\">" & strColorName & "</tspan></text></g></svg>") as text
  ####出力用テキスト
  set ocidSaveSVG to (refMe's NSString's stringWithString:(strSVG))
  ###ファイル名
  set strSaveFileName to (strColorID & ".svg") as text
  
  ###保存先
  set appFileManager to refMe's NSFileManager's defaultManager()
  set ocidContainerDirPathURL to (refMe's NSURL's alloc()'s initFileURLWithPath:(ocidContainerDirPath) isDirectory:true)
  set ocidSaveDirPathURL to (ocidContainerDirPathURL's URLByAppendingPathComponent:("OutPut"))
  ###保存先フォルダ作っておく
  set appFileManager to refMe's NSFileManager's defaultManager()
  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))
  ###保存先パスURL
  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(strSaveFileName))
  ###保存
  set listDone to (ocidSaveSVG's writeToURL:(ocidSaveFilePathURL) atomically:(refMe's NSNumber's numberWithBool:true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
  
end repeat


return



|

« [qlmanage]SVG to PNG | トップページ | 【CURL】基本的な事項 »

AppleScript XML SVG」カテゴリの記事