UNC Paths in Windows Command Shell

Problem:

Copy and other command shell commands don’t work with UNC paths, i.e. \\server\dir.

Solution:

Use pushd <path> to temporarily assign a drive letter and map it to the path.

pushd \\server\rootfolder\subfolder

When done, use popd to unmap the letter.

Extracting date and time from text using Ruby

Problem:
We want to extract a date and time of the format YYYY-MM-DD MM:SS from a body of text using Ruby.

Solution:
This is extremely easy in ruby, and other languages that features regular expressions.


require "Date"

text = "Some text containing a date and a time, for instance 2010-03-19 17:25."

if text =~ /(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2})/
  dt = DateTime.new($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i)
end

Upgrading a Customized Wordpress Theme

Problem:

You’re using a third party wordpress theme but have made changes to customize it for your own needs (changed a picture, added advertising, etc.). When the third party releases an upgrade to the theme you’d like to upgrade but don’t want to lose your changes.

Solution:

Let your version management system move your changes over to the new version for you. I use Subversion, but any modern VMS should do fine as well. Here’s the steps I use to upgrade from, let’s say Cool Theme 1.0 (that contain my changes) to Cool Theme 1.1.

  1. Import the original version of Cool Theme 1.0 (without my changes) to Subversion. I use a repository path like /reposroot/cooltheme/trunk.
  2. Create a tag to freeze the 1.0-version. In Subversion you just copy the above directory, I name the new directory something like /reposroot/cooltheme/tags/1_0.
  3. Create a branch from the 1_0 tag. I’d name the branch /reposroot/cooltheme/branches/r1_0.
  4. Checkout the r1_0 branch to a local working directory.
  5. Copy the files of the updated version of Cool Theme, the one that contain your changes, to your working directory.
  6. Check in. Now you have the current version in the r1_0 branch.
  7. Delete the working copy if you like, it has served it’s purpose and the time has come for the actual upgrade.
  8. Checkout the trunk branch (reposroot/cooltheme/trunk in my example) to a working directory.
  9. Copy the files of the upgraded theme to the working directory.
  10. Check in the changes.
  11. Once again, delete the working copy if you wish.
  12. Create a tag for the new version (reposroot/cooltheme/tags/1_1 for example).
  13. Create a branch for the new release (copy the 1_1 tag to reposroot/cooltheme/branches/r1_1)
  14. Merge the changes between r1_0 and r1_1
  15. Handle conflicts
  16. Check in.
  17. Voila! You should now have an upgraded version with your changes in the new release branch (reposroot/cooltheme/branches/r1_1)

Adding Linebreaks in C#

Problem:

We want to insert linebreak character sequences into a string in C#. 

Solution:

The platform safe way of doing this is by using the Environment.NewLine property.


msg = "An error occurred: " + Environment.NewLine + e.Message;

Composing a string with more than one line break can become really messy, so using string.Format might be a good idea.


msg = string.Format("{1} An error occurred!{0}Exception: {0}{2}",
  Environment.NewLine, CurrentTime, e.ToString());

Dynamic Updating of Action Visibilities

Problem: You want to change an action’s visible property and does so in its OnUpdate event handler, but it doesn’t seem to work. Once an action is hidden it can’t seem to become visible again.  Solution: By design Delphi won’t invoke OnUpdate event handlers for actions that have their Visible property set to false. Instead, one has to override the form’s UpdateActions method tod do the dynamic checking and setting of Visibility.

type
  TMainForm = class(TForm)
  ...
  protected
  ...
    procedure TMainForm.UpdateActions; override;
  ...
  end;

...

procedure TMainForm.UpdateActions;
begin
  inherited;
  // Those actions are updating their visible
  // properties and must therefore be updated
  // here and not in their respective update
  // event handlers. This is due to the fact
  // that invisible actions are not concidered
  // by the inherited UpdateActions loop.
  // This is by design according to CodeGear.

  CancelAction.Visible := IsRunning;
  StartAction.Visible := not CancelAction.Visible;
end;

More information can be found on the Delphi forum, here: https://forums.embarcadero.com/thread.jspa?threadID=18206&tstart=90

How To Create a Tray Icon Controlled Application in Delphi

Problem:

We want to create a System Tray Icon featured application that doesn’t shut down when the user closes the main window. Instead, the form simply hides and is reactivated when the user clicks the associated tray icon.

Solution:

Tested in Delphi 2009.

1. Hide the main form.

Since closing the main form automatically terminates the application we need to hide it from the user. This is done by setting Application.ShowMainForm to False in the project settings.

Application.Initialize;
Application.ShowMainForm := False;
Application.MainFormOnTaskbar := True;
:
Application.Run;

2. Add a TTrayIcon to Main Form

Set the Icon property and add an OnClick event handler that displays your real main form (the one that the user will see).

procedure TMainForm.TrayIcon1Click(Sender: TObject);
begin
  MyOtherForm.Show;
end;

3. Drop a popup menu on the main form and connect it to the tray icon.

Add a close menu item that calls Application.Terminate to kill the application.

procedure TMainForm.Close1Click(Sender: TObject);
begin
  Application.Terminate;
end;

4. Make the real main form show on start of application

Simply add a OnCreate event handler to the form and invoke Show.

procedure TMyOtherForm.FormCreate(Sender: TObject);
begin
Show;
end;

That’s it!

Alternative solution 1.

OBSERVE! This solution has the rather big flaw that it stops the computer from shutting down.

1. Intercept the automatic termination mechanism when the main form is closing

procedure TMainForm.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
  // Minimize application instead of closing
  CanClose := False;
  Application.Minimize;
  // Remove the main form from the Task bar
  Self.Hide;
end;

2. Clicking on the tray icon restores the application

procedure TMainForm.TrayIcon1Click(Sender: TObject);
begin
  // Restore application from minimized state
  Application.Restore;
  Application.BringToFront;
  // We must invoke Show on the main form, otherwise
  // it won't be able to minimize properly a second time
  // Tested on D2009 Upd 3, and Windows XP
  Self.Show;
end;

3. Add a popup menu with a close action to the tray icon, and use Application.Terminate in the event handler to kill the application

procedure TMainForm.CloseActionExecute(Sender: TObject);
begin
  Application.Terminate;
end;

Alternative solution 2

If you want to do it without the TTrayIcon component, this article has what you need: System Tray Delphi Application – Quick and Easy

Rounding off floating point numbers in Ruby

Problem:

If you want to round a floating point number off to two decimals, there is no standard method in Ruby that does it for you.

Solution:

The general solution is

(f * 10**d).round.to_f / 10**d

where f is the floating number and d is the number of decimals.

See my code sample for adding this functionality to the Float class.

Although the above works you should concider the reason you’re performing the rounding (or equivalent) operation. If it’s for presentation reasons only a better way might be to use a format string instead, and leave the original data intact.
Here’s an example where a representation (rounded_str) of a floating point number (f) is rounded to two decimals.

rounded_str = sprintf('%.2f', f)

Turn On ArcSDE Logging

Problem:

You want to log the traffic between the client (i.e. ArcMap) and an ArcSDE database.

Solution:

In the Command Window, write:

>set SDEINTERCEPTLOC = c:\tempclient
>c:\program files\arcgis\bin\arcmap.exe

It’s important to start the client (ArcMap in the above example) from the command prompt. ArcMap will intercept the traffic and log it to temp files, named like this:

c:\tempclient.001, c:\tempclient.002, and so on.

Migrations and Environments in Ruby on Rails

Problem:

You want to migrate a target environment other than development.

Solution:

For production:

>rake environment RAILS_ENV=production migrate

For test:

>rake environment RAILS_ENV=test migrate

Redirect Pages in Wordpress

Problem

I want to move a page in a WordPress site to a different location, but I still want the old address to be active and instead redirect to the new address.

Solution

Unfortunatelly as per WordPress 2.7 there isn’t any standard functionallity to accomplish this, so the easiest way to go is to use plugins.

I use Paul Bains EasyRedirect which works for WordPress 2.7 although the description states “compatible up to: 2.2.0″. Just install it and add a tag of the form [redirect url time] to your redirecting page.

And we have to make sure the redirecting page is not showing up in the automatically generated page menu. I use <a href=”http://wordpress.org/extend/plugins/exclude-pages/”>Simon Wheatley’s Exclude Pages plugin</a> for this. This excellent piece of code adds a check box named “Include this page in user menus” to your page administration page. Very convenient.