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

 

The Context menu handler

The HR.htm script is run when the user selects our new custom item in the context menu.

The main problem with understanding what you can do with the script is working out how it can interact with the original web page that the context click event came from.

This problem is very easily solved once you know that the script is running in a hidden web page and that web page has a full DOM – Document Object Model. This may not seem like much but the window.external object has a property menuArguments which returns the window object of the web page that triggered the event and from there you can access all of the HTML etc in the original page.

In this case all we need to discover is the URL of the original page, which makes everything very easy.

Create a new file called HR.htm in the same directory as the setup script. To retrieve the URL of the page that caused the script to run all we need is:

<!--
Script for adding current
page to home pages directory
-->
<script language="VBScript">
'Get URL of calling page
set Win = window.external.menuArguments
set Doc = Win.document
homeurl=doc.url

Notice that window is the window of the script, which is of course hidden but Win is the window of the original web page.

Now we have the URL of the page we need to add it to the user's Favorites. However first we need to check that a special favourites directory called "HomePages" exists and if it doesn't we create it. To do this, and other subsequent tasks, we need the same two additional objects that were so useful in setup.vbs:

set fso=CreateObject( _
"Scripting.FileSystemObject")
Set WshShell = CreateObject( _
"WScript.Shell")

The SpecialFolders method of the WshShell object can be used to find the path to any of the user's special directories such as My Documents and Favorites:

UserFavs=WshShell.SpecialFolders( _
"Favorites")
UserFavs=UserFavs & "\HomePages"

With the directory path set up for HomePages in the user's Favorites we can check for, and if necessary create, the directory:

if not(fso.folderexists(UserFavs)) then
fso.createfolder(UserFavs)
end if

To save the URL in the HomePages folder we need to create URL shortcuts. Each one added to the folder is given a name in the sequence Home0, Home1, Home2 and so on. This is achieved by finding out how many shortcut files are already in the directory:

N=fso.GetFolder(UserFavs).Files.Count

Creating a URL shortcut is a two-step process. First we create the URL shortcut object:

Set URLlink=WshShell.CreateShortcut( _
UserFavs & "\home" & N & ".url")

Notice that this is where the path and file name of the object is specified and the ".url" makes it a URL shortcut rather than a program shortcut. Finally we store the url in the shortcut and save it to disk:

 N=fso.GetFolder(UserFavs).Files.Count
Set URLlink=WshShell.CreateShortcut( _
UserFavs & "\home" & N & ".url")
URLlink.TargetPath=homeurl
URLlink.Save
</script>

This is the complete right-click menu handler and it is very simple for what it does. Notice that in this case all we have used is the document.url property of the original web page, but you can access its entire DOM.

The rotator

The final part of the jigsaw is the rotate.vbs script that is triggered by the Scheduler, examines the user's Favorites and selects the next home page.

Create a new file called rotate.vbs in the same directory as setup.vbs. This starts by creating the FSO and WshShell objects:

set fso=CreateObject(_
"Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject( _
"WScript.Shell")

Before we can rotate the home page we need to discover what it is and this is stored in the registry under the key:

HKEY_CURRENT_USER
Software
Microsoft
Internet Explorer
Main
Start Page

To discover the current home page you simply read the value associated with the key and to set another home page you write the URL to the key's value.

First retrieve the home page:

HomePage=wshShell.regread( _
"HKCU\Software\Microsoft\
Internet Explorer\Main\Start Page")

Next get the HomePages folder in the user's Favorites as before:

UserFavs=WshShell.SpecialFolders( _
"Favorites")
UserFavs=UserFavs & "\HomePages"

If this directory doesn't exist then the user hasn't setup any rotating home pages and there is nothing to do but if it does exist we open and process the files in it:

if fso.folderexists(UserFavs) then
set Fs=fso.GetFolder(UserFavs).Files

Unfortunately even if it does exist there might not be any files in it as yet so we need to add:

if Fs.count>0 then

so that the rest of the script is only obeyed if there are actually some file to process.

The simplest way to process the home pages is to read the URLs in each shortcut into an array. Unfortunately there is no way of opening an existing shortcut provided within script. However it is possible to discover the format of a URL shortcut file by looking inside and the good news is that the URL is stored in plain text as the line following [InternetShortcut] . So we can now read the URLs treating each shortcut as a text file:

reDim URLS(Fs.count)
i=0
for each f in Fs
set ts=f.OpenAsTextStream(1)
do
temp=ts.readline
loop until
InStr(temp,"[InternetShortcut]")<>0
temp=ts.Readline
temp=Split(temp,"=")
URLS(i)=temp(1)
ts.close
if URLS(i)=HomePage then
found=(i+1) Mod FS.count
end if
i=i+1
next

Each file is opened in turn and lines are read until [InternetShorcut] is found and discarded. The next line has the format "URL=" followed by the actual URL and so we split temp on the "=" sign and take the second item, i.e. the URL, and store this in URLS(i).

After closing the file we next check to see if the current home page is one of the files in the list. If it is then the next home page to be set up is the next home page in the list, i.e. found=(i+1). The only complication is what happens if this is the last item in the list – then i+1 is beyond the end of the list. Mod operator is used so that if i is greater than the length of the list i "wraps round" to 0. If the current home page isn't in the list then i is 0 by default. No matter what happens at the end of this process we have a value for i that indicates the location of the next home page in the list. All that remains is to set the registry key to this URL:

  NextURL=URLS(found)
wshShell.regWrite "HKCU\Software\
Microsoft\Internet Explorer\
Main\Start Page" ,NextURL, "REG_SZ"
end if
end if

Trying it out

Now we have all the scripts necessary to the system. If you run the setup script the files will be copied to the correct locations and the registry updated. If this doesn't work the most likely cause is that the script isn't being run with administrator privileges. Open a command prompt using "Run as administrator" and then run the script and it should all work.

After this you will see a new right-click context menu item. If you use this you will add a new directory to the Favorites and the Scheduler will start to cycle through these at the interval you have set. If you can't wait go to the Scheduled tasks directory and right click on the Rotate home page task and select Run.

There is still a lot to do. An uninstall script is a good idea and fairly easy to write and there is one included in the download to be found in the CodeBin. 

To access the code for this project, once you have registered, click on CodeBin.

<ASIN:0321501713>

<ASIN:1931841705>

<ASIN:1571690468>

<ASIN:0735619816>

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!

 



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.