« [Youtube]ビデオIDから主要な情報を取得 | トップページ | [PDFoutline]しおり boookmarkをテキストに保存 »

[SVG] adobe illustrator の編集機能 adobe_illustrator_pgfを削除する


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
(*
adobe_illustrator_pgf
Preserve Graphic Formatを削除する
*)
# com.cocolog-nifty.quicktimer.icefloe
----+----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
property refNSNotFound : a reference to 9.22337203685477E+18 + 5807

set listUTI to {"public.svg", "public.svgz", "public.svg-image"}

##XML宣言の無いSVGの場合の処理
# ture --> XML処理をする
# false --> XML処理をせず手を加えずそのままにする
set boolDoXML to false as boolean


###################################
#####入力ダイアログ
###################################
##デフォルトパス
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
#####ダイアログを前面に
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 strPromptText to "SVGファイルを選んでください" as text
set strMesText to "SVGファイルを選んでください" as text
set listAliasFilePath to (choose file strMesText with prompt strPromptText default location (aliasDesktopDirPath) of type listUTI with invisibles and multiple selections allowed without showing package contents) as list

###################################
#####保存先
###################################
set ocidSaveDirPathURL to ocidDesktopDirPathURL's URLByAppendingPathComponent:("SVG処理済")
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)
set aliasSaveDirPathURL to (ocidSaveDirPathURL's absoluteURL()) as alias
###################################
#####入力ファイルの数だけ繰り返し
###################################

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) isDirectory:false)
  set ocidFileName to ocidFilePathURL's lastPathComponent()
  #保存先
  set ocidSaveFilePathURL to (ocidSaveDirPathURL's URLByAppendingPathComponent:(ocidFileName))
  ####UTIの取得
  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 strExtensionName to (ocidFilePathURL's pathExtension()) as text
  
  if strUTI is "public.svgz" then
return "圧縮SVGは処理しない"
    set ocidOption to (refMe's NSFileManagerItemReplacementWithoutDeletingBackupItem)
    set listDone to (appFileManager's replaceItemAtURL:(ocidSaveFilePathURL) withItemAtURL:(ocidFilePathURL) backupItemName:(missing value) options:(ocidOption) resultingItemURL:(missing value) |error|:(reference))
    set listDone to (appFileManager's copyItemAtURL:(ocidSaveFilePathURL) toURL:(ocidFilePathURL) |error|:(reference))
  else if strExtensionName is "svgz" then
return "圧縮SVGは処理しない"
    set ocidOption to (refMe's NSFileManagerItemReplacementWithoutDeletingBackupItem)
    set listDone to (appFileManager's replaceItemAtURL:(ocidSaveFilePathURL) withItemAtURL:(ocidFilePathURL) backupItemName:(missing value) options:(ocidOption) resultingItemURL:(missing value) |error|:(reference))
    set listDone to (appFileManager's copyItemAtURL:(ocidSaveFilePathURL) toURL:(ocidFilePathURL) |error|:(reference))
  end if
  ###【1】ファイル読み込み
  ##NSDATAに読み込んで
  set ocidOption to (refMe's NSDataReadingMappedIfSafe)
  set listReadData to (refMe's NSData's dataWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
  set ocidReadData to (item 1 of listReadData)
  ###DTDを保存した形式でXMLドキュメントにする
  set ocidOption to (refMe's NSXMLNodePreserveCDATA as integer) + (refMe's NSXMLNodePreserveWhitespace as integer)
  set listReadXMLDoc to (refMe's NSXMLDocument's alloc()'s initWithData:(ocidReadData) options:(ocidOption) |error|:(reference))
  set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
  ##XML宣言があるか?チェック用にXMLとして読み込む
  set ocidOption to (refMe's NSXMLDocumentTidyXML)
  set listReadXMLFile to (refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
  set ocidReadXMLFile to (item 1 of listReadXMLFile)
  ###XML宣言の有無チェック
  set ocidError to (item 2 of listReadXMLFile)
  if ocidError = (missing value) then
log "svgタグから始まるpublic.svg-image xml宣言が無いタイプ"
    if boolDoXML is false then
log "XML宣言の無いSVGは処理しない"
      set listDone to (appFileManager's copyItemAtURL:(ocidFilePathURL) toURL:(ocidSaveFilePathURL) |error|:(reference))
      if (item 1 of listDone as boolean) is false then
        set ocidOption to (refMe's NSFileManagerItemReplacementWithoutDeletingBackupItem)
        set listDone to (appFileManager's replaceItemAtURL:(ocidSaveFilePathURL) withItemAtURL:(ocidFilePathURL) backupItemName:(missing value) options:(ocidOption) resultingItemURL:(missing value) |error|:(reference))
        set listDone to (appFileManager's copyItemAtURL:(ocidSaveFilePathURL) toURL:(ocidFilePathURL) |error|:(reference))
      end if
      
    else
      ##XML宣言が無い場合は新規でルートエレメントにテキストとして読み込み
      ##テキストで読み込みなおして
      set listReadStrings to (refMe's NSString's stringWithContentsOfURL:(ocidFilePathURL) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
      set ocidReadStrings to (item 1 of listReadStrings)
      ##ルートエレメントとしてエレメントにする
      set listReadSvgElement to (refMe's NSXMLElement's alloc()'s initWithXMLString:(ocidReadStrings) |error|:(reference))
      set ocidReadSvgElement to (item 1 of listReadSvgElement)
      ##ルートエレメントとして新規の保存用のXMLドキュメントにセットする
      set ocidReadXMLDoc to (refMe's NSXMLDocument's alloc()'s initWithRootElement:(ocidReadSvgElement))
(ocidReadXMLDoc's setVersion:"1.0")
(ocidReadXMLDoc's setCharacterEncoding:"UTF-8")
    end if
  else
log "xml宣言があるタイプ"
    ### DTD取得しておく
    set ocidReadDTD to ocidReadXMLDoc's DTD
  end if
  ###共通処理
  ###【2】ROOTエレメント
  set ocidRootElement to ocidReadXMLDoc's rootElement()
  ###【3】adobe_illustrator_pgfエレメント
  set ocidAiPgfArray to (ocidRootElement's elementsForName:("i:aipgf"))
  ##数を数えて
  set numCntAIPGF to (count of ocidAiPgfArray) as integer
  ##0なら編集機能はない
  if numCntAIPGF = 0 then
log "adobe_illustrator_pgfが見つかりませんので処理しません"
    ###そのままテキストで保存する この時XML宣言を付与する
    ###XML形式のテキストに出力
    set ocidSaveStrings to ocidReadXMLDoc's XMLString()
  else
log "adobe_illustrator_pgf イラストレーターの編集機能が見つかりました"
    ##ルートエレメントの子要素のArray
    set ocidChidArray to ocidRootElement's children()
    set numCntChild to 0 as integer
    ##子要素の数だけ繰り返し
    repeat with itemChild in ocidChidArray
      try
        ##子要素の名前を取得して
        set strChildName to itemChild's |name| as text
        ##adobe_illustrator_pgf なら削除する
        if strChildName is "i:aipgf" then
(ocidRootElement's removeChildAtIndex:(numCntChild))
        end if
      end try
      set numCntChild to numCntChild + 1 as integer
    end repeat
    ##adobe_illustrator_pgfを削除したXMLにDTDを追加して
(ocidReadXMLDoc's setDTD:(ocidReadDTD))
    ###テキストにする
    set ocidSaveStrings to ocidReadXMLDoc's XMLString()
  end if
  
  ###################################
  #####保存
  ###################################
  ###改行コードを指定して
(ocidSaveStrings's appendString:"\n")
  ##保存
  set listDone to (ocidSaveStrings's writeToURL:(ocidSaveFilePathURL) atomically:true encoding:(refMe's NSUTF8StringEncoding) |error|:(reference))
  
end repeat

##保存先を開く
# set appShardWorkspace to refMe's NSWorkspace's sharedWorkspace()
# set boolResults to appShardWorkspace's activateFileViewerSelectingURLs:({ocidSaveDirPathURL})

tell application "Finder"
make new Finder window to aliasSaveDirPathURL
activate
end tell
return


|

« [Youtube]ビデオIDから主要な情報を取得 | トップページ | [PDFoutline]しおり boookmarkをテキストに保存 »

AppleScript XML SVG」カテゴリの記事