| Dim ws, tempBase, today, bingXml, bingPic |
| Dim bingPicXmlUrl, bingPicUrl, resolution |
| |
| resolution = "1920x1200" |
| tempBase = CreateObject("wscript.Shell").ExpandEnvironmentStrings("%temp%") |
| today=Replace(Date, "/", "-") |
| bingXml = tempBase& "\bingTemp"& today& "_"& resolution& ".xml" |
| bingPic = tempBase&"\bingTemp"& today& "_"& resolution& ".jpg" |
| IF (CreateObject("Scripting.FileSystemObject").FileExists(bingPic)) Then |
| Dim choice |
| choice = MsgBox ("图片已存在, 是否重新下载?",4) |
| If choice = 7 Then |
| call setWallpaper(bingPic) |
| Wscript.quit |
| End If |
| End If |
| |
| |
| |
| bingPicXmlUrl = parseBingPicXmlUrl() |
| call downloadFile(bingPicXmlUrl, bingXml) |
| bingPicUrl = parseBingPicUrl(bingXml) |
| call downloadFile(bingPicUrl, bingPic) |
| If (isPicFile(bingPic)) Then |
| call setWallpaper(bingPic) |
| Else |
| MsgBox "指定分辨率的图片找不到, 请切换分辨率" |
| End IF |
| Wscript.quit |
| |
| |
| Function parseBingPicXmlUrl() |
| Dim dayOffset |
| If WScript.Arguments.Count = 0 Then |
| dayOffset = 0 |
| Else |
| dayOffset = Wscript.Arguments(0) |
| End If |
| parseBingPicXmlUrl = "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=" & dayOffset & "&n=8&pid=hp" |
| |
| |
| End Function |
| |
| Sub downloadFile(url, savePath) |
| Dim obj1,obj2 |
| Set obj1 = CreateObject("msxml2.xmlhttp") |
| Set obj2 = CreateObject("adodb.stream") |
| obj1.open "get",url,False |
| obj1.send |
| obj2.Type = 1 |
| obj2.Mode = 3 |
| obj2.Open() |
| obj2.Write(obj1.responseBody) |
| obj2.SaveToFile savePath,2 |
| obj2.Close |
| Set obj1 = Nothing |
| End Sub |
| |
| Function parseBingPicUrl(bingXml) |
| Dim xmlDoc, bingPicUrl |
| Set xmlDoc = CreateObject("Microsoft.XMLDOM") |
| xmlDoc.async = False |
| xmlDoc.load(bingXml) |
| parseBingPicUrl = "http://cn.bing.com" & xmlDoc.getElementsByTagName("urlBase")(0).childNodes(0).nodeValue & "_"& resolution & ".jpg" |
| End Function |
| |
| Function isPicFile(picPath) |
| Dim obj, char1, char2 |
| Set obj = CreateObject("ADODB.Stream") |
| obj.Type = 1 |
| obj.Mode = 3 |
| obj.Open() |
| obj.LoadFromFile picPath |
| If AscB(obj.Read(1)) = &HFF And AscB(obj.Read(1)) = &HD8 Then |
| isPicFile = True |
| Else |
| isPicFile = False |
| End If |
| End Function |
| |
| Sub setWallpaper(picPath) |
| Dim shApp, picFile, items |
| Set shApp = CreateObject("Shell.Application") |
| Set picFile = CreateObject("Scripting.FileSystemObject").GetFile(bingPic) |
| Set items = shApp.NameSpace(picFile.ParentFolder.Path).ParseName(picFile.Name).Verbs |
| For Each item In items |
| If item.Name = "设置为桌面背景(&B)" Then item.DoIt |
| Next |
| WScript.Sleep 5000 |
| End SubCOPY |