Category Archives: Software

How to Set Up a Photography Website Using WordPress

Let’s say I’m a professional photographer and I need a new website. The best thing to do generally is to ask someone else to do it. But what if I’m just starting out and don’t have money to spend on a fancy website? I can get a basic website for free using WordPress.

First of all, I’m going to create a mockup of my new website:

Home page mockup

Home page should have a nice big image, a heading and a menu bar. The gallery page should have thumbnails. Visitor should be able to click on any thumbnail to view a bigger photo. Something like this:

Gallery page mockup

Looks good! Next, I need to decide where I’m going to host my website. WordPress.com looks like a good option: it’s free (as long as your website address ends with .wordpress.com) and it is maintained by the same people that make WordPress.

Go to www.wordpress.com and sign up for a new account.

Creating new website at WordPress.com

The address of my website is going to be acmephotography.wordpress.com

Sign up form at WordPress.com

This is how my brand new website looks like:

Brand new WordPress website

Not very close to the mockup, isn’t it? Let’s fix it.

The next step is to choose a theme for my website. The problem with WordPress themes is that there are just too many of them. After some poking around I decided to try Twenty Twelve theme by the WordPress team. It looks nice and crisp. It has a menu on top. Also, it’s free! Here’s how to set it up.

Go to ‘My Blog’ on WordPress.com and click ‘Dashboard’ link there.

My blog - with Dashboard link

Move mouse over ‘Appearances’ link in the menu on left, then select ‘Themes’:

wordpress-themes-menu

Search for ‘Twenty Twelve’ theme and click Activate button:

Activating WordPress theme

Close the dialog confirming that the theme has been activated. Go to Settings, then General. Set site title to ‘Acme Photography’. Remove tagline (set it to empty):

General settings in WordPress

Don’t forget to click ‘Save Changes’ button. Now click on website name on the top left to view your website.

View website link in WordPress dashboard

Behold: my new photography website, Acme Photography:

Website with Twenty Twelve WordPress template

It has a title and a menu bar. However the front page doesn’t look right. Yet.

Command History in Far Manager

Far Manager has powerful suppport for command history. ConEmu author even suggests using Far Manager with panels turned off instead of standard command prompt.

Ctrl+E gives you previous command entered. You can press it multiple times to get older entries. Ctrl+X moves you forward in the history.

Press Alt+F8 to view command history in the list:

You can execute a command from the list by pressing Enter. If you want to edit a command press Ctrl+Enter.

All shortcuts available in command history list:

Enter Re-execute a command
Shift+Enter Re-execute a command in a new window
Ctrl+Alt+Enter Re-execute a command as administrator
Ctrl+Enter Copy a command to the command line
Del Clear the commands history
Ins Lock/unlock a history item
Shift+Del Delete the current history item
Ctrl+C or Ctrl+Ins Copy the text of the current command to clipboard

Another useful shortcut is Ctrl+Alt+F – it lets you to search in the list by filtering it down. This shortcut is totally non-obvious and not documented anywhere so I was happy when I discovered it. It also works in folder history (Alt+F12).

Far Manager and Console Output

One of the strongest features of Far Manager is the ability to start any program or script from command prompt. You can type anything there, just like in standard command prompt. Ctrl+Enter shortcut greatly helps here – it inserts selected file name to command prompt (Ctrl+F inserts full path).

If you launch a console program all program output will be displayed in Far window. Terrific! After program finishes you can view console output by pressing Ctrl+O.

There is a problem however: you can’t scroll up if program output is long. Only last 25 or so lines of text are visible. Anything before that is lost forever.

Far has a clever little known trick to view all program output. Add view:< before the command and all console output will be intercepted and redirected to internal viewer. For example:

view:<ipconfig /all

You can even redirect output to Far editor by using

edit:<ipconfig /all

This is nice but not ideal. You have to remember to add view:< before starting a console program or script. If a program takes long time to execute you won’t see any progress until it finishes. Also any interactive prompt in program screws the whole process.

ConEmu to the Rescue

ConEmu is a console emulator. It has lots of great enhancements to standard Windows command prompt. It works by intercepting all console output and displaying it in its own window.

ConEmu is also a close friend with Far Manager. After installing ConEmu you can open Far Manager inside ConEmu window:

Far Manager in ConEmu

Once Far Manager is inside ConEmu tab you can scroll console output by switching to so called alternative mode.

This mode hides Far panels and ‘freezes’ console. Ctrl+Up scrolls up, Ctrl+Down scrolls down. Scrolling with a mouse wheel also works.

But that’s not all. You can also assign Ctrl+O to view console output in Far viewer. To do this locate CtrlO_View.reg in ConEmu folder and execute it.

Link

Scott Hanselman on ConEmu

PowerShell Exit Codes

Recently I came across an interesting quirk in PowerShell: exit codes from powershell.exe are inconsistent.

As you may know there are two ways of launching PowerShell script: you can use -File or -Command parameter.

powershell -File test.ps1

or

powershell -Command .\test.ps1

-Command could be ommitted:

powershell .\test.ps1

Now let’s say you want to execute PowerShell script from a scheduled task or from continuous integration server. Naturally, you would be interested if the script fails or not.

And here’s the bug: if you launch your script using -File parameter powershell.exe exits with zero code even if there was an exception.

You can confirm this by creating a script that always throws an error:

throw 'oops'

Now if you launch it using -File parameter exit code is 0:

powershell.exe -File testerror.ps1
echo Error level is %ERRORLEVEL%

oops
At C:\testerror.ps1:1 char:6
+ throw <<<<  'oops'
    + CategoryInfo          : OperationStopped: (oops:String) [], RuntimeException
    + FullyQualifiedErrorId : oops

Error level is 0

But if you use -Command parameter exit code is set as expected:

powershell.exe -Command .\testerror.ps1
echo Error level is %ERRORLEVEL%

Chris Oldwood also came across this bug:

depending on whether I use the “-File” or “-Command” switch to execute the .ps1 script I get different behaviour. Is this a PowerShell bug or is there something fundamental about the execution model the differs between -File and -Command that I’ve yet to understand?

Bonus

How to execute PowerShell script from NAnt script:

<exec program="powershell.exe" append="true">
  <arg value="-noprofile" />
  <arg value="&amp;'C:\your-script.ps1'" />
  <arg value="param1" />
  <arg value="'param 2 with spaces'" />
</exec>

References

How to open HTML files with Visual Studio from Far Manager

Occasionally I want to edit plain HTML file in Visual Studio. You know, those files with .htm or .html extension. Normally pressing Enter in Far Manager opens them in a browser. This is what I want most of the time, so let’s keep it.

Idea: let’s use F4 key to open file in Visual Studio.

Here’s how to achieve it:

* Open Commands menu, select File associations.
* Add new association, enter *.htm;*.html as a mask. Enter this command for F4:

"C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe" /edit "!.!"

Change path to devenv.exe if you have different version of Visual Studio.

Note the /edit parameter. It makes Visual Studio to re-use existing window if it is alredy running. Visual Studio is not exactly a lightweight editor so let’s be efficient here.

What if you want to open HTML file in internal Far editor? Simple: press Alft+F4.

Reference:

Devenv Command Line Switches

Detaching Far Manager from long-running process on Windows 7

Occasionally I launch a program from Far Manager that takes very long to complete. Normally you can use Ctrl+Alt+Tab to “detach” Far from a running process. Technical note #27 explains this well:

If a long-running process (for example, archiving) was run in a FAR console, and for some reasons this very instance of FAR is needed (an editor in the background) or it is undesirable to run a new instance ofFAR, pressing this key combination will create a new console for FAR where it will continue running as if the process has already ended, and the process will continue working in the old console.

Unfortunately this doesn’t work on Windows 7. Instead of detaching you get Windows task switcher:

Windows 7 task switcher

Interestingly the switcher is in “sticky” mode. It doesn’t disappear when you release keys. You can use arrow keys to select a window.

The fix is easy:

  1. Start regedit, open HKCU\Software\far2\System key.
  2. Create ConsoleDetachKey string value, set it to some keyboard shortcut. I use CtrlAltX.
  3. Restart Far Manager

To test it try executing this command from Far:

ping google.com -t

Press Ctrl+Alt+X to get back to Far without stopping pings.

Converting .Net DateTime to JavaScript Date

JavaScript Date constructor accepts number of milliseconds since Unix epoch (1 January 1970 00:00:00 UTC). Here’s C# extension method that converts .Net DateTime object to JavaScript date:

public static class DateTimeJavaScript
{
   private static readonly long DatetimeMinTimeTicks =
      (new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Ticks;

   public static long ToJavaScriptMilliseconds(this DateTime dt)
   {
      return (long)((dt.ToUniversalTime().Ticks - DatetimeMinTimeTicks) / 10000);
   }
}

Usage:

var dt = new Date(<%= DateTime.Today.ToJavaScriptMilliseconds() %>);
alert(dt);

Reference

Java System.currentTimeMillis() equivalent in C#

How to safely remove USB flash drive with Far Manager

Do you have one of these:
USB flash drive
When you finish copying files from and to your USB flash drive you need to safely remove it, otherwise you risk losing data. Normally you do this by clicking icon with green arrow in system tray:

You can do the same with Far Manager. Open drive menu (Alt+F1 or Alt+F2), select your flash drive and press Shift+Del. You should get this confirmation:

Select “Remove” and you’re done. Note that the key combo (Shift+Del) is different from ejecting CD/DVD (Del).

Enhancement to launching PowerShell scripts from Far Manager

The method of launching PowerShell scripts from Far Manager I have described previously has 2 important drawbacks:

  1. You can’t pass arguments to the script, and
  2. You can’t launch script in a separate window

Here’s how to fix this:

  1. Start regedit, open HKCR\Microsoft.PowerShellScript.1\shell\Open\command key
  2. Set it to
    "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe" "-file" "%1" %*

Now you can press Shift+Enter to start PowerShell script in a new window.

As a bonus, you can now double-click .ps1 files in Windows Explorer to launch them. Why is this not a default behaviour? Most likely Microsoft decided to be extra cautious here.

Make sure you have Powershell version 2.0 or higher for this to work. How to check Powershell version.

How to redirect output of console program to a file in PowerShell

At some stage I decided I need backup plan for my virtual server. After some intensive web surfing I found brilliant command line tool – s3.exe. The next step was to schedule to run it periodically, every night, when everybody is sleeping.

Night

I decided to use PowerShell. It’s new, it’s powerful, it comes pre-installed with Windows Server 2008. PowerShell is very popular among system administrators.

Also, I got an idea to save output of backup application to log file so that I could see results later. And this is where things didn’t work as I expected. Here’s how to redirect console program output to a file:

s3.exe > log.txt

If you run this you will be surprised to find that log.txt is empty. As it turns out s3.exe, being a good Windows citizen, writes errors to STDERR stream. To redirect error output to the same file you need to add magic string 2>&1:

s3.exe 2>&1 > log.txt

Two new problems appear. First, errors appear as exceptions in log file:

s3.exe : s3.exe version 1.6 - check for updates at http://s3.codeplex.com
At PS Logging.ps1:4 char:43
+ s3.exe <<<<  2>&1 > log.txt
    + CategoryInfo          : NotSpecified: (s3.exe version ...s3.codeplex.com
   :String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

PowerShell helpfully wraps STDERR output into exceptions. This is nice but I don’t need it! All I want is to see how a program worked by examining log file. Do this to unwrap exceptions and show plain text:

s3.exe 2>&1 | foreach-object {$_.ToString()} | Out-File log.txt

And second, errors are out of order with normal output. They could appear before or after normal lines emitted by a program, making it hard to diagnose problem. To confirm that, I created a simple console program:

static void Main(string[] args)
{
   for (int i = 0; i < 10; i++)
   {
      Console.WriteLine("Hello");
   }
   Console.Error.WriteLine("Bah bah");
   for (int i = 0; i < 10; i++)
   {
      Console.WriteLine("2");
   }
}

If you run this program from PowerShell and redirect output to a file, error message (bah bah) could appear anywhere in the log file:

Hello
Bah bah
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
Hello
2
2
2
2
2
2
2
2
2
2

The solution I came up with is to let old reliable cmd.exe to do the work:

cmd /c s3.exe `>log.txt 2`>`&1

Note backsticks – I use them to prevent PowerShell from parsing redirect operators.

Links

PowerShell ABC's - O is for Output
How to capture exe output to a PowerShell variable
Another approach to unwrap exceptions: use add-content cmdlet.