Automatically Run and Minimize iTunes

So, more people now are having servers at home, which they’re using to run iTunes and stream to various speakers plugged into Airport Express routers. Myself included. I noted that iTunes does not abide by the “run minimized” functionality in Windows, and some googling produced a VBScript that would run and minimize iTunes. Unfortunately, the script was a little buggy and unreliable, so I rewrote here. Below are the results of a kinda of quick hack job on it.

Option Explicit

'// Change this value if you want the a longer timeout (if you notice iTunes not minimizing)
Dim maxtime
maxtime = 60

Dim starttime
starttime = 0

Dim WshShell
Set WshShell = CreateObject("WScript.Shell")

Dim strComputer
strComputer = "."

Dim objWMIService
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Dim colProcessList
Set colProcessList = objWMIService.ExecQuery _
	("Select * from Win32_Process Where Name = 'iTunes.exe'")

If colProcessList.Count < 1 Then
	WshShell.Run """C:\Program Files\iTunes\iTunes.exe""", 2
End If

starttime = Timer

Do
	If ( Timer - starttime ) > maxtime Then
		wscript.quit
	End If

	Set colProcessList = objWMIService.ExecQuery _
		("Select * from Win32_Process Where Name = 'iTunes.exe'")

	wscript.sleep 500
Loop While colProcessList.Count < 1

Dim objProcess
For Each objProcess in colProcessList
	Dim foobar
	foobar = split( objProcess.Name, "." )

	starttime = Timer

	Dim activated
	Do
		If ( Timer - starttime ) > maxtime Then
			wscript.quit
		End If

		activated = WshShell.AppActivate( foobar(0) )

		wscript.sleep 500
	Loop While activated = false

	if activated = true then
		WshShell.SendKeys("%( )") '//equivalent of ALT+space
		WshShell.SendKeys("N") '//equivalent of 'n' key
	end if
Next

Comments

Leave a Reply

You must be logged in to post a comment.