Archive for March, 2008

Writing An Application That Opens Windows Live Writer

Everyone knows that you can easily make a plugin for Windows Live Writer, but one of the APIs that’s not as well known or spoken about is the Windows Live Writer Application API. This set of API calls will allow you to open Windows Live Writer in a particular way. What do I mean by that? Well, you can open Live Writer at the Open Post window, or the options window, as just a couple of examples. The real power with this API is the BlogThis set of calls.

Some of you might be familiar with the Blog This addins for Internet Explorer (no longer available) and Firefox, which would allow you to click on the Blog This button in your browser, and it would open up Live Writer with either the page details loaded in Writer, or the highlighted text from the webpage you were viewing. Also anyone who uses my Live Writer sidebar gadget in Vista, this uses the same principle. In this article I will show you how to write a simple Windows application that uses some of the BlogThis calls.

To start off, make sure you have the latest version of Windows Live Writer installed by going to http://writer.live.com/. Now, create a new project in Visual Studio and add a reference in that project to the Windows Live Writer COM assembly (C:\Program Files\Windows Live\Writer\WindowsLiveWriter.Application.dll) and then for using it we put:

using WindowsLiveWriterApplicationLib;

Now we have to create an instance of the WindowsLiveWriterApplicationLibClass:

        WindowsLiveWriterApplicationClass wlwapp;
        public Form1()
        {
            InitializeComponent();
            wlwapp = new WindowsLiveWriterApplicationClass();
            cbOptions.SelectedIndex = 5;
        }

Now, for this example I have just created a basic WinForms application with a series of buttons for showing the different functions, it looks like this:

This App will show a few examples, but not all, after a certain point, they’re all one and the same really, just for a different use.

We’ll start off with the basic Open Writer button. The call that we make for this one is NewPost(), as that is effectively opening Writer as if running the program from a shortcut. So the code for this is:

        private void btnOpenWriter_Click(object sender, EventArgs e)
        {
            wlwapp.NewPost();
        }

For the Open Post button, we use OpenPost():

        private void btnOpenPost_Click(object sender, EventArgs e)
        {
            wlwapp.OpenPost();
        }

Now for the Open Options button, we use the ShowOptions() call. This call can be made with no parameters, and it will default to the preferences tab in the options window. However, you can put in a string parameter to open a different tab from the options window, and you can choose from the following: "preferences", “accounts”, "blogthis", “spelling”, “glossary”, "plugins", "webproxy", and "pings". And you can see from my App that I have put this in a drop down:

Which for the code looks like:

        private void btnOpenOptions_Click(object sender, EventArgs e)
        {
            string option = cbOptions.SelectedItem.ToString();
            wlwapp.ShowOptions(option);
        }

Now, the cool thing about this API is when it comes to putting images into a blog post and there are a couple of methods you can use for Images, one is for a single image, and one is for an image-set, so for multiple images. I’ll start with the single image method first which uses the BlogThisImageFile() call:

        private void btnOpenWithImage_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)
            {
                string title = string.Empty;
                if (!string.IsNullOrEmpty(txbxBlogTitle.Text))
                {
                    title = txbxBlogTitle.Text;
                }
                else
                {
                    title = "My New Blog Post";
                }

                string imagefile = openFileDialog1.FileName;
                
                // Open a new post using the imagefile we just selected

                wlwapp.BlogThisImageFile(title, imagefile, txbxComment.Text);
            }
        }

And for multiple images, we use the BlogThisImageFileList() call:

        private void btnOpenWithFiles_Click(object sender, EventArgs e)
        {
            DialogResult result = openFileDialog2.ShowDialog();
            if (result == DialogResult.OK)
            {
                string title = string.Empty;
                if (!string.IsNullOrEmpty(txbxBlogTitle.Text))
                {
                    title = txbxBlogTitle.Text;
                }
                else
                {
                    title = "My New Blog Post";
                }

                string[] imagefiles = openFileDialog2.FileNames;

                // Open a new post with the files we selected

                wlwapp.BlogThisImageFileList(title, imagefiles, txbxComment.Text);
            }
        }

The BlogThis set of calls also has one for URLs, BlogThisLink(), which will most likely be what the Blog This add-ins for IE and FF use. The code for this is:

        private void btnOpenUrl_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txbxUrl.Text) &&
                txbxUrl.Text.StartsWith("http://"))
            {
                label3.ForeColor = Color.Black;
                string title = string.Empty;
                if (!string.IsNullOrEmpty(txbxBlogTitle.Text))
                {
                    title = txbxBlogTitle.Text;
                }
                else
                {
                    title = "My New Blog Post";
                }
                wlwapp.BlogThisLink(title, txbxUrl.Text, txbxComment.Text);
            }
            else
            {
                label3.ForeColor = Color.Red;
            }            
        }

As I mentioned, I’m not going to go into detail about the others, but I will give a quick description of them:

  • BlogThisFeedItem() – This would be used in an RSS Feed Reader program, in the way that FeedDemon uses it.
  • BlogThisHTML() – This would be used to start Writer with some preconfigured HTML in the blog entry (self explanatory really)
  • BlogThisImageUrl() – Much like the BlogThisLink() call, this deals with links again, but image links, similar to if you inserted a picture and choosing a picture from the web.
  • BlogThisSnippet() – This is another one I suspect the Blog This add-ins use. In those examples this would be where you highlighted some text on a webpage to blog about it.

There are some good examples of where these API calls have been used, and I have tried to cover the main ones in this post, although one I haven’t mentioned is one I used quite frequently, which is the Live Writer System Tray App, which works in a similar way to my sidebar gadget.

If you wish to download the source code for my example app, you can do so from my SkyDrive area.

SL

Advertisements

9 Comments

Insert Video Source Code Released!

In a decision I have been mulling over for quite some time now, I have finally decided to release my Insert Video plugin’s source code. The reason I hadn’t released it before was because of the amount of time and effort I had put into making the plugin, it was my pride and joy, and I wasn’t comfortable releasing all my hard work so freely, call it selfish if you will, I don’t care .

If I’m honest though, the main reason I have released this isn’t so people can see how I made the plugin, but to be able to add to it themselves. I have a number of projects that I’m working on at the moment and just don’t have the time to maintain the plugin, to add in the video sites that people are requesting. So I figured if the source code is out there, the community themselves can actually update and build the plugin themselves with added sites.

Now, I will warn any enthusiast that wants to look at the code, it’s not clean, this was mainly written about 18 months ago and I was still very much finding my feet when it came to programming, having just got back into it again. There are virtually no comments, if any at all. My logic, whilst seemingly logical to me, might escape others and have them scratching their head thinking "what the f…". I don’t blame you, I actually had the same problem when I did the last update . But, I’m more than happy to help people out in trying to decipher the code, and if they do have any problems, then please feel free to raise a new discussion post and I will help out in there. You can email me if you really need to, but I am requesting that the discussions be held within the discussions section on CodePlex, this is mainly so that others can see the questions and answers.

So, go over to the source code now on CodePlex: http://www.codeplex.com/InsertVideo

SL

Whilst writing this I was listening to Oasis10 Cigarettes & Alcohol

3 Comments

Insert IMDb Plugin

For the longest time I’ve wanted to do a plugin for IMDb, but unfortunately, they don’t offer any kind of API to tap into, which would mean doing a whole bunch of screenscraping, which to be honest, I really couldn’t be bothered to do. Now, IMDb still doesn’t offer any kind of API, but I was on CodePlex the other day and found someone has been writing a wrapper for IMDb that does all the screenscraping that I would have to have done. Great So I started playing around, working out what I would want for my plugin, and I came up with this:

It’s very easy to use, there are two methods in which you can get the film details, you can either search for the film you want, or if you have the IMDb link, you can paste the link in and get the relevant details:

So now that you have the film information you want, you can choose what parts of it to put in your blog entry, so if you didn’t want the description to appear, just untick it and it won’t appear. Once you’re ready to insert, hit Insert and you’ll get something like this:

Released: 1993

Go to IMDb page

Information © IMDb.com

Jurassic Park

Sam Neill, Jeff Goldblum, Bob Peck, Joseph Mazzello, Samuel L. Jackson, Wayne Knight

Scientists clone dinosaurs to populate a theme park which suffers a major security breakdown and releases the dinosaurs.

Easy huh. Now, when it comes to what information you want to show, I have given three predefined templates, or you can choose a custom option:

Now, as I mentioned, I’m using the IMDb wrapper from CodePlex, but I had to make a slight alteration to its code for use in the plugin, so the plugin itself comes with a slightly custom build of the wrapper. All I did was change the following line of code from:

Wc.Proxy = Nothing

to:

Wc.Proxy = PluginHttpRequest.GetWriterProxy()

This makes sure my plugin carries on like my other plugins in using Writer’s proxy settings so it can be used behind a proxy. As the source code for the original wrapper is Open Source, I have also included the modified source code within my own project page on CodePlex.

I should however note that the wrapper was written in .NET 3.5 which means for my plugin to use it, it had to be built in .NET 3.5 too, which was kind of annoying, but hey ho. If you want to use this plugin you have to make sure you have .NET 3.5 installed. If you don’t you’ll be prompted by the installer anyway.

Download: http://gallery.live.com/LiveItemDetail.aspx?li=aa71d18d-e738-475d-85fa-198c64ea3987
Source code: http://www.codeplex.com/InsertIMDbInfo

SL

Whilst writing this I was listening to Manic Street PreachersInteriors (Song for Willem De Kooning )

Leave a comment

My Live Writer Article for CoDe Magazine

Last year at the back end of November, I got asked by CoDe Magazine if I would like to write an article for a special edition of the magazine dedicated to Windows Live that would be getting published for Mix08. Sure, I said. So I wrote my article on developing plugins for Windows Live Writer.

Well, Mix was last week, and on the Wednesday, the magazine was published, complete with my article . And now, a week on, the online version of the magazine is up, including the online version of my article. All rather cool really

CoDe Magazine – 2008 – Vol. 5 – Issue 2 – Windows Live

Developing Plugins for Windows Live Writer

SL

Whilst writing this I was listening to GarbagePush it

4 Comments

What The…?

I just went to check something in the source code of one of my Live Writer projects, so I went to CodePlex, and on the front page I saw this:

Notice the second one!

I didn’t even realise I’d put the installer up for download on CodePlex, I thought it was on Live Gallery only. Anyway, so I went to the release page to see how many downloads and was literally gobsmacked!

 

That’s almost 4 times as many downloads as my video plugin! So this makes it officially my most popular plugin, and possibly the most popular plugin, period! Wow!

SL

Whilst writing this I was listening to Lenny KravitzLittle Girl’s Eyes

2 Comments