2017-02-06 77 views
1

我想寫一個AppleScript來將我的桌面背景設置爲當天最新的NASA圖像。我正在嘗試使用這個RSS提要:https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss用AppleScript解析RSS(使用grep?)

拼圖的最後一部分是解析rss提要中圖像的URL。我認爲這應該是非常有可能使用grep,但我不知道如何使用grep。我確實查看了Feed,並且我知道我想要的網址將是第一個<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套件:您應該能夠通過參考提取相關的值。 – foo

回答

1

勝利!

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