Home page rotator |
Written by Ian Elliot | |||||
Tuesday, 09 February 2010 | |||||
Page 4 of 4
The Context menu handlerThe 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: <!-- 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( _ 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( _ 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 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( _ 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 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 rotatorThe 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(_ 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 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( _ Next get the HomePages folder in the user's Favorites as before: UserFavs=WshShell.SpecialFolders( _ 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 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) 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) Trying it outNow 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> |
|||||
Last Updated ( Monday, 08 February 2010 ) |