2017-02-06 4 views
1

私はデスクトップの背景をその日の最新のNASAイメージに設定するためにAppleScriptを書こうとしています。私はこのRSSフィードを使用してこれをしようとしています:https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rssAppleScriptでRSSを解析する(grep?を使用)

パズルの最後の部分は、RSSフィードからイメージのURLを解析しています。私はこれがgrepを使って非常に可能であるべきだと考えていますが、grepの使い方はわかりません。私はフィードを見て、私は最初の<item>の最初の<link>になりたいと思っています。

set {year:y, month:m, day:d} to (current date) 

set todaydate to d & "\\ " & m & "\\ " & y 
log todaydate 

set myFile to "/Users/me/Pictures/NASA\\ Image\\ of\\ the\\ Day/" & todaydate & ".jpg" 

property RSSURL : "https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss" 

--get the first download link from the rss feed 
--ie stuff inside <link> </link> directly after first <item> 

set pictureURL to "" --but to the right thing, of course 

do shell script "curl -o " & myFile & " " & pictureURL 

tell application "Finder" to set desktop picture to POSIX file "myFile" 
+0

グレープマークアップは厄介で間違っています。システムイベントのXML Suiteを参照してください。参照で関連する値を抽出できるはずです。 – foo

答えて

1

Victory !!

set {year:y, month:m, day:d} to (current date) 

set todaydate to d & "_" & m & "_" & y 
log todaydate 

set dir to "~/Pictures/NASA_image_of_the_day/" 

set myFile to dir & todaydate & ".jpg" 

property RSSURL : "https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss" 

set XMLfile to dir & "feed.xml" 
do shell script "curl -o " & XMLfile & " " & RSSURL 

tell application "System Events" 
    tell XML file XMLfile 
     tell XML element "rss" 
      tell XML element "channel" 
       tell XML element "item" 
        tell XML element "enclosure" 
         set pictureURL to value of XML attribute "url" 
        end tell 
       end tell 
      end tell 
     end tell 
    end tell 
end tell 

--display dialog pictureURL 
--display dialog myFile 

do shell script "curl -o " & myFile & " " & pictureURL & " -L" 

tell application "System Events" 
    tell every desktop 
     set picture to myFile 
    end tell 
end tell 
関連する問題