« [Youtube API]チャンネルIDからチャンネルのアップロードビデオを取得する | トップページ | OPMLのxmlURLを参照して、ファイルが見つからない項目を削除する »

[NSXMLElement]エレメントのコピー


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

#!/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 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 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 aliasDefaultLocation to (ocidDesktopDirPathURL's absoluteURL()) as alias
tell application "Finder"
  set aliasDefaultLocation to container of (path to me) as alias
  set aliasDefaultLocation to (path to desktop folder from user domain) as alias
end tell

############UTIリスト
set listUTI to {"public.xml", "public.rss", "public.opml", "dyn.ah62d4rv4ge8086drru"}

set strMes to ("ファイルを選んでください") as text
set strPrompt to ("ファイルを選んでください") as text
try
  ### ファイル選択時
  set aliasFilePath to (choose file strMes with prompt strPrompt default location (aliasDefaultLocation) of type listUTI with invisibles and showing package contents without multiple selections allowed) as alias
on error
log "エラーしました"
return "エラーしました"
end try

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 ocidContainerDirPathURL to ocidFilePathURL's URLByDeletingLastPathComponent()
set ocidSaveFilePathURL to ocidContainerDirPathURL's URLByAppendingPathComponent:("edit.opml")
####################
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to (refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidFilePathURL) options:(ocidOption) |error|:(reference))
###XMLドキュメント
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###ROOTエレメント
set ocidRootElement to ocidReadXMLDoc's rootElement()
set ocidHeadElement to (ocidRootElement's elementsForName:("head"))'s firstObject()
set ocidHeadCopy to ocidHeadElement's XMLString()
###bodyエレメント
set ocidBody to (ocidRootElement's elementsForName:("body"))'s firstObject()
set ocidBodyCopy to ocidBody's XMLString()

####################
#【保存】XML初期化
set ocidSaveXMLDoc to refMe's NSXMLDocument's alloc()'s init()
ocidSaveXMLDoc's setVersion:("1.0")
ocidSaveXMLDoc's setCharacterEncoding:("utf-8")
ocidSaveXMLDoc's setDocumentContentKind:(refMe's NSXMLDocumentXMLKind)
#【保存】ROOT
set ocidSaveRootElement to refMe's NSXMLElement's elementWithName:("opml")
set ocidAddNode to refMe's NSXMLNode's attributeWithName:("version") stringValue:("1.0")
ocidSaveRootElement's addAttribute:(ocidAddNode)
#【保存】HEAD
set ocidSaveHeadElement to refMe's NSXMLElement's alloc()'s initWithXMLString:(ocidHeadCopy) |error|:(missing value)
#【保存】BODY
set ocidSaveBodyElement to refMe's NSXMLElement's alloc()'s initWithXMLString:(ocidBodyCopy) |error|:(missing value)




####################
#【保存】
ocidSaveRootElement's addChild:(ocidSaveHeadElement)
ocidSaveRootElement's addChild:(ocidSaveBodyElement)
ocidSaveXMLDoc's setRootElement:(ocidSaveRootElement)
##########
#【保存】読み取りやすい表示
set ocidXMLdata to ocidSaveXMLDoc's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)
#保存
set listDone to ocidXMLdata's writeToURL:(ocidSaveFilePathURL) options:(refMe's NSDataWritingAtomic) |error|:(reference)


|

« [Youtube API]チャンネルIDからチャンネルのアップロードビデオを取得する | トップページ | OPMLのxmlURLを参照して、ファイルが見つからない項目を削除する »

AppleScript XML」カテゴリの記事