2017-02-06 9 views
1

Я пытаюсь написать AppleScript, чтобы установить фон рабочего стола на самое последнее изображение НАСА дня. Я пытаюсь сделать это, используя этот RSS-канал: https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rssПроанализируйте RSS с помощью AppleScript (используя grep?)

Последний фрагмент головоломки анализирует URL-адрес изображения из RSS-ленты. Я предполагаю, что это должно быть очень возможно с помощью grep, но я не знаю, как использовать grep. Я просмотрел канал, и я знаю, что URL, который я хочу, будет первым <link> в первом <item>.

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

Grepping markup является противным и неправильным. См. XML-пакет System Events: вы должны иметь возможность извлекать соответствующее значение с помощью ссылки. – 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