2017-02-06 3 views
1

Apple 데스크톱을 사용하여 바탕 화면 배경을 최신 NASA 이미지로 설정하려고합니다. 이 RSS 피드를 사용하여이 작업을 수행하려고합니다. https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rssRSS를 AppleScript로 구문 분석 (grep? 사용)

마지막 퍼즐 조각이 rss 피드의 이미지 URL을 구문 분석합니다. 이 grep을 사용하여 매우 가능해야한다고 가정하지만 grep 사용법을 모르겠습니다. 내가 피드를 살펴 봤는데, 내가 원하는 URL이 먼저 <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

마크 업 그레픽은 엉망이며 잘못되었습니다. System Events의 XML Suite 참조 : 관련 값을 참조로 추출 할 수 있어야합니다. – 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