Home page rotator
Written by Ian Elliot   
Tuesday, 09 February 2010
Article Index
Home page rotator
Setting up the system
Custom menu for IE and using the scheduler
Running the script

 

Adding a custom menu item to IE

The second section of the setup program adds the custom menu item to IE – and it is very short. It comes as something of a surprise to discover that all you have to do is modify the registry.

Of course the usual warnings apply to making sure you backup the registry while you are developing any programs that use it. The key that controls the extra menu items is:

HKEY_CURRENT_USER
Software
Microsoft
Internet Explorer
MenuExt

 All you have to do is to create a subkey with the name of the menu item you want to appear and set its value to the HTML file that contains the script that will be run when the user selects the item.

There are two optional subkeys to your menu key that you can also create to control exactly how it works. The Contexts subkey can be used to control when the menu item appears, i.e. only when the user right-clicks a picture say. The Flags subkey can be used to give the script that is run a dialog box window.

By default the script runs without any visible UI and it appears wherever the user right-clicks and as this is exactly what we need we can ignore the additional subkeys in this project.

Creating a key in the registry is very easy using the RegWrite method of the WshShell object:

 

ProgURL="file://" & ProgFolder &
 "\HR.htm"
MenuItem="Add page to home page rotation"
WshShell.RegWrite _
"HKCU\Software\Microsoft\Internet
Explorer\MenuExt\" _
& Menuitem & "\",_
ProgURL, "REG_SZ"

This adds the menu item

“Add page to home page rotation”

as the key and sets its value to be the URL to the page that contains the script to be run. After this line does its work IE has a new context menu item – what could be easier.

We still need to create HR.htm however!

Scheduler

sched

The Home page rotator can be edited or run using the Schedule Directory in the usual way

 

The third and final part of the setup is to enter an event into the Windows scheduler.

This isn't difficult in theory but in practice it proved to be by far the hardest thing to get exactly right. There is a much easier to use interface under Vista or later but this isn't available under Windows XP.

Under XP we can either use WMI (Windows Management Interface) or the command line utility SchTasks.

The WMI route is difficult and produces tasks that the user can't edit using the Wizard. Using a command line utility is never very satisfactory but in this case it is probably the best choice. Look up the details of SchTasks in Windows help to get a rough idea of how everything works.

We need the user's user name and password to be able to schedule a task. The user name can be obtained automatically but we have no choice but to prompt for the password:

Set Net = Create Object("WScript.Network")
User = Net.UserName
pass=InputBox( "Enter your password", _
"Password")

Before we create the new task we need to make sure it isn't already in the list by attempting to delete it. The task is called "Home Page Rotate" and this is enough to identify and delete it:

cmdline ="schtasks /delete
/tn ""Home Page Rotate"" /f"
set sched=wshShell.exec(cmdline)
do while sched.status=0
wscript.sleep 100
loop

Notice the use of the /f switch to suppress output from the command and the use of the WshShell's Exec method to actually run SchTasks. The status method returns 0 while the utility is running.

Now we can safely create the task knowing that it can't possibly exist:

cmdline="schtasks /create
/tn ""Home Page Rotate"""
cmdline=cmdline & " /tr rotate.vbs "
cmdline=cmdline & _
" /sc minute /mo 20 /ru " & _
User & " /rp " & pass
set sched=wshShell.exec(cmdline)

This is similar to the delete operation but now we have to specify the program to run when the task is triggered, i.e. rotate.vbs, and the schedule, every 20 minutes in this case. You can set any time period you want and a refinement that you can add is to allow the user to specify or change the interval after setup. The double and triple quotes in the first line aren't a mistake – to quote a double quote you have to use two double quotes.

Another complication is that the utility sends output to the two standard text streams. So after waiting a few moments we need to read the StdOut and StdErr streams to see if there is anything to report to the user:

wscript.sleep 100
input1 = sched.Stderr.ReadAll
input2 = sched.StdOut.ReadAll

As long as everything has worked, Input1 should be empty and Input2 should contain an "everything is ok" type message. We can show the user both or just ignore them:

 if input2<>"" then
msgbox input2
end if
if input1<>"" Then
msgbox input1
end if
end sub

That's all there is to the setup script but you can go on adding to it. Now we have to turn to the two other scripts involved in the working of the system.

One small point is that for the setup script to work it has to be run with administrator privileges. Under Vista and Window 7 this means that you have to run it from a command prompt that was started with the " run as Administrator".  There is a way to run a script so that automatically asks for elevated privileges but that's another story.

<ASIN:0596001266>

<ASIN:0470386800>

<ASIN:1565922476>

<ASIN:1592000568>



Last Updated ( Monday, 08 February 2010 )
 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.