Wednesday, February 11, 2009

AdSense JScript error

I don’t know wether it is just IE7 or what but I do seem to be getting a lot of JScript runtime errors on lots of different sites… I have opened a few in the debugger and found them to be, for the most part strangely, coming from within jQuery…

Now the AdSense stuff on this blog has started to have an error:

The bit where it barfs is:

<script>tick('1ad')</script>

with:

Object expected

so meh, not finding the tick function. I have not changed anything, but I am beginning to wonder if it is not something that I have done to this machine. Perhaps I am just paranoid, but I do seem pretty capable of some kind of voodoo against windows installations…


[Edit] S’not just me - there are some posts on the AdSense Help forum (which has a JScript error, haha) about this already (Recent Javascript Errors in Ads, Javascript errors, JavaScript error <script>tick('1ad')</script>) but no response from Google so far...


[Edit] Silently fixed by Google. I did not keep a copy of the offending source file from this morning, but looking at the same file generated just now, they seem to have totally removed the call to tick. That function by the way ends up injected into your main page and seems to be for storing timestamps for certain actions like the first load, the first time the page is scrolled, the load time of each ad etc.) Poor form really… perhaps an explanation will surface soon.

Tuesday, February 10, 2009

Multiple selection

I was watching someone the other day trying to select a number of different files from a directory to copy somewhere else. She had about four goes at it, control clicking on each file she wanted, skipping over the ones she didn’t, then accidentally clicking without holding control somewhere and undoing all of her selections, before she gave up and copied them one at a time.

Just now I was trying to move some gmail ‘suggested contacts’ to ‘my contacts’. I had never used that interface before, but I don’t like it. You select each contact by checking its checkbox, but for some odd reason they have made clicking on the name of the contact select the checkbox also – and unfortunately deselect all of the other ones! The nice thing, usually, about a list of checkboxes is that they avoid the accidental de-selection that is so easy with the control click way of doing things…

This all has just reminded me that it would be nice to have an explorer extension that made selecting multiple files more forgiving. Maybe by altering the selected state in a more robust way. You could do undo and redo of selection, you could do add to and subtract from selection, you could keep selections or save and re-load them… you could make it usable. As it is, it is just about inevitable that someone not hugely skilled with the mouse will loose all of their selections if they are trying to select a non-trivial number of files together.

SQLServer 2005

Having failed to install SQLServer 2008 – don’t ask, I grabbed the 2005 dvd and ‘installed’ it… But the server setup could not find any of the client msi files. I looked at the directory structure on the disk and where they used to have Server and Client directories, there are now Server and Tools. I am not sure if that is the problem but I went into the Tools directory and ran the setup there. It got some way through its install, but still could not find the msi for the native client, and the ‘workstation, books online and development tools’ installer would not run. So I went spelunking in the \Tools\Setup directory and ran a few msi files by hand and now it seems to be happy.

Yay, I have my SQL Server Management Studio!

The msi installers that actually managed to do something were: \Tools\Setup\sqlncli.msi and \Tools\Setup\SqlRun_Tools.msi though there may be other stuff missing… we will see.

TortoiseSVN right drag

I did not know this: http://tortoisesvn.net/most-forgotten-feature

SVN global ignore pattern

I have just been fiddling with my global ignore pattern so I thought I would post it here (more for my future self than anything else). This modification was triggered by my having fun trying to use the same repo from two different machines where the directory structure is not identical, so the *.suo files and *.user files can cause problems. I also noticed that there were a few other odd files in the repo that I don’t really want.

[Bb]in [Oo]bj Debug Release *.suo *.user _UpgradeReport_Files UpgradeLog.XML RECYCLER Thumbs.db Desktop.ini

So a little explanation:

  • [Bb]in [Oo]bj – directories containing build files generated by studio, seems like older versions of studio create the files in all lowercase where 2008 has an initial cap and as SVN is case sensitive you unfortunately need to deal with both
  • Debug Release – directories created when building a deployment project
  • *.suo *.user – user project setting and general studio user settings files
  • _UpgradeReport_Files_ – directory containing log files and stuff created when you migrate a project from an older version of studio
  • UpgradeLog.XML – the main project migration report file
  • RECYCLER – not sure if this is still needed, but I was finding this directory sometimes gets created, I guess it is for storing deleted files… was just searching for it in the repo local copy but could not find it, oh well
  • Thumbs.db Desktop.ini – files created when you do certain things in explorer, like look at a directory of images in thumbnail view

Monday, February 09, 2009

Office Live

I have a new install of Microsoft Office Word 2007 with the Office Live plug-in installed. The Office live splash screen comes up and I was having trouble configuring it – it could not seem to deal with my Live account that had a Gmail address… anyway, that is sorted now (though all my cookies are gone in the process), but the splash screen still comes up each time. I thought it was because the sign-in process was failing, but I am all set up now and it is still showing up every time I open word.

Fortunately I have found the solution on TechNet: Get Started with Office Live.

The workaround is to create a registry key called OfficeLive under the key HKEY_CURRENT_USER\Software\Microsoft.

Group policy

I hate that the group policies specified for our domain accounts have the Internet Explorer home page locked down, and the web proxy enabled. Those settings though are just registry entries, and as there is no inherent security on registry editing, you can just go back in and edit them after login.

After some time interval though I have noticed that there must be a gpupdate or something that resets the keys to the values defined by the policy. I just wrote a quick VBScript to ensure that my preferred settings are made after login.

Set shell = WScript.CreateObject("WScript.Shell")
 
shell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Start Page", "http://www.google.com.au", "REG_SZ"
shell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Default_Page_URL", "http://www.google.com.au", "REG_SZ"
shell.RegWrite "HKCU\Software\Microsoft\Internet Explorer\Main\Search Bar", "http://www.google.com.au", "REG_SZ"
shell.RegWrite "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings\ProxyEnable", 0, "REG_DWORD"

Most of the time when I pop open IE it is to go to Google. Plus the group policy had some stupid HP news site specified, and several times over the last week their first displayed news story was some model in a state of partial undress – difficult it is when the company policy enforces a breach of company policy…

Sunday, February 08, 2009

WLW Plug-in

I just finished working on a Windows Live Writer plug-in to allow insertion of snippets. You can define templates for snippets that you can then insert into the current document from the Insert list. The snippets can be anything basically, but I will be using it to insert HTML snippets of the right format to make error message blocks, command line blocks, basically all of the little bits of mark-up that I want to re-use all of the time.

To create a new template you access the options dialog via Tools > Options > Plug-ins where you select the Snippet plug-in and click Options.

You can insert the currently selected text or the clipboard contents into the template by specifying #content# or #clipboard# somewhere in the template. You can also take the content or the clipboard if the content is blank with #contentorclipboard# or the reverse with #clipboardorcontent#.

There is no documentation (other than the info here), no installer (you just need to copy the dll file to C:\Program Files\Windows Live\Writer\Plugins), and not too many features. So hopefully not too many bugs. There are a few things that I would like to work on: A context menu to select the #content# things; Pretty up the interface a bit; Add a different mode that can ask for user input to fill in the template; An installer.

I created a Google Code project for this one too, it is wlwsnippetplugin. I think that I have put the source up properly in the SVN repository… Regardless, the source is zipped there too, as is the SnippetPlugin.dll file that you can just grab and copy to the right directory.

Enjoy!

Mark-up cleanup

I have just spent some hours going through all of my old posts and editing them in WLW to get all the mark-up nice and neat and how Blogger and WLW likes it.

I have put everything in p tags, removed all of the display oriented tags (fonts, inline classes), and fixed up the code and error message and command text blocks that I had. I think I fixed most of the quote marks and apostrophes too - mainly because WLW does it automatically and they do look pretty neat.

I guess that is all a bit anal, but I do feel clean now.

Friday, February 06, 2009

Erm… ForEach

Why did I not see this before? I was pretty sure that I looked for a ForEach function before I went off looking for Map etc… but bugger me if there isn’t an extension method on IEnumerable(Of T) that does just exactly what I want.

It’s even called ForEach!

List<thing> myList = new List<thing>();
myList.ForEach(item => item.DoSomething());

[Edit] Okay, I feel a little better now… I know why I didn’t see it before - ForEach is defined on List(Of T) and not, unfortunately, on IEnumerable(Of T) which is where I was looking for it. Perhaps next week in a spare moment I might get to try my hand at writing it.

Select method problems…

An issue that I have run into using Select as Map HOF.

The Select function is only evaluated when the returned Enumerable(Of T) is evaluated. That means that the code that I posted (which I did not test until now…) does not really do anything. I guess that means that the foreach method is probably the way to go for calling a function for each item in a list.

Thinking about this, and re-reading some information about the Map function, I come to the realisation that it is perhaps my understanding of what Map is supposed to do that is incorrect. It does say that map is for transforming the items. Perhaps what I want is another, slightly different HOF that is for operating on a list of items, i.e. calling a function for each of them, rather than for building a second list. Oh I don’t know, I will keep looking.

Windows Live Writer first paragraph problem

I was having trouble with WLW messing up the mark-up for the first paragraph when I start a new blog entry.

Once I had typed the first paragraph then pressed enter, rather than starting a new paragraph tag after the first, it inserted a new one in the first one before the text. After that the paragraphs come up in pretty much the right order, but the nesting is all wrong and not at all what you would want.

Here is an example of the mess that it manages:

<p>
  <p>Second paragraph.</p>
 
  <p></p>
Third paragraph.
 
  <p>First paragraph.</p></p>

What you want really is this:

<p>First paragraph.</p>
 
<p></p>
 
<p>Second paragraph.</p>
 
<p></p>
 
<p>Third paragraph.</p>

I have finally worked out what was causing this. It was the fact that I was clicking in the edit area before typing! So if you edit the title, you need to NOT click in the post body area, you can just hit Enter after you are done with the title and it seems to work.

Unfortunately, clicking will have bad effects on the mark-up whenever you do it in a blank area… You need to be clicking in an existing paragraph or else it seems to get a bit stuffed up. I am still trying to work out exactly which action causes which problems and hot to possibly avoid them. One is to paste in the html for a paragraph with some text in it rather than clicking at the end of a post to add a new paragraph. This is a beta product though so I guess one is meant to deal with quirkiness.

Another related note: you really have to hit Enter at the end of a paragraph, even the last one. If you don’t you can end up with:

<p>Paragraph 1.</p>
 
<p></p>
Paragraph 2.
 

I like WLW. I am sure there is not much more to go before it is kind to your mark-up. The sad thing is that this kind of stuff is probably not that important to a lot of people.

Thursday, February 05, 2009

foreach to LINQ to HOF progression

Three stages of a real world example.

protected virtual string ReplaceFunctions(string messageTemplate) {
    string messageBody = messageTemplate;
    Regex ex = new Regex(@"\$(?<name>[^\$]+)\$");
    MatchCollection functions = ex.Matches(messageTemplate);
 
    foreach(Match function in functions) {
        if(function.Groups["name"].Success) {
            messageBody = messageBody.Replace(string.Format("${0}$", function.Groups["name"].Value), "");
        }
    }
    return messageBody;
}

protected virtual string ReplaceFunctions(string messageTemplate) {
    string messageBody = messageTemplate;
    Regex ex = new Regex(@"\$(?<name>[^\$]+)\$");
    MatchCollection functions = ex.Matches(messageTemplate);
 
    foreach(var function in (from Match f in functions where f.Groups["name"].Success select f)) {
        messageBody = messageBody.Replace(string.Format("${0}$", function.Groups["name"].Value), "");
    }
 
    return messageBody;
}

protected virtual string ReplaceFunctions(string messageTemplate) {
    string messageBody = messageTemplate;
    Regex ex = new Regex(@"\$(?<name>[^\$]+)\$");
    MatchCollection functions = ex.Matches(messageTemplate);
 
    functions.OfType<Match>().Where(function => function.Groups["name"].Success).Select(function => messageBody.Replace(string.Format("${0}$", function.Groups["name"].Value), ""));
    
    return messageBody;
}

C# Map HOF goodness

The Map higher order function is provided in C# as the Select extension method.

So to cover the last step that I was going on about this morning, i.e. to transform this:

foreach(var field in (from field in TheForm.Fields where field.Disabled select field)) {
    field.DoSomething();
}

We can use method syntax (fluent) rather than query syntax to give the one liner:

TheForm.Fields.Where(field => field.Disabled).Select(field => field.DoSomething());

Neato!

Incidentally though, this seems to only work when DoSomething returns a non void. For example, if you try to compile:

TheForm.Fields.Where(field => field.Disabled).Select(field => Console.WriteLine(field.Name));

You will get an error:

The type arguments for method 'System.Linq.Enumerable.Select<TSource,TResult>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TResult>)' cannot be inferred from the usage. Try specifying the type arguments explicitly.

This is pretty easy to fix though by changing the expression lambda to a statement lambda:

TheForm.Fields.Where(field => field.Disabled).Select(field => { Console.WriteLine(field.Name); return true; });

Windows Live Writer and Blogger formatting

I think I have it worked out.

There is a Blogger setting that stuffs up edits done with Live Writer. Settings > Formatting > Convert line breaks needs to be set to No. Then you don’t seem to get the double line spacing.

The trick with WLW seems to be to let it put in paragraph tags and to never ever edit the post in blogger. Or at least to never switch between the blogger html editor and the compose editor – that really screws things up.

In the end, WLW is still in beta so there are issues. Undo does not do what I would expect. You can’t just type away and press enter to get a new paragraph… Seems like for the first paragraph you have to hit enter then the down arrow otherwise the insertion point is above the first paragraph container. Odd.

I will just resign myself to being careful with the editor, then being prepared to fix the mark-up by hand.

The CodeSnippet pug-in works best if you do not embed the styles but copy them to your template to be re-used. That is how I wanted to do it anyway. Here is a little code snippet:

// Check for required fields
foreach(var f in from f in TheForm.Fields
                 where f.RequiredForEmail
                 select f) {
    prop = requestType.GetProperty(f.Name);
    if(prop == null || prop.GetValue(data, null).IsBlank()) {
        Ok = false;
        break;
    }
}

Query pattern problem

I have been getting this error with some LINQ expressions:

Could not find an implementation of the query pattern for source type 'System.Text.RegularExpressions.MatchCollection'. 'Where' not found. Consider explicitly specifying the type of the range variable 'v'

You get this error because MatchCollection implements only IEnumerable and not IEnumerable(Of T) All you need to do is change:

from v in ex.Matches(template)

to:

from Match v in ex.Matches(template)

I.e. do what it says and specify the type of the range variable.

CodeSnippet plug-in

Dammit. Those code snippets look so nice in Live Writer… grr. They get all sorts of horrible linebreaks and the formatting does not work at all. Work dammit!

Explorer:

Live Writer:

Grumble.

LINQ expression syntax

I have been recently starting to use LINQ expression syntax a little more. One refactoring that I have been doing is changing things like:

foreach(SomeItem item in items) {
    if(item.SomeProperty == someValue) {
        item.DoSomething();
    }
}

into the slightly nicer:

foreach(var item in (from i in items
                     where i.SomeProperty == someValue
                     select i)) {
    item.DoSomething();
}

… My only hesitation is the nastiness of the syntax (if you try to get it inline with no extra variable definitions – yes I know the extra parentheses are not required, but I find var item in from i in items to be very unreadable, plus I am often adding method syntax to these things at a later stage)… Some syntactic sugar might be nice to get:

foreach(var item from items                                      
                 where item.SomeProperty == someValue                 
                 select item) {
    item.DoSomething();
}

There is probably also a higher order function way to do this for simple cases of just calling one function on each match, but most of the time the code is not as simple as a single method call. Ideally I would love to do something like:

(from i in items where i.SomeProperty == someValue select i).Map(DoSomething);

P.S. Do not like the new Windows Live Writer editor. I just want an editor that makes it easy to create nice neat mark-up! Why is it so difficult to NOT put in paragraphs and other containers that I don’t need? And why can’t I see them in edit mode? And why do I drop out of the edit container and then have the backspace key delete the whole container?

Friday, January 30, 2009

WindowsUpdate

Installing Studio and stuff on my new desktop (laptop is almost officially dead) I was having a problem with Microsoft Update...

The website has encountered a problem and cannot display the page you are trying to view. The options provided below might help you solve the problem.

The error code is 0x8007000B. Looking in the WindowsUpdate.log shows:

WARNING: DownloadFileInternal failed for http://download.windowsupdate.com/v8/windowsupdate/b/selfupdate/WSUS3/x86/Other/wsus3setup.cab: error 0x8007000b

So I tried to download that file in ie and get:

Access to http://download.windowsupdate.com/v8/windowsupdate/b/selfupdate/WSUS3/x86/Other/wsus3setup.cab has been blocked by WebMarshal

Ah ha!

(P.S. I don't remember the blogger editor being this bad... the format of this post is terrible right now.)

[Edit] Formatting maybe fixed...

Thursday, January 29, 2009

Lambda expressions

I just used a lambda expression for the first time. They are neat!