Advertisement
Guest User

Untitled

a guest
Mar 10th, 2011
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ' Calvin + NASA - VBScript version
  2.  
  3. Dim objFSO, WshShell, picURL
  4.  
  5. ' =================================================
  6. 'set this for yourself widthxheight  - no spaces
  7. desksize="1366x768"
  8. ' This controls whether you use the preview size image, or the large size one
  9. bUseBigImage = True
  10. ' =================================================
  11.  
  12. Set objFSO = CreateObject("Scripting.FileSystemObject")
  13. Set WshShell = WScript.CreateObject("WScript.Shell")
  14.  
  15. picPageURL="http://antwrp.gsfc.nasa.gov/apod/astropix.html"
  16. irfanviewURL="http://irfanview.tuwien.ac.at/iview427.zip"
  17. ImageMagickURL="http://www.imagemagick.org/download/binaries/"
  18. CalvinImageURL="http://i.imgur.com/pcaNT.png"
  19. strScriptDir = Left(WScript.ScriptFullName, InstrRev(WScript.ScriptFullName, "\") - 1)
  20. ' The visibility of the ImageMagick commands. 0 = hide, 2 = minimize
  21. iWinVisibility = 0
  22. strTempPath = WshShell.ExpandEnvironmentStrings("%TEMP%")
  23. Const ForReading = 1, ForWriting = 2, ForAppending = 8
  24.  
  25. ' Look for irfanview, Imagemagick and calvincanvas.png
  26. If Not objFSO.FolderExists(strScriptDir & "\iview") then getIrfanview
  27. If Not objFSO.FolderExists(strScriptDir & "\ImageMagick") then getImageMagick
  28. If Not objFSO.FileExists(strScriptDir & "\calvincanvas.png") then VBSWget CalvinImageURL, strScriptDir & "\calvincanvas.png"
  29.  
  30. ' Find the jpg. First get the HTML page
  31. strImagePage = getHTML(picPageURL)
  32. ' Find the IMG tag
  33. Set regEx = New RegExp   ' Create a regular expression.
  34. If bUseBigImage Then
  35.     regEx.Pattern = "<a href="".*\.jpg"""   ' Set pattern.
  36.     strRegOffset = Len("<a href=""")
  37. Else
  38.     regEx.Pattern = "<IMG SRC="".+"""   ' Set pattern.
  39.     strRegOffset = Len("<IMG SRC=""")
  40. End If
  41. regEx.IgnoreCase = True   ' Set case insensitivity.
  42. regEx.Global = True   ' Set global applicability.
  43. Set Matches = regEx.Execute(strImagePage)   ' Execute search.
  44. For Each Match in Matches   ' Iterate Matches collection.
  45.     RetStr = Match.Value
  46.     wscript.echo RetStr
  47.     If LCase(Right(RetStr, 5)) = ".jpg""" Then
  48.         picURL = Right(RetStr, Len(RetStr) - strRegOffset)
  49.         picURL = Left(picURL, Len(picURL) - 1)
  50.     End If
  51. Next
  52. If picURL = "" Then
  53.     Wscript.echo "Error: can't find picture"
  54.     Wscript.Quit 1
  55. End If
  56. If LCase(Right(picURL, 4)) <> "http" Then
  57.     ' Returned a relative path; use the web page URL to complete
  58.     picURL = Left(picPageURL, InstrRev(picPageURL, "/")) & picURL
  59. End If
  60.  
  61. ' Download the picture
  62. VBSWget picURL, strScriptDir & "\apod.jpg"
  63.  
  64. ' Combine
  65. strConvert = """" & strScriptDir & "\ImageMagick\convert.exe"""
  66. WshShell.Run strConvert & " """ _
  67.     & strScriptDir & "\calvincanvas.png"" -background transparent -resize " & desksize _
  68.     & " -gravity center -extent " & desksize & " """ & strScriptDir & "\calvincanvasRS.png""", iWinVisibility, True
  69. WshShell.Run strConvert & " """ _
  70.     & strScriptDir & "\apod.jpg"" -background transparent -resize " & desksize _
  71.     & " -gravity center -extent " & desksize & " """ & strScriptDir & "\apodRS.jpg""", iWinVisibility, True
  72. WshShell.Run strConvert & " """ _
  73.     & strScriptDir & "\apodRS.jpg"" """ & strScriptDir & "\calvincanvasRS.png"" -mosaic """ & strScriptDir & "\calvin.jpg""", iWinVisibility, True
  74.  
  75. ' set as wallpaper
  76. WshShell.Run """" & strScriptDir & "\iview\i_view32.exe"" """ & strScriptDir & "\calvin.jpg"" /wall=0 /killmesoftly", 0, True
  77.  
  78. Set objFSO = Nothing
  79. Set WshShell = Nothing
  80.  
  81.  
  82. Sub VBSWget(remoteFile, localFile)
  83. ' Download a remote binary file
  84. ' From http://blog.netnerds.net/2007/01/vbscript-download-and-save-a-binary-file/
  85.     Dim objXMLHTTP, objADOStream, objFSO
  86.  
  87.     ' Fetch the file
  88.     Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP")
  89.  
  90.     objXMLHTTP.open "GET", remoteFile, false
  91.     objXMLHTTP.send()
  92.  
  93.     If objXMLHTTP.Status = 200 Then
  94.         Set objADOStream = CreateObject("ADODB.Stream")
  95.         objADOStream.Open
  96.         objADOStream.Type = 1 'adTypeBinary
  97.  
  98.         objADOStream.Write objXMLHTTP.ResponseBody
  99.         objADOStream.Position = 0   'Set the stream position to the start
  100.  
  101.         Set objFSO = Createobject("Scripting.FileSystemObject")
  102.         If objFSO.Fileexists(localFile) Then objFSO.DeleteFile localFile
  103.         Set objFSO = Nothing
  104.  
  105.         objADOStream.SaveToFile localFile
  106.         objADOStream.Close
  107.         Set objADOStream = Nothing
  108.     Else
  109.         errorText = "VBSWget Error:" & vbCrLf _
  110.             & "URL: " & vbCrLf _
  111.             & remoteFile & vbCrLf _
  112.             & "Status: " & objXMLHTTP.Status & " " & objXMLHTTP.StatusText
  113.         WScript.echo errorText
  114.         Wscript.Quit
  115.     End if
  116.  
  117.     Set objXMLHTTP = Nothing
  118. End Sub ' <==VBSWget
  119.  
  120. Sub Unzip(zipFile, destFolder)
  121. ' Unzip a file
  122. ' From http://stackoverflow.com/questions/911053/how-to-unzip-a-file-in-vbscript-using-internal-windows-xp-options-in
  123.     Dim objShell
  124.     If NOT objFSO.FolderExists(destFolder) Then
  125.         'If the extraction location does not exist create it.
  126.        objFSO.CreateFolder(destFolder)
  127.     End If
  128.  
  129.     'Extract the contents of the zip file.
  130.     set objShell = CreateObject("Shell.Application")
  131.     set FilesInZip=objShell.NameSpace(zipFile).items
  132.     objShell.NameSpace(destFolder).CopyHere(FilesInZip)
  133.     Set objShell = Nothing
  134.  
  135. End Sub ' <==Unzip
  136.  
  137. Function getHTML(strURL)
  138. ' Get the HTML from a web page
  139. ' from http://www.suite101.com/content/how-to-use-vbscript-to-download-a-web-page-a89661
  140.     dim xmlhttp : set xmlhttp = createobject("MSXML2.ServerXMLHTTP")
  141.     xmlhttp.open "get", strURL, false
  142.     xmlhttp.send
  143.     getHTML = xmlhttp.responseText
  144. End Function    ' <== getHTML
  145.  
  146. Sub getIrfanview
  147. ' Download and unzip Irfanview
  148.     ProgressStart "Now downloading Irfanview. This should only need to be done once. Please wait."
  149.     VBSWget irfanviewURL, strScriptDir & "\iview.zip"
  150.     ProgressStop
  151.     Unzip strScriptDir & "\iview.zip", strScriptDir & "\iview"
  152.     objFSO.DeleteFile(strScriptDir & "\iview.zip")
  153. end Sub ' <== getIrfanview
  154.  
  155. Sub getImageMagick
  156. ' Download and unzip ImageMagick
  157.     ProgressStart "Now downloading ImageMagick. This should only need to be done once. Please wait."
  158.     strImageMagickData = getHTML(ImageMagickURL)
  159.     strImageMagickCurrent = Left(strImageMagickData, _
  160.         Instr(strImageMagickData, "Q16-windows.zip</a>") + 14)
  161.     strImageMagickCurrent = Right(strImageMagickCurrent, _
  162.         Len(strImageMagickCurrent) - InstrRev(strImageMagickCurrent, ">"))
  163.     VBSWget ImageMagickURL & strImageMagickCurrent, strScriptDir & "\imagemagick.zip"
  164.     ProgressStop
  165.     Unzip strScriptDir & "\imagemagick.zip", strScriptDir
  166.     objFSO.DeleteFile(strScriptDir & "\imagemagick.zip")
  167.     ' Find the Imagemagick folder and rename to 'im' so that the rest of the script can find it
  168.     Set objScriptDir = objFSO.GetFolder(strScriptDir)
  169.     Dim strImageMagickFolder
  170.  
  171.     For Each oFolder In objScriptDir.SubFolders
  172.         If LCase(Left(oFolder.Name, Len("ImageMagick"))) = "imagemagick" Then
  173.             strImageMagickFolder = oFolder.Path
  174.         End If
  175.     Next
  176.     If strImageMagickFolder = "" Then
  177.         Wscript.Echo "ERROR: Couldn't find ImageMagick folder!"
  178.         Wscript.Quit 1
  179.     End If
  180.     objFSO.MoveFolder strImageMagickFolder, strScriptDir & "\ImageMagick"
  181.     Set objScriptDir = Nothing
  182. End Sub ' <== getImageMagick
  183.  
  184. Sub ProgressStart(strMessage)
  185. ' Pop up a status message
  186.     strProgressData = "Set objFSO = CreateObject(""Scripting.FileSystemObject"")" & vbCrLf _
  187.         & "Set WshShell = WScript.CreateObject(""WScript.Shell"")" & vbCrLf _
  188.         & "strMsg = Wscript.arguments(0)" & vbCrLf _
  189.         & "strScriptDir = Left(WScript.ScriptFullName, InstrRev(WScript.ScriptFullName, ""\"") - 1)" & vbCrLf _
  190.         & "strStop = strScriptDir & ""\progress.stop""" & vbCrLf _
  191.         & "If objFSO.FileExists(strStop) Then Wscript.Quit" & vbCrLf _
  192.         & "While Not objFSO.FileExists(strStop)" & vbCrLf _
  193.         & " ret = WshShell.Popup(strMsg, 10, ""ApodCalvin"", 0)" & vbCrLf _
  194.         & "Wend" & vbCrLf _
  195.         & "objFSO.DeleteFile(strStop)"
  196.  
  197.     strProgressVBS = strtempPath & "\progress.vbs"
  198.     Set f = objFSO.OpenTextFile(strProgressVBS, ForWriting, True)
  199.     f.Write strProgressData
  200.     f.Close
  201.     If objFSO.FileExists(strtempPath & "\progress.stop") Then
  202.         objFSO.DeleteFile strtempPath & "\progress.stop"
  203.     End If
  204.     WshShell.Run "wscript.exe """ & strProgressVBS & """ """ & strMessage & """", 1, False
  205.  
  206. End Sub ' <== ProgressStart
  207.  
  208. Sub ProgressStop
  209. ' Close the progress message
  210.     Set f = objFSO.OpenTextFile(strtempPath & "\progress.stop", ForWriting, True)
  211.     f.WriteLine "stop"
  212.     f.Close
  213.     iCount = 0
  214.     While objFSO.FileExists(strtempPath & "\progress.stop") And iCount < 10
  215.         Wscript.Sleep 1000
  216.         iCount = iCount + 1
  217.     Wend
  218.     If objFSO.FileExists(strtempPath & "\progress.vbs") Then
  219.         objFSO.DeleteFile strtempPath & "\progress.vbs"
  220.     End If
  221.     If objFSO.FileExists(strtempPath & "\progress.stop") Then
  222.         objFSO.DeleteFile strtempPath & "\progress.stop"
  223.     End If
  224. End Sub ' <== ProgressStop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement