Archive for July, 2009

h1

iPhone and UK Train Times

20/07/2009

National Rail Enquiries in the UK have cornered the market for iPhone apps for UK rail.  It used to be open and there apparently used to be a pretty reasonable app out there for free.  National Rail’s app on the other hand is not as nice and costs a few too many pennies for many people.

I used to use a WAP version on my old phone and I still use it through a WML browser add-on for Firefox when I need super speedy quick access or am on a really slow connection – such as that on the East Coast mainline trains.  However the iPhone’s Safari browser doesn’t support WML.  However there is another alternative that has a lightweight portal for the small screen – National Rail’s PDA page.

I’m not currently an iPhone user myself but I’ve suggested this to a few people and they seem to find it a viable and practical alternative to the National Rail iPhone app.

h1

Presentations – Keeping your screen active

20/07/2009

Power saving options on computers are something that I personally think are great.  They not only help reduce carbon footprints, but they also help draw out even more life from your laptop battery.   One of the main ways in which this is done (others being things such as disabling hardware) is by having an inactivity option that turns off the screen and the hard disk after a period of inactivity.  Unfortunately there are times when this isn’t something you want.  The primary example of this is when you are giving a presentation.

The typical way of overriding this in Windows is to change what power saving scheme is being used through the Windows control panel or a third party application that links directly to these options.  However sometimes end users don’t have access to this.

This typically occurs where a group policy has been applied to stop users ‘tinkering’ with the power saving schemes.  Users with access can amend or delete power saving schemes which can lead to complications with machines and so it is not that uncommon to restrict access.

Whilst applications such as Microsoft PowerPoint can lead the OS into knowing that it should keep things like monitors on if a presentation is in a browser or some other “power unaware” application, then power saving actions can kick in.

I decided to apply a brute force solution to this by creating an AutoHotKey script to send a key press to the OS at regular intervals.  By default it sends a Shift key press as this is least likely to have any impact on any application, but this can be modified along with the frequency with which it is sent by the use of a settings file.  The settings file also includes an option to choose whether to start sending key presses as soon as it is run.

The code for the script is given at the end of this post, but I have also compiled this into a stand alone executable that you could even pop onto your flash drive if you’re going to be presenting on someone else’s PC.  Similarly you could add it into your start-up group (with it set not to auto start sending key presses) so it is always there when you need it.

The script places a monitor icon in the system tray.  When it is black power saving actions through inactivity will take place.  When it is blue, key presses will be sent.  Right clicking on the icon will display a menu with an override option.  Selecting this option will place a check mark next to it which will set the icon blue and initiate the override mode.  Selecting the option again will uncheck it, set the icon black and turn off the override mode.

Only one setting is available on the settings menu.  This is another check item and determines whether override mode should be enabled immediately when the utility is first started.

The utility looks for a settings file called ActiveDisplay.ini in the folder from which the utility is being run.  If it does not exist, the utility will use its default settings which match the settings given in this example file:

[Settings]
;Set to 1 to enable override mode at start-up and 0 to disable.
EnableOnRun=0
;Specify what characters should be sent.  Use {} for special key strokes
KeyStroke={SHIFT}
;Number of milliseconds between sending the key strokes (120,000 milliseconds : 2 minutes)
Period=120000

If you want to tweak the script to meet your own needs you can get the details below and use your own icons.  If you want to get the compiled version you can download it for free.

#Persistent
#SingleInstance

;Read in settings
iniread, EnableAtStartup, %A_ScriptDir%\ActiveDisplay.ini, Settings, EnableOnRun, 0
iniread, KeyStroke, %A_ScriptDir%\ActiveDisplay.ini, Settings, KeyStroke, {Shift}
iniread, Period, %A_ScriptDir%\ActiveDisplay.ini, Settings, Period, 120000

;Create the tray menu
menu, tray, add, Override, Override
Menu, SettingsMenu, add, EnabledAtStartup
Menu, tray, add, Settings, :SettingsMenu
menu, tray, add, About
menu, tray, add, Exit
menu, tray, nostandard

;Initialise
;Time to do something that seems crazy - we'll flip these and then call the menu selection routines where it will get flipped back
EnableTimer := EnableAtStartup
EnableAtStartup := Not(EnableAtStartup)
GoSub, EnabledAtStartup
EnableTimer := Not(EnableTimer)
GoSub, Override
return

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

SendKeyStroke:
Send %KeyStroke%
return

Override:
EnableTimer := Not(EnableTimer)
If EnableTimer
{
 menu, tray, Check, Override
 menu, tray, icon, C:\Icons\Monitor On.ico
 menu, tray, tip, Default Power Saving Mode Overridden
 SetTimer, SendKeyStroke, %Period%
}
Else
{
 menu, tray, UnCheck, Override
 menu, tray, icon, C:\Icons\Monitor Off.ico
 menu, tray, tip, Default Power Saving Mode
 SetTimer, SendKeyStroke, Off
}
return

EnabledAtStartup:
EnableAtStartup := Not(EnableAtStartup)
If EnableAtStartup
{
 menu, SettingsMenu, Check, EnabledAtStartup
 iniwrite, 1, %A_ScriptDir%\ActiveDisplay.ini, Settings, EnableOnRun
}
Else
{
 menu, SettingsMenu, UnCheck, EnabledAtStartup
 iniwrite, 0, %A_ScriptDir%\ActiveDisplay.ini, Settings, EnableOnRun
}
return

About:
MsgBox, 0, About Active Display, Version 1.0`nCopyright 2009 RebootIT`n`nhttps://flagit.wordpress.com
return

Exit:
ExitApp
h1

A Flexible Progress Window in VBScript

19/07/2009

This post has now been migrated to ThoughtAsylum.com.

Follow this link to go directly to the article.

h1

Shortcuts with VB Script

18/07/2009

Sometimes I need to create shortcuts with VB Script especially if the script is being installed and a shortcut needs to be created to the script.  The code to create a shortcut is quite straight forward,  but the CreateShortcut() function presented here pulls everything together into a single reusable form.  Optional parameters that you don’t want to use can be filled by the use of “NULL” and environment variables can be included in file paths to make this as flexible as possible.

Option Explicit

CreateShortcut "%UserProfile%\SendTo\Test Link.lnk", _
 "C:\Scripts\Test-Script.vbs", NULL, "A test script", NULL, NULL, NULL, NULL, NULL

Function CreateShortcut(p_strShortcutPath, p_strTargetPath, _
 p_strArguments, p_strComment, _
 p_strHotKey, p_strIconPath, p_strIconPosition, _
 p_strWindowStyle, p_strWorkingDirectory)

 'Example Parameters
 'p_strShortcutPath = "%UserProfile%\SendTo\Shortcut to Something"
 'p_strTargetPath = "%PROGRAMFILES%\Somewhere\Something.exe"
 'p_strArguments = "/O"
 'p_strDescription = "Start Something"
 'p_strHotKey = "CTRL+ALT+Q"
 'p_strIconPath = "%PROGRAMFILES%\Somewhere\Something.exe"
 'p_strIconPosition = "3"
 'p_strWindowStyle = "1"
 'p_strWorkingDirectory = "C:\Working\"
 'Use NULL for parameters which are not to be set

 Dim objShell, objShortcut

 Set objShell = WScript.CreateObject("WScript.Shell")
 Set objShortcut = objShell.CreateShortcut(objShell.ExpandEnvironmentStrings(p_strShortcutPath))

 If Not isNull(p_strTargetPath) Then
 objShortcut.TargetPath = objShell.ExpandEnvironmentStrings(p_strTargetPath)
 End If

 If Not isNull(p_strArguments) Then
 objShortcut.Arguments = p_strArguments
 End If

 If Not isNull(p_strComment) Then
 objShortcut.Description = p_strComment
 End If

 If Not isNull(p_strHotKey) Then
 objShortcut.HotKey = p_strHotKey
 End If

 If Not isNull(p_strIconPath) Then
 objShortcut.IconLocation = objShell.ExpandEnvironmentStrings(p_strIconPath & ", " & p_strIconPosition)
 End If

 If Not isNull(p_strWindowStyle) Then
 objShortcut.WindowStyle = p_strWindowStyle
 End If

 If Not isNull(p_strWorkingDirectory) Then
 objShortcut.WorkingDirectory = objShell.ExpandEnvironmentStrings(p_strWorkingDirectory)
 End If

 objShortcut.Save
End Function
h1

Self Installing VB Scripts

18/07/2009

I like using VB Scripts and the Windows Script Host to resolve issues as it has proved quick and flexible for most tasks.  One thing I have found problematic on occasion is the installation of the scripts on a user’s PC.  It takes time to do if it is not being deployed using logon scripts, group policy, etc.  To this end I decided to write a function to allow a script to install itself.   The script below is primarily made up from three functions/routines.

Starting from the bottom, the BuildFolderPath() function just takes a file path and adds a backslash to the end if it needs one.

The next function is a little more interesting.  ScriptPath() returns the folder in which the script is being run from.  Note that if it is being run from a shortcut it is the target folder.  This script works by removing the name of the script from the full file path of the script file.

The last chunk of code is the sub routine InstallScript() and does the majority of the work so to speak.  This checks to see if the script is in the correct location.  If it is not it asks the user for permission to copy it from the current location to the one against which it checked.  This does of course rely on the user having appropriate file permissions to copy the script there in the first place, but if we need anything more then some sort of executable or direct access by an administrator is always going to be best otherwise you’ll be handing out administrator passwords verbally or in plain text in script files.  Once installed (or if the user chose not to install) there is a bit of feedback to the user and the script ends.  If the script is running from the right location, no further action is taken and the script continues.

Option Explicit

Const INSTALL_TO = "%UserProfile%\SendTo"

InstallScript INSTALL_TO

Msgbox "The script is being run from the right place..."

'This routine checks if the script is in the right folder location and if it
'isn't it will give the user the option of copying the script to the right location
Sub InstallScript(p_strInstallTo)
 Dim strInstallToFolder
 Dim objShell, objFSO

 'Set the installation folder
 Set objShell = CreateObject( "WScript.Shell" )
 strInstallToFolder = BuildFolderPath(objShell.ExpandEnvironmentStrings(p_strInstallTo))

 If Not strInstallToFolder = ScriptPath Then
 'Copy the script to the correct folder if the user agrees
 If Msgbox ("This script is not being run from the expected location." & vbCrLf & _
 "Would you like to install it to " & strInstallToFolder & " now?", _
 vbYesNo + vbQuestion, "Install Script") = vbYes Then

 Set objFSO = CreateObject("Scripting.FileSystemObject")
 objFSO.CopyFile WScript.ScriptFullName, strInstallToFolder & WScript.ScriptName

 MsgBox "The script has been installed/updated.", vbOkOnly & vbInformation, "Install Complete"
 Else
 MsgBox "The script has not been installed and will not run.", vbOkOnly + vbExclamation, "Install Cancelled"
 End If

 'Finish the script at this point
 WScript.Quit
 End If

End Sub

'This function returns the path which the script is running from
Function ScriptPath()
 ScriptPath = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
End Function

'This function just adds a trailing backslash if there isn't one
Function BuildFolderPath(p_strPath)
 If Right(p_strPath,1) = "\" Then
 BuildFolderPath = p_strPath
 Else
 BuildFolderPath = p_strPath & "\"
 End If
End Function

An interesting thing to note is that I’ve passed folder paths through the ExpandEnvironmentStrings() function.  This converts any environment variables in the string and allows them to be converted to their full path.  If you want to know more about environment variables I’d recommend a few minutes browsing this page on environment variables in Windows XP.

So all you have to do is decide on a folder where you want the script run from and then pop these functions into your script.  Personally I like to create shortcuts for scripts too (e.g. in the user’s send to folder, start menu or the desktop).  I’ll be posting a little later on how to add that to your toolbox – particularly useful for installations like this.

h1

A portable web server where I want it

13/07/2009

I’ve been tinkering recently with some things around portable applications which I will probably post more about in the hopefully not too distant future.  One of the things I’ve been trawling around for and busily configuring are various free web server packages that are “portable”.  Unfortuately it has taken a fair bit of effort to actually track down and configure something that is portable enough.   The problem for me is that I have a particular need to locate the web server within a particular sub folder and in the case of most software packages this is not an option.

Most implementations are based around Apache (e.g. micro apache, XAMPP, TWAMP, WAMP, USB Web Server) but there are a few other options out there I also looked at  such as RITlabs TinyWeb.  Some of these experience problems when they are not installed to the root of a drive and others have a problem when the installation path contains spaces.  My desired installation path unfortunately is not in the root folder and does contain spaces which ruled out most of the options.

I found two options that did seem to cope with the path.  These were WAMP and USB Web Server.  My preference is for USB Web Server and I’ll explain why…

When installed my WAMP system tray application did not match up to the one shown on the web site.  Even though it was running, the status told me that it wasn’t and there was no option to stop/start individual applications such as Apache and MySQL.  Moreover WAMP comes as an installation package which adds an entry into the registry – at least for the add/remove software entry … which seems blatantly false for something that is supposed to be portable.

USB Web Server on the other hand does work as expected and does allow me to stop and start specific web related services and even to move it to a different folder (you have to close it in between).  The one thing I would like is to be able to specify which web services it should run at start-up.  It runs Apache, MySQL and SMTP by default, but I’d really just like to run Apache and free up my RAM without specifically having to manually turn them off.  I tried various renames of folders and changing of settings, but everything I tried either had no effect or stopped Apache running as well.  I’m guessing that there’s some sort of start-up chain at work rather than independent requests.  But maybe someone else knows what to change to address this or perhaps the developer will provide this option in a future release.

So if you want a truly portable web server try USB Web Server.