Guest

Calvin + NASA

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