2013-02-27 85 views
0

我嘗試創建一個從網站下載圖像的Automator工作流程。 圖像的名稱是在一個XML文件,我想提取它們,以創建一個url像http://test.com/images/dynamic_image_name.jpg用Apple腳本解析XML

我發現如何下載圖像時,分析XML文件的方式,以便提取圖像名稱並自動生成良好的URL。

下面是XML文件的一部分:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> 
<plist version="1.0"> 
<dict> 
<key>Managers</key> 
<dict> 
    <key>Sophie Barriac</key> 
    <dict> 
     <key>image</key> 
     <string>sophie.png</string> 
     <key>Téléphone</key> 
     <dict> 
      <key>number</key> 
      <string>0460046150</string> 
      <key>mobile</key> 
      <string>0614589665</string> 
     </dict> 
     <key>Email</key> 
     <dict> 
      <key>email</key> 
      <string>[email protected]</string> 
     </dict> 
    </dict> 
    <key>Kevin Berthier</key> 
    <dict> 
     <key>image</key> 
     <string>kevin.png</string> 
     <key>Téléphone</key> 
     <dict> 
      <key>number</key> 
      <string>0469646007</string> 
     </dict> 
     <key>Email</key> 
     <dict> 
      <key>email</key> 
      <string>[email protected]</string> 
     </dict> 
    </dict> 
</dict> 

我看到了有關AppleScript的一些事情,但我對此一無所知。 我該怎麼做?

回答

3

既然您發佈的XML文件是.plist文件,你可以使用這樣的事情:

tell application "System Events" 
    set plistFile to contents of property list file "PATH TO PLIST FILE" 

    set managersPlist to property list item "Managers" of plistFile 
    set managers to every property list item of managersPlist 

    repeat with manager in managers 
     set imageFile to value of property list item "image" of manager 
     display dialog imageFile 
    end repeat 
end tell 

這將提取所有image鍵的值。