XML RSS

[RSS] SoftpediaのRSSを取得してHTMLで表示する(URL修正)


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

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

### 【1】RSSのURL
#基本となるURL
set strURL to ("https://mac.softpedia.com/backend.xml") as text
#NSURLにして
set ocidBaseURL to refMe's NSURL's alloc()'s initWithString:(strURL)
#コンポーネントに分割
set ocidURLomponents to refMe's NSURLComponents's componentsWithURL:(ocidBaseURL) resolvingAgainstBaseURL:(false)
#クエリーアイテム
set ocidQueryItems to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
#クエリー定義
set ocidQueryItem to refMe's NSURLQueryItem's alloc()'s initWithName:("") value:("_all")
#クエリーをセット
ocidQueryItems's addObject:(ocidQueryItem)
#コンポーネントに追加
ocidURLomponents's setQueryItems:(ocidQueryItems)
#URLを再定義
set ocidURL to ocidURLomponents's |URL|
#ホスト
set strHost to ocidURL's |host|() as text

### 【2】RSS読み込み
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###【3】RSSから必要なデータを取得
#【3-1】:ROOT
#【3-2】:RSS
set ocidRootElement to ocidReadXMLDoc's rootElement()
#【3-3】:channel
#########【取得方法A】
##channelを要素として取得してから処理する
set ocidChannel to ocidRootElement's elementsForName:("channel")
set ocidChannelElement to ocidChannel's firstObject()
##タイトル
set strMainTitle to (ocidChannelElement's elementsForName:("title"))'s stringValue as text
##メインのリンク
set strMainLink to (ocidChannelElement's elementsForName:("link"))'s stringValue as text
##説明
set strDescription to (ocidChannelElement's elementsForName:("description"))'s stringValue as text
##言語
set strLanguage to (ocidChannelElement's elementsForName:("language"))'s stringValue as text
##発行日
set strPubDate to (ocidChannelElement's elementsForName:("lastBuildDate"))'s stringValue as text
######imageの子要素を取得するのでobjectValueは不要
set ocidImageArray to ocidChannelElement's elementsForName:("image")
set ocidImageDict to ocidImageArray's firstObject()
set strImageTitle to (ocidImageDict's elementsForName:("title"))'s stringValue as text
set strImageLink to (ocidImageDict's elementsForName:("link"))'s stringValue as text
set strImageURL to (ocidImageDict's elementsForName:("url"))'s stringValue as text
# set strImageWidth to (ocidImageDict's elementsForName:("width"))'s stringValue as text
# set strImageHeight to (ocidImageDict's elementsForName:("height"))'s stringValue as text
############pubDateの日付の処理
##日付を日本語表記に
set ocidPubDate to refMe's NSString's stringWithString:(strPubDate)
set ocidInputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidEnLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("en_US_POSIX")
ocidInputFormatter's setLocale:(ocidEnLocale)
ocidInputFormatter's setDateFormat:("EEE, dd MMM yyyy HH:mm:ss ZZZ")
set ocidEnDate to ocidInputFormatter's dateFromString:(ocidPubDate)
##
set ocidOutputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidJpLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP")
ocidOutputFormatter's setLocale:(ocidJpLocale)
ocidOutputFormatter's setDateFormat:("yyyy年MM月dd日EEEE")
set strJpDate to (ocidOutputFormatter's stringFromDate:(ocidEnDate)) as text

###【4】データをHTMLに整形
########################################
#headerに渡すエレメント
set ocidH3Element to refMe's NSXMLElement's elementWithName:("h3")
(ocidH3Element's setStringValue:("RSS表示"))
########################################
#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 ocidTableElement to refMe's NSXMLElement's elementWithName:("table")
#【caption】
set ocidCaptionElement to refMe's NSXMLElement's elementWithName:("caption")
set strSetValue to (strMainTitle & ": " & strJpDate) as text
ocidCaptionElement's setStringValue:(strSetValue)
ocidTableElement's addChild:(ocidCaptionElement)
#【colgroup】
set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup")
#テーブルのタイトル部
set listColName to {"行番号", "ジャンル", "名称", "内容", "リンク", "修正日"} 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
  #ここは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))
  #値を入れる
(ocidAddElement's setStringValue:(itemColName))
  #TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
end repeat
#TRをTHEADにセット
ocidTheadElement's addChild:(ocidTrElement)
#THEADをテーブルにセット
ocidTableElement's addChild:(ocidTheadElement)
########################################
#【tbody】
set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
###【3-4】:item
set ocidItemArray to (ocidChannelElement's elementsForName:("item"))
set numLineNO to 1 as integer
repeat with itemArray in ocidItemArray
  ###タイトル
  set ocidItemTitle to (itemArray's elementsForName:("title"))'s stringValue
  ###詳細
  set ocidItemDescription to (itemArray's elementsForName:("description"))'s stringValue
  ###category
  set ocidItemCategory to (itemArray's elementsForName:("category"))'s stringValue
  ###日付 英語表記でdateにしてから日本語表記にテキスト出力
  set ocidPubDate to (itemArray's elementsForName:("pubDate"))'s stringValue
  set ocidInputFormatter to refMe's NSDateFormatter's alloc()'s init()
  set ocidEnLocale to (refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("en_US_POSIX"))
(ocidInputFormatter's setLocale:(ocidEnLocale))
(ocidInputFormatter's setDateFormat:("EEE, dd MMM yyyy HH:mm:ss Z"))
  set ocidEnDate to (ocidInputFormatter's stringFromDate:(ocidPubDate))
  set ocidOutputFormatter to refMe's NSDateFormatter's alloc()'s init()
  set ocidJpLocale to (refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP"))
(ocidOutputFormatter's setLocale:(ocidJpLocale))
(ocidOutputFormatter's setDateFormat:("yyyy年MM月dd日EEEE"))
  set ocidJpDate to (ocidOutputFormatter's stringFromDate:(ocidEnDate))
  ###リンク
  set ocidItemLink to (itemArray's elementsForName:("link"))'s stringValue
  #TRの開始
  set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
  #【行番号】をTHでセット
  set strZeroSupp to ("00") as text
  set strZeroSupp to ("00" & numLineNO) as text
  set strLineNO to (text -3 through -1 of strZeroSupp) as text
  set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("行番号"))
(ocidThElement's addAttribute:(ocidAddNode))
(ocidThElement's setStringValue:(strLineNO))
(ocidTrElement's addChild:(ocidThElement))
  #【ジャンル】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ジャンル"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidItemCategory))
(ocidTrElement's addChild:(ocidTdElement))
  #【ジャンル】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("タイトル"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidItemTitle))
(ocidTrElement's addChild:(ocidTdElement))
  #【詳細】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("詳細"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidItemDescription))
(ocidTrElement's addChild:(ocidTdElement))
  #【リンク】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("リンク"))
(ocidTdElement's addAttribute:(ocidAddNode))
  #
  set ocidAElement to (refMe's NSXMLElement's elementWithName:("a"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(ocidItemLink))
(ocidAElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidAElement's addAttribute:(ocidAddNode))
(ocidAElement's setStringValue:("LINK"))
  #リンクをTDにセット
(ocidTdElement's addChild:(ocidAElement))
(ocidTrElement's addChild:(ocidTdElement))
  #【更新日】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("更新日"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidJpDate))
(ocidTrElement's addChild:(ocidTdElement))
  #出来上がったTRをTBODYにセット
(ocidTbodyElement's addChild:(ocidTrElement))
  set numLineNO to numLineNO + 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 ocidTfotterAElement to refMe's NSXMLElement's elementWithName:("a")
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strMainLink))
(ocidTfotterAElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidTfotterAElement's addAttribute:(ocidAddNode))
set strContents to ("ソフトぺディアMac") as text
(ocidTfotterAElement's setStringValue:(strContents))
ocidThElement's addChild:(ocidTfotterAElement)
#THをTRにセットして
ocidTrElement's addChild:(ocidThElement)
#TRをTFOOTにセット
ocidTfootElement's addChild:(ocidTrElement)
#TFOOTをテーブルにセット
ocidTableElement's addChild:(ocidTfootElement)

##############################
#HTMLにする
##############################
set ocidHTML to doMakeRootElement({ocidH3Element, ocidTableElement, ocidFotterAElement})

##############################
#保存
##############################
#####【5−1】保存先ディレクトリの確保
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
#上書きできないようにUUIDで別名フォルダにする
set ocidUUID to refMe's NSUUID's alloc()'s init()
set ocidUUIDString to ocidUUID's UUIDString
set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
#アクセス権777=511を指定
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
#保存ファイル名
set strFileName to (strHost & ".html") as text
#保存先URL
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false

#####【5−2】保存
#読み取りやすい表示
set ocidXMLdata to ocidHTML's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)

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

##############################
# 基本的な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:("RSS一覧")
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:("http-equiv") stringValue:("Content-Style-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/css")
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:("http-equiv") stringValue:("Content-Script-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/javascript")
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 { 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 td { word-wrap: break-word;max-width: 360px;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 th { border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; } 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 } }")
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)
  
ocidRootElement's addChild:(ocidBodyElement)
ocidXMLDoc's setRootElement:(ocidRootElement)
return ocidXMLDoc
end doMakeRootElement

|

[RSS]新しいもの好きのRSSの取得とHTML表示(CSS修正)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
【1】RSSのURL
【2】RSS読み込み
【3】RSSから必要なデータを取得
【4】データをHTMLに整形
【5】保存
【6】ブラウザで開く

【3】RSSから必要なデータを取得
【3-1】:ROOT
【3-2】:RSS
【3-3】:channel
【3-4】:item

【4】データをHTMLに整形
【4−1】ヘッダー部等
【4−2】itemの追加
【4−3】整形終了


【5】保存
【5−1】保存先ディレクトリの確保
【5−2】保存

com.cocolog-nifty.quicktimer.icefloe
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application

### 【1】RSSのURL
set strURL to ("https://macsoft.jp/feed/") as text
set ocidURL to refMe's NSURL's alloc()'s initWithString:(strURL)
set strHost to ocidURL's |host|() as text

### 【2】RSS読み込み
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###【3】RSSから必要なデータを取得
#【3-1】:ROOT
#【3-2】:RSS
set ocidRootElement to ocidReadXMLDoc's rootElement()
#【3-3】:channel
#########【取得方法A】
##channelを要素として取得してから処理する
set ocidChannel to ocidRootElement's elementsForName:("channel")
set ocidChannelElement to ocidChannel's firstObject()
##タイトル
set strMainTitle to (ocidChannelElement's elementsForName:("title"))'s stringValue as text
##メインのリンク
set strMainLink to (ocidChannelElement's elementsForName:("link"))'s stringValue as text
##説明
set strDescription to (ocidChannelElement's elementsForName:("description"))'s stringValue as text
##言語
set strLanguage to (ocidChannelElement's elementsForName:("language"))'s stringValue as text
##発行日
set strPubDate to (ocidChannelElement's elementsForName:("lastBuildDate"))'s stringValue as text
######imageの子要素を取得するのでobjectValueは不要
set ocidImageArray to ocidChannelElement's elementsForName:("image")
set ocidImageDict to ocidImageArray's firstObject()
set strImageTitle to (ocidImageDict's elementsForName:("title"))'s stringValue as text
set strImageLink to (ocidImageDict's elementsForName:("link"))'s stringValue as text
set strImageURL to (ocidImageDict's elementsForName:("url"))'s stringValue as text
set strImageWidth to (ocidImageDict's elementsForName:("width"))'s stringValue as text
set strImageHeight to (ocidImageDict's elementsForName:("height"))'s stringValue as text
############pubDateの日付の処理
##日付を日本語表記に
set ocidPubDate to refMe's NSString's stringWithString:(strPubDate)
set ocidInputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidEnLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("en_US_POSIX")
ocidInputFormatter's setLocale:(ocidEnLocale)
ocidInputFormatter's setDateFormat:("EEE, dd MMM yyyy HH:mm:ss ZZZ")
set ocidEnDate to ocidInputFormatter's dateFromString:(ocidPubDate)
##
set ocidOutputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidJpLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP")
ocidOutputFormatter's setLocale:(ocidJpLocale)
ocidOutputFormatter's setDateFormat:("yyyy年MM月dd日EEEE")
set strJpDate to (ocidOutputFormatter's stringFromDate:(ocidEnDate)) as text

###【4】データをHTMLに整形
########################################
##HTML 基本構造
#【4−1】ヘッダー部等
set strStylle to "<style>body {margin: 10px;background-color: #FFFFFF;}#bordertable {padding: 10px;width: 100%;margin: 0;border-collapse: collapse;border-spacing: 0;word-wrap: break-word;}table {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 td {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 th {border-spacing: 0;border: solid 1px #666666;padding: .5ch 1ch;border-block-width: 1px 0;border-inline-width: 1px 0;}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}}</style>" as text
###ヘッダー部
#タイトル挿入
set strHead to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>" & strMainTitle & "</title>" & strStylle & "</head><body>" as text
###最後
set strHtmlEndBody to "</body></html>"
###HTML書き出し用のテキスト初期化
set ocidHTMLString to refMe's NSMutableString's alloc()'s initWithCapacity:0
####
ocidHTMLString's appendString:(strHead)
###テーブルの開始部
set strHTML to ("<div id=\"bordertable\"><table><caption title=\"タイトル\"><a href=\"" & strMainLink & "\" target=\"_blank\">" & strMainTitle & "</a> : " & strJpDate & " </caption><thead><tr><th title=\"連番\" scope=\"row\"> </th><th title=\"ジャンル\">ジャンル</th><th title=\"バージョン\">バージョン</th><th title=\"ディスクリプション\">内容</th><th title=\"リンク\">LINK</th><th title=\"更新日\">更新日</th></tr></thead><tbody>") as text
ocidHTMLString's appendString:(strHTML)
########################################
###【3-4】:item
set ocidItemArray to (ocidChannelElement's elementsForName:("item"))
set strLineNO to 1 as integer
repeat with itemArray in ocidItemArray
  ###タイトル
  set strItemTitle to (itemArray's elementsForName:("title"))'s stringValue as text
  ###詳細
  set strItemDescription to (itemArray's elementsForName:("description"))'s stringValue as text
  set ocidItemDescription to (refMe's NSString's stringWithString:(strItemDescription))
  set ocidItemDescription to (ocidItemDescription's stringByReplacingOccurrencesOfString:("<p>Copyright &copy; 2024 <a href=\"https://macsoft.jp\">新しもの好きのダウンロード</a> All Rights Reserved.</p>") withString:(""))
  set strItemDescription to ocidItemDescription as text
  ###category
  set ocidItemCategoryArray to (itemArray's elementsForName:("category"))
  set strCategory to ("") as text
  repeat with itemCategory in ocidItemCategoryArray
    set strItemCategory to itemCategory's stringValue as text
    set strCategory to (strCategory & strItemCategory & "<hr />") as text
  end repeat
  
  ###日付 英語表記でdateにしてから日本語表記にテキスト出力
  set strItemPubDate to (itemArray's elementsForName:("pubDate"))'s stringValue as text
  set ocidPubDate to (refMe's NSString's stringWithString:(strItemPubDate))
  set ocidInputFormatter to refMe's NSDateFormatter's alloc()'s init()
  set ocidEnLocale to (refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("en_US_POSIX"))
(ocidInputFormatter's setLocale:(ocidEnLocale))
(ocidInputFormatter's setDateFormat:("EEE, dd MMM yyyy HH:mm:ss ZZZ"))
  set ocidEnDate to (ocidInputFormatter's dateFromString:(ocidPubDate))
  set ocidOutputFormatter to refMe's NSDateFormatter's alloc()'s init()
  set ocidJpLocale to (refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP"))
(ocidOutputFormatter's setLocale:(ocidJpLocale))
(ocidOutputFormatter's setDateFormat:("yyyy年MM月dd日EEEE"))
  set strJpDate to (ocidOutputFormatter's stringFromDate:(ocidEnDate)) as text
  ###リンク
  set strItemLink to (itemArray's elementsForName:("link"))'s stringValue as text
  ##HTML整形
  set strHTML to ("<tr><th title=\"連番\" scope=\"row\">" & strLineNO & "</th><td title=\"カテゴリ\">" & strCategory & "</td><td title=\"タイトル\">" & strItemTitle & "</td><td title=\"詳細\">" & strItemDescription & "</td><td title=\"リンク\"><a href=\"" & strItemLink & "\" target=\"_blank\">LINK</a></td><td title=\"日付\">" & strJpDate & "</td></tr>") as text
  ##↑のHTMLを出力用のテキストに積み上げていく
(ocidHTMLString's appendString:(strHTML))
  set strLineNO to strLineNO + 1 as integer
end repeat

set strHTML to ("</tbody><tfoot><tr><th colspan=\"6\" title=\"フッター表の終わり\" scope=\"row\"><a href=\"https://quicktimer.cocolog-nifty.com/\" target=\"_blank\">AppleScriptで生成しました</a></th></tr></tfoot></table></div>") as text
####テーブルまでを追加
(ocidHTMLString's appendString:(strHTML))
####終了部を追加
(ocidHTMLString's appendString:(strHtmlEndBody))

#####【5−1】保存先ディレクトリの確保
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
#上書きできないようにUUIDで別名フォルダにする
set ocidUUID to refMe's NSUUID's alloc()'s init()
set ocidUUIDString to ocidUUID's UUIDString
set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
#アクセス権777=511を指定
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
#保存ファイル名
set strFileName to (strHost & ".html") as text
#保存先URL
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false

#####【5−2】保存
set listDone to ocidHTMLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)

####【6】ブラウザで開く
set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
tell application "Finder"
open location aliasFilePath
end tell



|

[RSS] SoftpediaのRSSを取得してHTMLで表示する


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

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

### 【1】RSSのURL
set strURL to ("https://mac.softpedia.com/backend.xml") as text
set ocidURL to refMe's NSURL's alloc()'s initWithString:(strURL)
set strHost to ocidURL's |host|() as text

### 【2】RSS読み込み
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###【3】RSSから必要なデータを取得
#【3-1】:ROOT
#【3-2】:RSS
set ocidRootElement to ocidReadXMLDoc's rootElement()
#【3-3】:channel
#########【取得方法A】
##channelを要素として取得してから処理する
set ocidChannel to ocidRootElement's elementsForName:("channel")
set ocidChannelElement to ocidChannel's firstObject()
##タイトル
set strMainTitle to (ocidChannelElement's elementsForName:("title"))'s stringValue as text
##メインのリンク
set strMainLink to (ocidChannelElement's elementsForName:("link"))'s stringValue as text
##説明
set strDescription to (ocidChannelElement's elementsForName:("description"))'s stringValue as text
##言語
set strLanguage to (ocidChannelElement's elementsForName:("language"))'s stringValue as text
##発行日
set strPubDate to (ocidChannelElement's elementsForName:("lastBuildDate"))'s stringValue as text
######imageの子要素を取得するのでobjectValueは不要
set ocidImageArray to ocidChannelElement's elementsForName:("image")
set ocidImageDict to ocidImageArray's firstObject()
set strImageTitle to (ocidImageDict's elementsForName:("title"))'s stringValue as text
set strImageLink to (ocidImageDict's elementsForName:("link"))'s stringValue as text
set strImageURL to (ocidImageDict's elementsForName:("url"))'s stringValue as text
# set strImageWidth to (ocidImageDict's elementsForName:("width"))'s stringValue as text
# set strImageHeight to (ocidImageDict's elementsForName:("height"))'s stringValue as text
############pubDateの日付の処理
##日付を日本語表記に
set ocidPubDate to refMe's NSString's stringWithString:(strPubDate)
set ocidInputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidEnLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("en_US_POSIX")
ocidInputFormatter's setLocale:(ocidEnLocale)
ocidInputFormatter's setDateFormat:("EEE, dd MMM yyyy HH:mm:ss ZZZ")
set ocidEnDate to ocidInputFormatter's dateFromString:(ocidPubDate)
##
set ocidOutputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidJpLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP")
ocidOutputFormatter's setLocale:(ocidJpLocale)
ocidOutputFormatter's setDateFormat:("yyyy年MM月dd日EEEE")
set strJpDate to (ocidOutputFormatter's stringFromDate:(ocidEnDate)) as text

###【4】データをHTMLに整形
########################################
#headerに渡すエレメント
set ocidH3Element to refMe's NSXMLElement's elementWithName:("h3")
(ocidH3Element's setStringValue:("RSS表示"))
########################################
#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 ocidTableElement to refMe's NSXMLElement's elementWithName:("table")
#【caption】
set ocidCaptionElement to refMe's NSXMLElement's elementWithName:("caption")
set strSetValue to (strMainTitle & ": " & strJpDate) as text
ocidCaptionElement's setStringValue:(strSetValue)
ocidTableElement's addChild:(ocidCaptionElement)
#【colgroup】
set ocidColgroupElement to refMe's NSXMLElement's elementWithName:("colgroup")
#テーブルのタイトル部
set listColName to {"行番号", "ジャンル", "名称", "内容", "リンク", "修正日"} 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
  #ここは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))
  #値を入れる
(ocidAddElement's setStringValue:(itemColName))
  #TH→TRにセット
(ocidTrElement's addChild:(ocidAddElement))
end repeat
#TRをTHEADにセット
ocidTheadElement's addChild:(ocidTrElement)
#THEADをテーブルにセット
ocidTableElement's addChild:(ocidTheadElement)
########################################
#【tbody】
set ocidTbodyElement to refMe's NSXMLElement's elementWithName:("tbody")
###【3-4】:item
set ocidItemArray to (ocidChannelElement's elementsForName:("item"))
set numLineNO to 1 as integer
repeat with itemArray in ocidItemArray
  ###タイトル
  set ocidItemTitle to (itemArray's elementsForName:("title"))'s stringValue
  ###詳細
  set ocidItemDescription to (itemArray's elementsForName:("description"))'s stringValue
  ###category
  set ocidItemCategory to (itemArray's elementsForName:("category"))'s stringValue
  ###日付 英語表記でdateにしてから日本語表記にテキスト出力
  set ocidPubDate to (itemArray's elementsForName:("pubDate"))'s stringValue
  set ocidInputFormatter to refMe's NSDateFormatter's alloc()'s init()
  set ocidEnLocale to (refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("en_US_POSIX"))
(ocidInputFormatter's setLocale:(ocidEnLocale))
(ocidInputFormatter's setDateFormat:("EEE, dd MMM yyyy HH:mm:ss Z"))
  set ocidEnDate to (ocidInputFormatter's stringFromDate:(ocidPubDate))
  set ocidOutputFormatter to refMe's NSDateFormatter's alloc()'s init()
  set ocidJpLocale to (refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP"))
(ocidOutputFormatter's setLocale:(ocidJpLocale))
(ocidOutputFormatter's setDateFormat:("yyyy年MM月dd日EEEE"))
  set ocidJpDate to (ocidOutputFormatter's stringFromDate:(ocidEnDate))
  ###リンク
  set ocidItemLink to (itemArray's elementsForName:("link"))'s stringValue
  #TRの開始
  set ocidTrElement to (refMe's NSXMLElement's elementWithName:("tr"))
  #【行番号】をTHでセット
  set strZeroSupp to ("00") as text
  set strZeroSupp to ("00" & numLineNO) as text
  set strLineNO to (text -3 through -1 of strZeroSupp) as text
  set ocidThElement to (refMe's NSXMLElement's elementWithName:("th"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("行番号"))
(ocidThElement's addAttribute:(ocidAddNode))
(ocidThElement's setStringValue:(strLineNO))
(ocidTrElement's addChild:(ocidThElement))
  #【ジャンル】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("ジャンル"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidItemCategory))
(ocidTrElement's addChild:(ocidTdElement))
  #【ジャンル】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("タイトル"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidItemTitle))
(ocidTrElement's addChild:(ocidTdElement))
  #【詳細】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("詳細"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidItemDescription))
(ocidTrElement's addChild:(ocidTdElement))
  #【リンク】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("リンク"))
(ocidTdElement's addAttribute:(ocidAddNode))
  #
  set ocidAElement to (refMe's NSXMLElement's elementWithName:("a"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(ocidItemLink))
(ocidAElement's addAttribute:(ocidAddNode))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidAElement's addAttribute:(ocidAddNode))
(ocidAElement's setStringValue:("LINK"))
  #リンクをTDにセット
(ocidTdElement's addChild:(ocidAElement))
(ocidTrElement's addChild:(ocidTdElement))
  #【更新日】をTDでセット
  set ocidTdElement to (refMe's NSXMLElement's elementWithName:("td"))
  set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("title") stringValue:("更新日"))
(ocidTdElement's addAttribute:(ocidAddNode))
(ocidTdElement's setStringValue:(ocidJpDate))
(ocidTrElement's addChild:(ocidTdElement))
  #出来上がったTRをTBODYにセット
(ocidTbodyElement's addChild:(ocidTrElement))
  set numLineNO to numLineNO + 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 ocidTfotterAElement to refMe's NSXMLElement's elementWithName:("a")
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("href") stringValue:(strMainLink))
(ocidTfotterAElement's addAttribute:(ocidAddNode))
set ocidAddNode to (refMe's NSXMLNode's attributeWithName:("target") stringValue:("_blank"))
(ocidTfotterAElement's addAttribute:(ocidAddNode))
set strContents to ("ソフトぺディアMac") as text
(ocidTfotterAElement's setStringValue:(strContents))
ocidThElement's addChild:(ocidTfotterAElement)
#THをTRにセットして
ocidTrElement's addChild:(ocidThElement)
#TRをTFOOTにセット
ocidTfootElement's addChild:(ocidTrElement)
#TFOOTをテーブルにセット
ocidTableElement's addChild:(ocidTfootElement)

##############################
#HTMLにする
##############################
set ocidHTML to doMakeRootElement({ocidH3Element, ocidTableElement, ocidFotterAElement})

##############################
#保存
##############################
#####【5−1】保存先ディレクトリの確保
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
#上書きできないようにUUIDで別名フォルダにする
set ocidUUID to refMe's NSUUID's alloc()'s init()
set ocidUUIDString to ocidUUID's UUIDString
set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
#アクセス権777=511を指定
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
#保存ファイル名
set strFileName to (strHost & ".html") as text
#保存先URL
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false

#####【5−2】保存
#読み取りやすい表示
set ocidXMLdata to ocidHTML's XMLDataWithOptions:(refMe's NSXMLNodePrettyPrint)

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

##############################
# 基本的な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:("RSS一覧")
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:("http-equiv") stringValue:("Content-Style-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/css")
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:("http-equiv") stringValue:("Content-Script-Type")
ocidAddElement's addAttribute:(ocidAddNode)
  set ocidAddNode to refMe's NSXMLNode's attributeWithName:("content") stringValue:("text/javascript")
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 { 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 td { word-wrap: break-word;max-width: 360px;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 th { border-spacing: 0; border: solid 1px #666666; padding: .5ch 1ch; border-block-width: 1px 0; border-inline-width: 1px 0; } 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 } }")
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)
  
ocidRootElement's addChild:(ocidBodyElement)
ocidXMLDoc's setRootElement:(ocidRootElement)
return ocidXMLDoc
end doMakeRootElement

|

[System Events]RSSを取得してテキスト表示する


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

#!/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

set strURL to ("https://www.nhk.or.jp/rss/news/cat0.xml") as text


########################
##【1】RSSのダウンロード
###最近の仕様でtemporary items folderを要求するとフォルダ生成される
tell application "System Events"
  set aliasDownloadsDirPath to (path to downloads folder from user domain) as alias
end tell
###念の為フォルダ作っておく
tell application "Finder"
  set boolExists to exists (folder "RssXml" of folder aliasDownloadsDirPath)
  if boolExists is false then
make new folder at aliasDownloadsDirPath with properties {name:"RssXml"}
  end if
  set aliasSaveDirPath to (folder "RssXml" of folder aliasDownloadsDirPath) as alias
  ##ランダムな番号をフォルダ名にして
  set strDirName to (random number from 100000 to 999999) as text
make new folder at aliasSaveDirPath with properties {name:strDirName}
  set aliasRssSaveDirPath to (folder strDirName of folder aliasSaveDirPath) as alias
end tell
set strSaveDirPath to (POSIX path of aliasRssSaveDirPath) as text
set strSaveFilePath to (strSaveDirPath & "cat0.xml") as text
###
set strCommandText to ("/usr/bin/curl -L \"" & strURL & "\" -o \"" & strSaveFilePath & "\"") as text
do shell script strCommandText
##
delay 1
########################
##【2】RSSの解析
set strOutPutText to ("") as text

tell application "System Events"
  tell XML file strSaveFilePath
    tell XML element "rss"
      tell XML element "channel"
        set listEveryItem to every XML element
        repeat with itemElement in listEveryItem
          tell itemElement
set listItemElement to every XML element
repeat with itemItemElement in listItemElement
tell itemItemElement
set strName to name as text
if strName is "title" then
set strValue to ("タイトル: " & value) as text
set strOutPutText to (strOutPutText & strValue & "\n") as text
else if strName is "link" then
set strValue to ("リンク: " & value) as text
set strOutPutText to (strOutPutText & strValue & "\n") as text
else if strName is "description" then
set strValue to ("詳細: " & value) as text
set strOutPutText to (strOutPutText & strValue & "\n") as text
end if
end tell
end repeat
          end tell
          set strOutPutText to (strOutPutText & "\n") as text
        end repeat
      end tell
    end tell
  end tell
end tell
set strOutPutText to strOutPutText as text
########################
##【3】テキスト表示
#ファイル名
set strFileName to ("index.html") as text
set strSaveHTMLFilePath to (strSaveDirPath & strFileName) as text
log strSaveHTMLFilePath
tell application "TextEdit"
activate
  set objNewDoc to make new document with properties {name:strFileName, path:strSaveHTMLFilePath}
  tell objNewDoc
activate
save in (POSIX file strSaveHTMLFilePath)
close saving yes
  end tell
end tell
tell application "TextEdit"
open (POSIX file strSaveHTMLFilePath)
  tell objNewDoc
    set its text to strOutPutText
    tell its text
properties
      set its font to "sans"
      set its color to {0, 0, 0}
      set its size to 16
    end tell
  end tell
  tell objNewDoc
save in (POSIX file strSaveHTMLFilePath)
  end tell
end tell

tell application "Finder"
make new Finder window to aliasSaveDirPath
activate
end tell

return


|

[RSS]MacUpdateの最新RSS25件の取得と表示(不具合修正)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
MacUpdateの最新25項目をHTMLで表示
com.cocolog-nifty.quicktimer.icefloe
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application

### 【1】RSSのURL
set strURL to ("https://www.macupdate.com/rss") as text
set ocidURL to refMe's NSURL's alloc()'s initWithString:(strURL)
### 【2】RSS読み込み
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###【3】ROOTエレメント
set ocidRootElement to ocidReadXMLDoc's rootElement()
###【4】channelエレメント
########タイトル(取得方法A)
set ocidChannel to ocidRootElement's elementsForName:("channel")
set ocidChannelElement to ocidChannel's firstObject()
set strMainTitle to (ocidChannelElement's elementsForName:("title"))'s stringValue as text
log strMainTitle
##メインのリンク
set strMainLink to (ocidChannelElement's elementsForName:("link"))'s stringValue as text

########発行日(取得方法B)
set listPubDate to ocidRootElement's nodesForXPath:("//channel/pubDate") |error|:(reference)
set strPubDate to (item 1 of listPubDate)'s stringValue as text
##日付を日本語表記に
set ocidPubDate to refMe's NSString's stringWithString:(strPubDate)
set ocidInputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidEnLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("en_US_POSIX")
ocidInputFormatter's setLocale:(ocidEnLocale)
ocidInputFormatter's setDateFormat:("EEE, dd MMM yyyy HH:mm:ss ZZZ")
set ocidEnDate to ocidInputFormatter's dateFromString:(ocidPubDate)
##
set ocidOutputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidJpLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP")
ocidOutputFormatter's setLocale:(ocidJpLocale)
ocidOutputFormatter's setDateFormat:("yyyy年MM月dd日EEEE")
set strJpDate to (ocidOutputFormatter's stringFromDate:(ocidEnDate)) as text

###【5】生成するHTMLパーツ
########################################
##HTML 基本構造
###スタイル
set strStylle to "<style>#bordertable {padding: 10px;width: 100%;margin: 0;border-collapse: collapse;border-spacing: 0;word-wrap: break-word;}table {width: 80%;margin: 0px;padding: 0px;border: 0px;border-spacing: 0px;border-collapse: collapse;}caption {font-weight: 900;}thead {font-weight: 600;border-spacing: 0px;}td {border: solid 1px #666666;padding: 5px;margin: 0px;word-wrap: break-word;border-spacing: 0px;}tr {border: solid 1px #666666;padding: 0px;margin: 0px;border-spacing: 0px;}th {border: solid 1px #666666;padding: 0px;margin: 0px;border-spacing: 0px;}tr:nth-of-type(odd) {background: #F2F2F2;}</style>" as text
###ヘッダー部
set strHead to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>" & strMainTitle & "</title>" & strStylle & "</head><body>" as text
###最後
set strHtmlEndBody to "</body></html>"
###HTML書き出し用のテキスト初期化
set ocidHTMLString to refMe's NSMutableString's alloc()'s initWithCapacity:0
####
ocidHTMLString's appendString:(strHead)
###テーブルの開始部
set strHTML to ("<div id=\"bordertable\"><table><caption title=\"タイトル\"><a href=\"" & strMainLink & "\" target=\"_blank\">" & strMainTitle & "</a> : " & strJpDate & " </caption>") as text
ocidHTMLString's appendString:(strHTML)
set strHTML to ("<thead><tr><th title=\"連番\" scope=\"row\"> </th><th title=\"タイトル\">名称</th><th title=\"バージョン\">バージョン</th><th title=\"ディスクリプション\">内容</th><th title=\"リンク\">LINK</th></tr></thead>") as text
ocidHTMLString's appendString:(strHTML)
set strHTML to ("<tbody>") as text
ocidHTMLString's appendString:(strHTML)
###【6】itemエレメント
set ocidItemArray to (ocidChannelElement's elementsForName:("item"))
set strLineNO to 1 as integer
repeat with itemArray in ocidItemArray
  ###タイトル
  set strItemTitle to (itemArray's elementsForName:("title"))'s stringValue as text
  ###タイトルを 名称 バージョン 詳細 に分離
  set ocidItemTitle to (refMe's NSString's stringWithString:(strItemTitle))
  set ocidItemTitleArray to (ocidItemTitle's componentsSeparatedByString:(" - "))
  set strDiscription to (ocidItemTitleArray's lastObject()) as text
  set strAppName to (ocidItemTitleArray's firstObject()) as text
  set ocidAppName to (refMe's NSString's stringWithString:(strAppName))
  set ocidAppNameArray to (ocidAppName's componentsSeparatedByString:(" "))
  set strVersIon to (ocidAppNameArray's lastObject()) as text
  set numCntArrya to count of ocidAppNameArray
  if numCntArrya = 2 then
    set strAppName to (ocidAppNameArray's componentsJoinedByString:(""))
  else
ocidAppNameArray's removeLastObject()
    set strAppName to (ocidAppNameArray's componentsJoinedByString:(""))
  end if
  ###リンクからAppIDを取得
  set strItemLink to (itemArray's elementsForName:("link"))'s stringValue as text
  set ocidItemURL to (refMe's NSURL's alloc()'s initWithString:(strItemLink))
  set ocidAppURL to ocidItemURL's URLByDeletingLastPathComponent()
  set strAppID to (ocidAppURL's lastPathComponent()) as text
  
  set strHTML to ("<tr><th title=\"連番\" scope=\"row\">" & strLineNO & "</th><td title=\"タイトル\">" & strAppName & "</td><td title=\"バージョン\">" & strVersIon & "</td><td title=\"ディスクリプション\">" & strDiscription & "</td><td title=\"リンク\"><a href=\"" & strItemLink & "\" target=\"_blank\">LINK</a></td></tr>") as text
(ocidHTMLString's appendString:(strHTML))
  set strLineNO to strLineNO + 1 as integer
end repeat

set strHTML to ("</tbody><tfoot><tr><th colspan=\"5\" title=\"フッター表の終わり\" scope=\"row\"><a href=\"https://quicktimer.cocolog-nifty.com/\" target=\"_blank\">AppleScriptで生成しました</a></th></tr></tfoot></table></div>") as text
####テーブルまでを追加
(ocidHTMLString's appendString:(strHTML))
####終了部を追加
(ocidHTMLString's appendString:(strHtmlEndBody))

#####【7】保存先ディレクトリとURLを生成しておく
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
set ocidUUID to refMe's NSUUID's alloc()'s init()
set ocidUUIDString to ocidUUID's UUIDString
set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
###パス
set strFileName to ("macupdate.html") as text
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false

#####【8】HTMLをファイルにする
set listDone to ocidHTMLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)

####【9】Finderで開く
set strFilePath to ocidSaveFilePathURL's |path| as text
set strFilePathURL to ocidSaveFilePathURL's absoluteString() as text
set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias

tell application "Finder"
open location aliasFilePath
end tell



|

気象庁のXMLの受信(少し修正)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
#
# 気象庁のXMLを受信してHTMLにして出力します
# https://xml.kishou.go.jp/xmlpull.html
#
# com.cocolog-nifty.quicktimer.icefloe
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
##自分環境がos12なので2.8にしているだけです
use AppleScript version "2.8"
use framework "Foundation"
use scripting additions

property refMe : a reference to current application



### 【1】RSSのURL
############################
set ocidXmlKishouDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:(0)
#長期(定時)
set strBaseURL to ("https://www.data.jma.go.jp/developer/xml/feed/regular_l.xml") as text
ocidXmlKishouDict's setValue:(strBaseURL) forKey:("長期(定時)")
#長期(随時)
set strBaseURL to ("https://www.data.jma.go.jp/developer/xml/feed/extra_l.xml") as text
ocidXmlKishouDict's setValue:(strBaseURL) forKey:("長期(随時)")
#長期(地震火山)
set strBaseURL to ("https://www.data.jma.go.jp/developer/xml/feed/eqvol_l.xml") as text
ocidXmlKishouDict's setValue:(strBaseURL) forKey:("長期(地震火山)")
#長期(その他)
set strBaseURL to ("https://www.data.jma.go.jp/developer/xml/feed/other_l.xml") as text
ocidXmlKishouDict's setValue:(strBaseURL) forKey:("長期(その他)")
###########################
#高頻度(定時)
set strBaseURL to ("https://www.data.jma.go.jp/developer/xml/feed/regular.xml") as text
ocidXmlKishouDict's setValue:(strBaseURL) forKey:("高頻度(定時)")
#高頻度(随時)
set strBaseURL to ("https://www.data.jma.go.jp/developer/xml/feed/extra.xml") as text
ocidXmlKishouDict's setValue:(strBaseURL) forKey:("高頻度(随時)")
#高頻度(その他)
set strBaseURL to ("https://www.data.jma.go.jp/developer/xml/feed/other.xml") as text
ocidXmlKishouDict's setValue:(strBaseURL) forKey:("高頻度(その他)")
#高頻度(地震火山)
set strBaseURL to ("https://www.data.jma.go.jp/developer/xml/feed/eqvol.xml") as text
ocidXmlKishouDict's setValue:(strBaseURL) forKey:("高頻度(地震火山)")
#
set ocidAllKeys to ocidXmlKishouDict's allKeys()
set ocidDescriptor to refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(false) selector:"localizedStandardCompare:"
set ocidDescriptorArray to refMe's NSArray's arrayWithObject:(ocidDescriptor)
set ocidSortedArray to ocidAllKeys's sortedArrayUsingDescriptors:(ocidDescriptorArray)
set listAllKeys to ocidSortedArray as list

###ダイアログ
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
try
  set listResponse to (choose from list listAllKeys with title "選んでください" with prompt "取得するXMLを選んでください" default items (item 1 of listAllKeys) OK button name "OK" cancel button name "キャンセル" without multiple selections allowed and empty selection allowed) as list
on error
log "エラーしました"
return "エラーしました"
end try
if (item 1 of listResponse) is false then
return "キャンセルしました"
end if
set strResponse to (item 1 of listResponse) as text

####
set strBaseURL to (ocidXmlKishouDict's valueForKey:(strResponse))


set ocidURL to refMe's NSURL's alloc()'s initWithString:(strBaseURL)
### 【2】RSS読み込み
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
###XMLドキュメント
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###ROOTエレメント
set ocidRootElement to ocidReadXMLDoc's rootElement()
set strMainTitle to (ocidRootElement's elementsForName:("title"))'s stringValue as text
set strUpdate to (ocidRootElement's elementsForName:("updated"))'s stringValue as text
###
set listEntryArray to ocidRootElement's nodesForXPath:("//entry") |error|:(reference)
set ocidEntryArray to (item 1 of listEntryArray)
set numChild to (count of ocidEntryArray) as integer
log numChild & "件の情報があります"
###エントリーの数だけ繰り返す


########################################
##HTML 基本構造
###スタイル
set strStylle to "<style>#bordertable {padding: 10px;width: 100%;margin: 0;border-collapse: collapse;border-spacing: 0;word-wrap: break-word;} #bordertable table { width: 80%;margin: 0px;padding: 0px;border: 0px;border-spacing:0px;border-collapse: collapse;} #bordertable caption { font-weight: 900;} #bordertable thead { font-weight: 600;border-spacing:0px;} #bordertable td {border: solid 1px #666666;padding: 5px;margin: 0px;word-wrap: break-word;border-spacing:0px;} #bordertable tr {border: solid 1px #666666;padding: 0px;margin: 0px;border-spacing:0px;} #bordertable th {border: solid 1px #666666;padding: 0px;margin: 0px;border-spacing:0px;}</style>"
###ヘッダー部
set strHead to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>[eqvol.xml]" & strMainTitle & "</title>" & strStylle & "</head><body>"
###最後
set strHtmlEndBody to "</body></html>"
###HTML書き出し用のテキスト初期化
set ocidHTMLString to refMe's NSMutableString's alloc()'s initWithCapacity:0
####
(ocidHTMLString's appendString:strHead)

#########
###テーブルの開始部
set strHTML to ("<div id=\"bordertable\"><table><caption title=\"タイトル\">" & strMainTitle & "[地震]<a href=\"https://www.jma.go.jp/bosai/map.html#contents=earthquake_map\" target=\"_blank\">LINK</a> [噴火]<a href=\"https://www.jma.go.jp/bosai/map.html#contents=volcano\" target=\"_blank\">LINK</a></caption>") as text
set strHTML to (strHTML & "<thead title=\"項目名称\"><tr><th title=\"項目1\" scope=\"row\" >連番</th><th title=\"項目2\" scope=\"col\"> タイトル </th><th title=\"項目3\" scope=\"col\">日時</th><th title=\"項目4\" scope=\"col\">発表</th><th title=\"項目5\" scope=\"col\"> 詳細</th></tr></thead><tbody title=\"検索結果一覧\" >") as text
(ocidHTMLString's appendString:(strHTML))

set strLineNO to 1 as integer
repeat with itemEntry in ocidEntryArray
  set strTitle to (itemEntry's elementsForName:("title"))'s stringValue as text
  ## log (itemEntry's elementsForName:("id"))'s stringValue as text
  set strUpdate to (itemEntry's elementsForName:("updated"))'s stringValue as text
  set strDate to doDateExchange(strUpdate)
  set listEntryArray to (itemEntry's nodesForXPath:("//author") |error|:(reference))
  set strAuth to (((item 1 of listEntryArray)'s firstObject())'s elementsForName:("name"))'s stringValue as text
  set strContents to (itemEntry's elementsForName:("content"))'s stringValue as text
  
  ###HTMLにして
  set strHTML to ("<tr><th title=\"連番\" scope=\"row\">" & strLineNO & "</th><td title=\"タイトル\"><b>" & strTitle & "</b></td><td title=\"日時\">" & strDate & "</td><td title=\"発表\">" & strAuth & "</td><td title=\"詳細\">" & strContents & "</td></tr>") as text
(ocidHTMLString's appendString:(strHTML))
  
  set strLineNO to strLineNO + 1 as integer
end repeat

set strHTML to ("</tbody><tfoot><tr><th colspan=\"5\" title=\"フッター表の終わり\" scope=\"row\"><a href=\"https://xml.kishou.go.jp/index.html\" target=\"_blank\">www.jma.go.jp</a></th></tr></tfoot></table></div>") as text
####テーブルまでを追加
(ocidHTMLString's appendString:(strHTML))
####終了部を追加
(ocidHTMLString's appendString:(strHtmlEndBody))

###ディレクトリ
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
set ocidUUID to refMe's NSUUID's alloc()'s init()
set ocidUUIDString to ocidUUID's UUIDString
set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
###パス

set strFileName to ((strMainTitle) & ".html") as text
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false
###ファイルに書き出し
set listDone to ocidHTMLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)
####テキストエディタで開く
set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias
(*
tell application "TextEdit"
activate
open file aliasFilePath
end tell
*)
tell application "Safari"
activate
open file aliasFilePath
end tell


####################
#日付フォーマット変換
####################
to doDateExchange(argDateText)
  ###【1】入力日付テキスト
  set ocidDateString to refMe's NSString's stringWithString:(argDateText)
  set ocidFormatter to refMe's NSDateFormatter's alloc()'s init()
  ###【2】入力日付フォーマット
ocidFormatter's setDateFormat:"yyyy-MM-dd'T'HH:mm:ss'Z'"
  ###【3】入力テキストを入力フォーマットに沿ってDateにする
  set ocidStringDate to ocidFormatter's dateFromString:(ocidDateString)
  ###【4】出力用の日付フォーマット
ocidFormatter's setDateFormat:("M月d日 H時m分")
  ###【5】3で作った入力日付データを4の出力フォーマットでテキストに
  set ocidOutPutDate to ocidFormatter's stringFromDate:(ocidStringDate)
  ###【6】テキストにして戻す
  set strOutPutDate to ocidOutPutDate as text
return strOutPutDate
end doDateExchange

|

[XML] RSS

Screen_1_20240103151501

データ取得
1:Applescripts
RSSの取得(yahoo news)
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-fc4671.html
[sourceforge.net]sourceforge.netのダウンロード
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-80d963.html

2:BASH
[bash]rss XMLから情報を取得する
https://quicktimer.cocolog-nifty.com/icefloe/2023/09/post-6b0a0a.html
[BASH]RSSの取得と表示(yahoo.news)
https://quicktimer.cocolog-nifty.com/icefloe/2024/01/post-192d01.html


3:アプリケーション
NetNewsWire
https://netnewswire.com/

|

[RSS]MacUPdateのRSSの取得とHTML表示(CSSを修正)


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

#!/usr/bin/env osascript
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
(*
MacUpdateの最新25項目をHTMLで表示
com.cocolog-nifty.quicktimer.icefloe
*)
----+----1----+----2----+-----3----+----4----+----5----+----6----+----7
use AppleScript version "2.8"
use framework "Foundation"
use framework "AppKit"
use scripting additions
property refMe : a reference to current application

### 【1】RSSのURL
set strURL to ("https://www.macupdate.com/rss") as text
set ocidURL to refMe's NSURL's alloc()'s initWithString:(strURL)
### 【2】RSS読み込み
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidURL) options:(ocidOption) |error|:(reference)
set ocidReadXMLDoc to (item 1 of listReadXMLDoc)
###【3】ROOTエレメント
set ocidRootElement to ocidReadXMLDoc's rootElement()
###【4】channelエレメント
########タイトル(取得方法A)
set ocidChannel to ocidRootElement's elementsForName:("channel")
set ocidChannelElement to ocidChannel's firstObject()
set strMainTitle to (ocidChannelElement's elementsForName:("title"))'s stringValue as text
log strMainTitle
##メインのリンク
set strMainLink to (ocidChannelElement's elementsForName:("link"))'s stringValue as text

########発行日(取得方法B)
set listPubDate to ocidRootElement's nodesForXPath:("//channel/pubDate") |error|:(reference)
set strPubDate to (item 1 of listPubDate)'s stringValue as text
##日付を日本語表記に
set ocidPubDate to refMe's NSString's stringWithString:(strPubDate)
set ocidInputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidEnLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("en_US_POSIX")
ocidInputFormatter's setLocale:(ocidEnLocale)
ocidInputFormatter's setDateFormat:("EEE, dd MMM yyyy HH:mm:ss ZZZ")
set ocidEnDate to ocidInputFormatter's dateFromString:(ocidPubDate)
##
set ocidOutputFormatter to refMe's NSDateFormatter's alloc()'s init()
set ocidJpLocale to refMe's NSLocale's alloc()'s initWithLocaleIdentifier:("ja_JP")
ocidOutputFormatter's setLocale:(ocidJpLocale)
ocidOutputFormatter's setDateFormat:("yyyy年MM月dd日EEEE")
set strJpDate to (ocidOutputFormatter's stringFromDate:(ocidEnDate)) as text

###【5】生成するHTMLパーツ
########################################
##HTML 基本構造
###スタイル
set strStylle to "<style>body {margin: 10px;background-color: #FFFFFF;}#bordertable {padding: 10px;width: 100%;margin: 0;border-collapse: collapse;border-spacing: 0;word-wrap: break-word;}table {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 td {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 th {border-spacing: 0;border: solid 1px #666666;padding: .5ch 1ch;border-block-width: 1px 0;border-inline-width: 1px 0;}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}}</style>" as text
###ヘッダー部
set strHead to "<!DOCTYPE html><html lang=\"en\"><head><meta charset=\"utf-8\"><title>" & strMainTitle & "</title>" & strStylle & "</head><body>" as text
###最後
set strHtmlEndBody to "</body></html>"
###HTML書き出し用のテキスト初期化
set ocidHTMLString to refMe's NSMutableString's alloc()'s initWithCapacity:0
####
ocidHTMLString's appendString:(strHead)
###テーブルの開始部
set strHTML to ("<div id=\"bordertable\"><table><caption title=\"タイトル\"><a href=\"" & strMainLink & "\" target=\"_blank\">" & strMainTitle & "</a> : " & strJpDate & " </caption>") as text
ocidHTMLString's appendString:(strHTML)
set strHTML to ("<thead><tr><th title=\"連番\" scope=\"row\"> </th><th title=\"タイトル\">名称</th><th title=\"バージョン\">バージョン</th><th title=\"ディスクリプション\">内容</th><th title=\"リンク\">LINK</th></tr></thead>") as text
ocidHTMLString's appendString:(strHTML)
set strHTML to ("<tbody>") as text
ocidHTMLString's appendString:(strHTML)
###【6】itemエレメント
set ocidItemArray to (ocidChannelElement's elementsForName:("item"))
set strLineNO to 1 as integer
repeat with itemArray in ocidItemArray
  ###タイトル
  set strItemTitle to (itemArray's elementsForName:("title"))'s stringValue as text
  ###タイトルを 名称 バージョン 詳細 に分離
  set ocidItemTitle to (refMe's NSString's stringWithString:(strItemTitle))
  set ocidItemTitleArray to (ocidItemTitle's componentsSeparatedByString:(" - "))
  set strDiscription to (ocidItemTitleArray's lastObject()) as text
  set strAppName to (ocidItemTitleArray's firstObject()) as text
  set ocidAppName to (refMe's NSString's stringWithString:(strAppName))
  set ocidAppNameArray to (ocidAppName's componentsSeparatedByString:(" "))
  set strVersIon to (ocidAppNameArray's lastObject()) as text
  set numCntArrya to count of ocidAppNameArray
  if numCntArrya = 2 then
    set strAppName to (ocidAppNameArray's componentsJoinedByString:(""))
  else
ocidAppNameArray's removeLastObject()
    set strAppName to (ocidAppNameArray's componentsJoinedByString:(""))
  end if
  ###リンクからAppIDを取得
  set strItemLink to (itemArray's elementsForName:("link"))'s stringValue as text
  set ocidItemURL to (refMe's NSURL's alloc()'s initWithString:(strItemLink))
  set ocidAppURL to ocidItemURL's URLByDeletingLastPathComponent()
  set strAppID to (ocidAppURL's lastPathComponent()) as text
  
  set strHTML to ("<tr><th title=\"連番\" scope=\"row\">" & strLineNO & "</th><td title=\"タイトル\">" & strAppName & "</td><td title=\"バージョン\">" & strVersIon & "</td><td title=\"ディスクリプション\">" & strDiscription & "</td><td title=\"リンク\"><a href=\"" & strItemLink & "\" target=\"_blank\">LINK</a></td></tr>") as text
(ocidHTMLString's appendString:(strHTML))
  set strLineNO to strLineNO + 1 as integer
end repeat

set strHTML to ("</tbody><tfoot><tr><th colspan=\"5\" title=\"フッター表の終わり\" scope=\"row\"><a href=\"https://quicktimer.cocolog-nifty.com/\" target=\"_blank\">AppleScriptで生成しました</a></th></tr></tfoot></table></div>") as text
####テーブルまでを追加
(ocidHTMLString's appendString:(strHTML))
####終了部を追加
(ocidHTMLString's appendString:(strHtmlEndBody))

#####【7】保存先ディレクトリとURLを生成しておく
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
set ocidUUID to refMe's NSUUID's alloc()'s init()
set ocidUUIDString to ocidUUID's UUIDString
set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
###パス
set strFileName to ("macupdate.html") as text
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false

#####【8】HTMLをファイルにする
set listDone to ocidHTMLString's writeToURL:(ocidSaveFilePathURL) atomically:(true) encoding:(refMe's NSUTF8StringEncoding) |error|:(reference)

####【9】Finderで開く
set strFilePath to ocidSaveFilePathURL's |path| as text
set strFilePathURL to ocidSaveFilePathURL's absoluteString() as text
set aliasFilePath to (ocidSaveFilePathURL's absoluteURL()) as alias

tell application "Finder"
open location aliasFilePath
end tell



|

[sourceforge.net]sourceforge.netのダウンロード

プロジェクトを指定してダウンロード
CURLを利用します

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

#!/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 "AppKit"
use scripting additions

property refMe : a reference to current application

################################################
####設定項目
################################################
set strProjectName to ("xld") as text
set strTargetTitle to ("dmg") as text

################################################
######
################################################
set strURL to "https://sourceforge.net/projects/" & strProjectName & "/rss" as text

###
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidTempDirURL to appFileManager's temporaryDirectory()
set ocidUUID to refMe's NSUUID's alloc()'s init()
set ocidUUIDString to ocidUUID's UUIDString
set ocidSaveDirPathURL to ocidTempDirURL's URLByAppendingPathComponent:(ocidUUIDString) isDirectory:true
#
set ocidAttrDict to refMe's NSMutableDictionary's alloc()'s initWithCapacity:0
# 777-->511 755-->493 700-->448 766-->502
ocidAttrDict's setValue:(511) forKey:(refMe's NSFilePosixPermissions)
set listBoolMakeDir to appFileManager's createDirectoryAtURL:(ocidSaveDirPathURL) withIntermediateDirectories:true attributes:(ocidAttrDict) |error|:(reference)
###パス
set strFileName to (strProjectName & "_rss.xml") as text
set ocidSaveFilePathURL to ocidSaveDirPathURL's URLByAppendingPathComponent:(strFileName) isDirectory:false
set strSaveFilePath to ocidSaveFilePathURL's |path| as text
################################################
######
################################################
set strCommandText to ("/usr/bin/curl -L \"" & strURL & "\" -o \"" & strSaveFilePath & "\"") as text
do shell script strCommandText
################################################
######
################################################
set ocidOption to (refMe's NSXMLDocumentTidyXML)
set listReadXMLDoc to refMe's NSXMLDocument's alloc()'s initWithContentsOfURL:(ocidSaveFilePathURL) options:(ocidOption) |error|:(reference)
set ocidXMLDoc to (item 1 of listReadXMLDoc)
###RSSデータ
set ocidRootElement to ocidXMLDoc's rootElement()
set ocidChannel to ocidRootElement's childAtIndex:0


################################################
###### バージョン取得
################################################
set listURL to {} as list
repeat with itemRootElement in ocidChannel's children()
  ## set strName to itemRootElement's |name|() as text
  ## itemRootElement = item
  set ocidItemDict to (itemRootElement's childAtIndex:0)'s objectValue()
  set strItemTitle to ocidItemDict as text
  ###ここは対象のRSS毎に違ってくるので書き換えて
  if strItemTitle contains strTargetTitle then
    set ocidoVer to (itemRootElement's childAtIndex:1)'s stringValue()
    set end of listURL to (ocidoVer as text)
  end if
end repeat

################################################
###### 念の為ソートする
################################################
set ocidURLArray to refMe's NSArray's arrayWithArray:(listURL)
set ocidURLArrayM to refMe's NSMutableArray's alloc()'s initWithCapacity:(0)
(ocidURLArrayM's addObjectsFromArray:(ocidURLArray))
###並び替え
set ocidDescriptor to (refMe's NSSortDescriptor's sortDescriptorWithKey:("self") ascending:(false) selector:"localizedStandardCompare:")
ocidURLArrayM's sortUsingDescriptors:{ocidDescriptor}
set strNewerFileURL to (ocidURLArrayM's firstObject()) as text
###取り出したダウンロードURL(最新)
set ocidURLString to refMe's NSString's stringWithString:(strNewerFileURL)
set ocidURL to refMe's NSURL's alloc()'s initWithString:(ocidURLString)
set ocidLastPathURL to ocidURL's URLByDeletingLastPathComponent()
set strFileName to (ocidLastPathURL's lastPathComponent()) as text
set strURL to ("https://sourceforge.net/projects/" & strProjectName & "/files/" & strFileName) as text


################################################
###### ダウンロード
################################################
set appFileManager to refMe's NSFileManager's defaultManager()
set ocidURLsArray to (appFileManager's URLsForDirectory:(refMe's NSDownloadsDirectory) inDomains:(refMe's NSUserDomainMask))
set ocidDownloadsDirPathURL to ocidURLsArray's firstObject()
set ocidSaveFilePathURL to ocidDownloadsDirPathURL's URLByAppendingPathComponent:(strFileName)
set strSaveFilePath to (ocidSaveFilePathURL's |path|()) as text
set strCommandText to ("/usr/bin/curl -L \"" & strURL & "\" -o \"" & strSaveFilePath & "\"") as text
log (do shell script strCommandText) as text
return strURL


|

[BASH]RSSの取得と表示(yahoo.news)


#!/bin/bash

#################################
## RSS項目表示
STR_RSS_URL="https://news.yahoo.co.jp/rss/media/tvkv/all.xml"
XML_RSS_DATA=$(/usr/bin/curl -s "$STR_RSS_URL" | xmllint --format -)
##【title】タイトルを取得してリストに
STR_TITLE_LINE=$(/bin/echo "$XML_RSS_DATA" | xmllint --xpath "//item/title/text()" -)
IFS=$'\n' read -r -d '' -a LIST_TITLE_LINE <<<"$STR_TITLE_LINE"

##【link】リンクURLを取得してリストに
STR_LINK_LINE=$(/bin/echo "$XML_RSS_DATA" | xmllint --xpath "//item/link/text()" -)
IFS=$'\n' read -r -d '' -a LIST_LINK_LINE <<<"$STR_LINK_LINE"

NUM_CNT_ITEM=0
for ITEM_TITLE_LINE in "${LIST_TITLE_LINE[@]}"; do
/bin/echo "$ITEM_TITLE_LINE"
/bin/echo "${LIST_LINK_LINE[$NUM_CNT_ITEM]}"
NUM_CNT_ITEM=$NUM_CNT_ITEM+1
/usr/bin/printf "\n"
done




|

その他のカテゴリー

Accessibility Acrobat Acrobat 2020 Acrobat AddOn Acrobat Annotation Acrobat ARMDC Acrobat AV2 Acrobat BookMark Acrobat Classic Acrobat DC Acrobat Dialog Acrobat Distiller Acrobat Form Acrobat JS Acrobat Manifest Acrobat Menu Acrobat Open Acrobat Plugin Acrobat Preferences Acrobat Preflight Acrobat python Acrobat Reader Acrobat SCA Acrobat SCA Updater Acrobat Sequ Acrobat Sign Acrobat Stamps Acrobat Watermark Acrobat Windows Acrobat Windows Reader Admin Admin Account Admin Apachectl Admin configCode Admin Device Management Admin LaunchServices Admin Locationd Admin loginitem Admin Maintenance Admin Mobileconfig Admin Permission Admin Pkg Admin Power Management Admin Printer Admin SetUp Admin SMB Admin Support Admin System Information Admin Tools Admin Users Admin Volumes Adobe Adobe FDKO Adobe RemoteUpdateManager AppKit Apple AppleScript AppleScript do shell script AppleScript List AppleScript ObjC AppleScript Osax AppleScript PDF AppleScript Pictures AppleScript record AppleScript Script Editor AppleScript Script Menu AppleScript Shortcuts AppleScript Shortcuts Events AppleScript System Events AppleScript System Events Plist AppleScript Video Applications AppStore Archive Attributes Automator BackUp Barcode Barcode QR Barcode QR Decode Bash Basic Basic Path Bluetooth BOX Browser Calendar CD/DVD Choose Chrome CIImage CityCode CloudStorage Color com.apple.LaunchServices.OpenWith Console Contacts CotEditor CURL current application Date&Time delimiters Desktop Device Diff Disk Dock DropBox Droplet eMail Encode % Encode Decode Encode UTF8 Error EXIFData ffmpeg File Finder Firefox Folder FolderAction Fonts GIF github Guide HTML HTML Entity Icon Illustrator Image Events Image2PDF ImageOptim iPhone iWork Javascript Jedit Json Label Leading Zero List locationd LRC lsappinfo LSSharedFileList m3u8 Mail MakePDF Map Math Media Media AVAsset Media AVconvert Media AVFoundation Media AVURLAsset Media Movie Media Music Memo Messages Microsoft Microsoft Edge Microsoft Excel Mouse Music NetWork Notes NSArray NSArray Sort NSBezierPath NSBitmapImageRep NSBundle NSCFBoolean NSCharacterSet NSColor NSColorList NSData NSDecimalNumber NSDictionary NSError NSEvent NSFileAttributes NSFileManager NSFileManager enumeratorAtURL NSFont NSFontManager NSGraphicsContext NSImage NSIndex NSKeyedArchiver NSKeyedUnarchiver NSLocale NSMutableArray NSMutableDictionary NSMutableString NSNotFound NSNumber NSOpenPanel NSPasteboard NSpoint NSPredicate NSPrintOperation NSRange NSRect NSRegularExpression NSRunningApplication NSScreen NSSize NSString NSString stringByApplyingTransform NSStringCompareOptions NSTask NSTimeZone NSURL NSURL File NSURLBookmark NSURLComponents NSURLResourceKey NSURLSession NSUserDefaults NSUUID NSView NSWorkspace Numbers OAuth OneDrive PDF PDFAnnotation PDFAnnotationWidget PDFContext PDFDisplayBox PDFDocumentPermissions PDFImageRep PDFKit PDFnUP PDFOutline perl Photoshop PlistBuddy pluginkit postalcode PostScript prefPane Preview Python QuickLook QuickTime ReadMe Regular Expression Reminders ReName Repeat RTF Safari SaveFile ScreenCapture ScreenSaver SF Symbols character id SF Symbols Entity sips Skype Slack Sound Spotlight sqlite SRT StandardAdditions Swift System Settings TCC TemporaryItems Terminal Text Text CSV Text MD Text TSV TextEdit Tools Translate Trash Twitter Typography UI Unit Conversion UTType valueForKeyPath Video VisionKit Visual Studio Code Wacom webarchive webp Wifi Windows XML XML EPUB XML OPML XML Plist XML RSS XML savedSearch XML SVG XML TTML XML webloc XML XMP YouTube zoom