Development

This category is for all things relating to scripting or programming languages, as well as software construction, design and development.

Bluetooth Improvements in Windows Phone Mango

It was a pleasant surprise to see that Windows Phone Mango includes some improvements to the Bluetooth stack, which means I now see the name of the currently playing track in my car.

This gets added to the already existing ability to pause, fast-forward and rewind directly from the steering wheel, so now I can do just about everything from the Zune software on my phone while driving.

Very handy – as the built in USB player in my car doesn’t support content downloaded from Zune Pass…

WMPowerUser.com has a more information on what has been the Windows Phone Bluetooth stack, if you fancy being nerdy about it.

Querying TFS with TFPT.EXE and PowerShell

At Branded3 we use Team Foundation Server for source control, task managment, and various other tracking purposes. One of the benefits of this is being able to run queries with WIQL to pull off reports.

As is usually the case with me, I have set up a couple of PowerShell scripts that use TFPT.EXE from the Team Foundation Server Power Tools to make life a little simpler for myself…

Viewing Open WorkItems

$TFSSERVER   = "hq-tfs08-01.branded3.net"

Function Get-WorkItem
{
    $query = "SELECT [System.Id], [System.Title] FROM WorkItems " +
             "WHERE [System.AssignedTo] = 'Julian Kay' " +
             "AND [System.State] <> 'Closed' " +
             "AND [System.State] <> 'Resolved' " +
             "ORDER BY [System.Id]"

    tfpt query /collection:$TFSSERVER /wiql:$query /include:data
}

This little script gets a list of WorkItems which are not closed or resolved from TFS. I find this much faster than opening a copy of Visual Studio to find out which tasks I have assigned to me. True you could run this kind of script by using a batch file, but I like the fact I can use this in conjunction with the various Outlook scripts I use for PowerShell with simple one-liners like foreach ($workItem in Get-WorkItem) { Add-OutlookTask “$workItem” }

Getting Work Hours

Function Get-WorkItemHours
{
    $month = (Get-Date).ToString("MMMM")
    $year  = (Get-Date).Year
    $query = "SELECT [Completed Work] FROM WorkItems " +
             "WHERE [System.AssignedTo] = 'Julian Kay' " +
             "AND [Assigned Month] = '$month' " +
             "AND [Assigned Year] = '$year'"

    $hours = tfpt query /collection:$TFSSERVER /wiql:$query /include:data
    $total = 0.0
    foreach ($hour in $hours)
    {
       $total += $hour
    }
    $total
}

We also have custom fields which help us keep track of the hours we’ve spent on a project, and as shown above, we can even see how many hours have been spent in a month. By running this simple script I can be sure that all my time is correctly recorded to enable us to both bill correctly, and keep track of how long things really take.

Naturally, everyone has their own requirements for scripts like this, but PowerShell gives me the flexibility to create these simple utilities with very little development work.

Fix for Italic Fonts in Windows Command Prompt

I kept getting a problem where the Consolas font was in italics on when using either CMD or PowerShell on Windows 7.

After messing around with lots of options, I actually ran a repair on the Office 2010 installation I had. It worked. I’m not exactly sure why this is the case, but if you’ve got this problem – you might want to try this too.

I know Consolas ships with Office 2010, so maybe the repair just makes sure the font files are installed correctly. If anyone knows the real reason, please feel free to leave me a comment!

Outlook Tasks in PowerShell and Vim

Getting Outlook Tasks in PowerShell is actually pretty easy. All you need to do is use the Office Interop to get access to the default tasks folder, then iterate through the items that are returned.

Create a file called Get-OutlookTask.ps1 in your scripts folder, and paste the following code:

Add-Type -AssemblyName Microsoft.Office.Interop.Outlook
$folders = "Microsoft.Office.Interop.Outlook.OlDefaultFolders" -as [type]
$outlook = New-Object -ComObject outlook.application
$mapi = $outlook.GetNameSpace("mapi")
$tasks = $mapi.getDefaultFolder($folders::olFolderTasks)

foreach($task in $tasks.Items)
{
  if(!$task.complete)
  { 
    $task.subject 
  }
}

Naturally, you could customize this script to work any way you like – for example, you could sort by priorty or date, or you could show an extra column for category information. I have actually set up an alias to this script, so all I have to do is type tasks to see all my current tasks.

If you have set Vim to work with PowerShell, you can import your Outlook Tasks directly into the open document by using the Vim command:

:r! tasks

Pretty cool huh? If you’re looking to add Outlook Tasks in a similar way, you can check out this script by Lee Holmes. I use this one with the alias of nt to save on all that extra typing.

If you want to be able to add Outlook Tasks from Vim, I suggest you add the following function to your vimrc file:

function! Task(args)
  system("nt \\\"" . a:args . "\\\"")
endfunction

command! -nargs=1 Task :call Task('')

Now you just need to call :Task buy milk and it’ll get added – without switching to the shell itself.

Writing and Running F# Scripts with Vim

When I’m writing software for the .NET Framework I tend to have a copy of F# Interactive open. This lets me run commands directly like so…

This is fine for simple stuff, but if you’re writing something a little more complex it’s better to write a script. In the past I’ve done this by having another copy of Visual Studio open and running the script that way. This works really well, and includes colour coding, intellisense and all the other good stuff you expect.

However there are times that having yet another copy of Visual Studio is a little heavy for just keeping track of a script. Enter Vim, the de facto command line editor for Unix and other operating systems.

  1. Set up Vim to work with PowerShell
  2. Grab yourself a copy of the F# Syntax file
  3. Save it into your Vim plugins directory
  4. Add the following lines to your vimrc file…
au BufRead,BufNewFile *.fs set filetype=fs
au BufRead,BufNewFile *.fsx set filetype=fs

Now when you create a .fsx file, you can run it directly from F# Interactive by using Vim’s shell execution feature.

:!fsi %

This will run your script in F# Interactive and present you with the results. The :! Vim command is for running the external program, and the % represents the filename of the currently open document.

Note that you’ll need to have set up F# Interactive by either adding it to the path or setting an alias in PowerShell. If you haven’t done this already, you can do it by adding the following lines to your PowerShell profile:

$FSIPATH     = "C:\Program Files (x86)\Microsoft F#\v4.0\Fsi.exe"

Set-Alias fsi  $FSIPATH

Setting up Vim to work with PowerShell

As an avid console user, I like being able to edit text without opening an interface that requires a mouse. For me this text editor is Vim – the extremely well regarded editor that ships with a very large number of operating systems.

My command line of choice is PowerShell, and I set up any extra commands to live in a Scripts directory inside my Windows user directory. If you’d like to do this you need to download Vim to the Scripts directory, then edit your PowerShell profile to include an alias so you can access Vim from any directory you might be in, as well as a couple of commands to make editing common files even simpler.

# There's usually much more than this in my profile!
$SCRIPTPATH = "C:\Users\Julian\Scripts"
$VIMPATH    = $SCRIPTSPATH + "\vim73\vim.exe"

Set-Alias vi   $VIMPATH
Set-Alias vim  $VIMPATH

# for editing your PowerShell profile
Function Edit-Profile
{
    vim $profile
}

# for editing your Vim settings
Function Edit-Vimrc
{
    vim $home\_vimrc
}

Then you need to set Vim up in the way you like it, there are lots of sites with suggestions for how to set your vimrc file, but for now I’m just going to suggest you add a link back to PowerShell by adding the following lines:

set shell=powershell
set shellcmdflag=-command

This means that when you run the :shell command in Vim, you will actually use PowerShell itself to run commands, including all the aliases you set in your profile. I find this especially handy for writing and running F# scripts, as well as task management with Outlook – both of which I’ll write about in posts this week, but you can test this out now by running :sh or :!get-childitem | more and seeing what happens!

Windows 8 must have a .NET story for the new UI

On one side, I’m excited about the prospects of HTML5-based applications coming to the new Windows 8 user interface which was revealed yesterday. But this has to be only part of the story. Would Microsoft alienate all their existing developers just to go for the next big thing?

The issue comes to play when you think about the current platforms. Many Microsoft developers have spent a long time learning in the WPF and Silverlight user interface frameworks. On this side of the fence things look rather bleak if, especially if you’re currently investing time and money on Windows Phone 7 application development.

But what’s the actual deal? Why are people so worried?

Julie Larson-Green: Today, we also talked a bit about how developers will build apps for the new system. Windows 8 apps use the power of HTML5, tapping into the native capabilities of Windows using standard JavaScript and HTML to deliver new kinds of experiences.

This sounds like it’s the only option for making apps for the new system. Yes I know this is a limited snippet, but there doesn’t seem more information than what is provided here.

We’ll find out for sure at the BUILD conference in September… but from my view they must have a .NET story for the new UI – they’ve invested far too much into the whole .NET ecosystem to not have it running on future tablets.

I’ll be discussing this further on our company website. Check out seo leeds for more info.

Build your own Astrolight app

Astrolight is a simple red flashlight application which you can get in the Windows Phone Marketplace, it’s so simple that I’m going to tell you how you can create one too. With two lines of code.

Assuming you’re all up and running with the free Visual Studio Express for Windows Phone, we just need to start a new Windows Phone Game project…

Then we double click the Game1.cs file and find the Draw method. Here we need to change replace one line with two:

/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
    graphics.IsFullScreen = true;
    graphics.GraphicsDevice.Clear(Color.Red);

    base.Draw(gameTime);
}

Press play and watch your brand new app on the Windows Phone Emulator!

TouchStudio for Windows Phone 7

Microsoft Research has created a fancy project for Windows Phone 7 called TouchStudio – a scripting environment which allows you to write code on your phone with just your fingertips.

I’ve been playing with TouchStudio for a couple of weeks now, and I can definitely say it brings the fun of messing around with a programming language to the phone. But don’t expect to be creating any higher level applications just yet.

The language itself seems to be still in flux right now, including breaking changes in the V1.1 release. But it is a very modern and good looking language, without extra semicolons or curly braces that would be included if you were porting a language like C# over to the phone.

Check Channel 9 for some cool videos, or if you don’t have a Windows Phone you can check out a related project from Microsoft called Small Basic. While the language is totally different to the one in TouchStudio, it does seem to have similar goals for the end user.

Update…

TouchStudio 1.2 has been released. This includes new features such as posting to Facebook!