Category Archives: Useful

WPF Popup focus nightmares

We have a fairly advanced windowing system for the app I’m developing, and one of the side effects is that when using controls inside a Popup control, the keyboard focus does not always behave correctly for the controls inside the popup.

I found this great article which immediately solved my problem :

The root cause of my issue is that a Popup’s HWND has the WS_EX_NOACTIVATE flag set, meaning that the focus is not set properly.

Credit to Philipp Sumi and Andrew Smith.



Leave a comment

Filed under UI Hints, Useful, UX, WPF

Scripting iTunes – Turning Films into TV shows

A while back, I posted about using the iTunes API for automating tasks in my music & video collection.

However, I didn’t go into a lot of detail, and so  now I thought I’d share a bit more – in particular how to automatically organise videos into the “TV shows” section in iTunes.

Previously I had tried to drag my freshly downloaded/ripped videos into the “TV Shows” area, but they always ended up in “Films” – and the only videos in my TV shows section were ones that I actually bought off the iTunes store.

A quick bit of googling, and lots of experimentation later – I wrote a fairly simple jscript:

var iTunesApp = WScript.CreateObject("iTunes.Application");
var tracks = iTunesApp.SelectedTracks;
var numTracks = tracks.Count;
for (i = 1; i <= numTracks; i++)
  var currTrack = tracks.Item(i);
  var name = currTrack.Name
  var se= name.substring(name.lastIndexOf(".")+1);
  var show = name.substring(0,name.lastIndexOf("."));
  var bits = se.substring(1).toUpperCase().split("E");
  var season = bits[0];
  var episode = bits[1];

  currTrack.Show = show;
  currTrack.SeasonNumber = season;
  currTrack.EpisodeNumber = episode;
  currTrack.Album = show;
  currTrack.Artist = show;
  currTrack.TrackNumber = episode;
for (i = numTracks; i >= 1; i--)
  var currTrack = tracks.Item(i);
  urrTrack.VideoKind = 3;

The script splits the filename up into show name, season number and episode number, and then sets the “VideoKind” to 3, which means TV Show.

The only requirement is that the starting filename should be in the format Show.Name.S01E01.mp4


Leave a comment

Filed under Apple, Useful

While you were sleeping…

I just came across an interesting use case which I thought I’d share – why let your application churn away doing unnecessary processing if the user isn’t paying attention?

So I started looking into detecting whether the workstation is locked, or if the screensaver is running – two fairly reliable ways of telling that the user has gone walkabout…

To detect a locked workstation, you need to listen out for:


…which fires each time the user locks or unlocks the workstation.

To detect the state of the screensaver, you have to do slightly more work – there are no helpful events – instead you need some sort of polling mechanism to call away to a Win32 function:

private const int SPI_GETSCREENSAVERRUNNING = 0x0072;
[DllImport("user32.dll", SetLastError=true)]
static extern bool SystemParametersInfo(int action, int param, ref int retval, int updini);

int active = 1;
SystemParametersInfo(SPI_GETSCREENSAVERRUNNING, 0, ref active, 0);
bool screenSaverActive = (active == 1);

So I stitched these 2 bits of logic together into a nice helper class and now I can tell (within reason) when my users are there or not, and give our servers a break every now and then.

Leave a comment

Filed under UI Hints, Useful, Winforms, WPF

Scripting iTunes

Love it or hate it, iTunes is a massively popular piece of software, not because it’s fast, good looking or ultra-usuable, but because you need it if you’re one of the millions and millions of people that own an iPhone, iPad or iPod.

However, I was recently very pleasantly surprised when I found that iTunes has a COM interface that allows you to very easily script actions – such as bulk renaming your tracks, or bulk-editing track metadata

Although I’m a C# developer, one of the most convenient ways to access this functionality was actually JScript:

var iTunesApp = WScript.CreateObject("iTunes.Application");

I wrote a script to take some videos that were showing as “Movies” section in iTunes, and changed the metadata (“Media Kind”, “Episode Number” and “Season Number”)  and made them show up in the “TV Shows” section of iTunes.

Useful Links:

Leave a comment

Filed under Apple, Useful