SQL Server Management Studio and Macros

There’s a plugin available to add macros to SSMS, but I don’t need more than about 1 functionality: Convert a results column I just copied to a Comma Separated Values (CSV) list.  Usually I select the text, run a Search & Replace, change the options to enable “Selection Only” and “Regular Expression”, type in \n\r, and , and then run the search.  That’s kind of a hassle.  If it’s just a small section of text, I’ll even do ye olde “end, comma, delete, repeat” carpal tunnel exercise.  But I hate that, and I wanted something better.

I knew about AutoHotKey previously but didn’t know it could modify clipboard contents. So today I made some magic: Now Win+, (as in Comma Separate) is defined in the script below to:

  • Search my clipboard contents for Carriage Return + Line Feeds and replace them with Commas.
  • It also handles the case where I want the values wrapped in single-quotes.
  • It also sorts and removes duplicates from the list.

It’s bound to Win+, in the script, and you can change that – it’s commented.

To install this, download and install AutoHotKey, then save the script below somewhere on your computer and double-click it to load it in memory (so Win+, will work).  You might also put it in your Startup folder so it’s loaded every time you log in.

Usage:

  1. Make sure the script is running
  2. Select some text containing line feeds – or a column of SSMS Grid results
  3. Copy it
  4. Hit Win+,
  5. Answer the question about single quotes (Include them or No.. or Cancel?)
  6. Paste it
If you’re using a tool like Ditto, that’ll save the before/after versions too, which can be handy.  I’m sure similar scripts can be created for other SSMS shortcomings, too.  Oh, and there’s nothing about this that limits you to using it in SSMS.  It’ll work in any program on whatever is in your clipboard- SSMS is just my use case.

Lines-To-CSV.ahk

; Lines-To-CSV.ahk
; Lines-To-CSV.ahk
; AutoHotkey Version: 1.x
; Language:       English
; Platform:       Win9x/NT
; Author:         A.N.Other 
;
; Script Function:
;	Search & Replace clipboard contents: Convert line feeds to comma+space
;
;	Example:
;		1
;		2
;		3
;
;	Becomes: 1, 2, 3
;

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

#,:: ; bind to [Win]+[,]

; Grab the current clipboard contents. Call it haystack
haystack := Clipboard

; Define what we're splitting on: CR+LF
needle := "\r\n"

; Check to see if there's any lines to convert here.
FoundPos := RegExMatch(haystack, "\r\n")
if FoundPos = 0
{
    MsgBox, 64, Convert Lines to CSV, Clipboard contains no lines to convert to CSV., 2
    return
}

; Prompt user for quote option
;MsgBox 3, Convert Clipboard Lines to CSV, Converting Clipboard lines to CSV`n`nWrap lines in single quotes?
;IfMsgBox Cancel
;	return
;else IfMsgBox Yes
;	quote := 1
;else
;	quote := 0

; Can we auto-determine quote option? Get the first 10 items in the list, if they're all numeric, no quotes.
; Can always save & tweak this script as a separate hot key to force quotes.
test := RegExReplace(haystack, needle, ",")
quote := 0
Loop, parse, test, `,
{
if A_LoopField is not number
{
	quote := 1
	break
}
if a_index > 10
	break
}
	
If quote > 0
    replacement := "', '"
else
    replacement := ", "

; Optional Sort & remove Dupes  If you don't want sorting, comment this out.
; If we're using quotes, sort as text.  If not using quotes, sort as numbers...
; Sort haystack, use linefeed as delimiter, remove duplicates
If quote > 0
	Sort, haystack, U
else
	Sort, haystack, N U

; Perform the RegEx find and replace operation,
    result := RegExReplace(haystack, needle, replacement)

; If using quotes, the result string needs a first & last single quote added
If quote > 0
    result := "'" . result . "'"

; Empty the Clipboard
    Clipboard =
; Copy the result to the Clipboard.
    Clipboard := result
; Wait for the Clipboard to fill.
    ClipWait

; Optional Paste:
send, {CTRLDOWN}v{CTRLUP} ;send normal paste command
	
; Done!
    return

Google Tablet thoughts

Saw speculation about a forthcoming Google Tablet to try and fit somewhere between the Kindle Fire and IPad.

I would not be surprised if they went with a device offering A slightly better screen than Kindle Fire, passable performance, along with expansion (front/rear camera, sd slot, BT, external volume control, hdmi) etc, while keeping the price near $199US. Those are features not on IPad or KF(v1).

If the price point is low enough to achieve ubiquity, THAT is the market entry to want. Get users addicted to ubiquitous full color tablet presence, then up-sell future versions that wouldn’t be able compete on their own with an IPad but have 1: Google ubiquity, 2: foundation of existing user account/apps/familiarity but with better screen, faster proc, 4G service, longer battery, etc.

Google doesn’t have it’s on compelling content to offer like Amazon or Apples’s cult following and chic appeal. They have “open” (no vendor device or platform lock-in), Android marketplace and flexibility (customization, etc). They need “duh, yes get that” appeal.

Citrix Desktop – Fix for when Tab and Esc keys quit working

I work in a Citrix Desktop session all day, and once in a while, the Tab & Escape keys quit working in that session. No Alt-Tab, Ctrl-Tab, Tab, Esc, etc. Really annoying when you’re programming. They work fine on my local desktop, but not in Citrix.

When it happened today I started testing various settings – re-sizing Citrix sessions & windows, etc. I opened my local “Citrix Connection Center” and disconnected that session, reconnected, still no luck.

Then I noticed that inside that desktop session, I had another Citrix Connection Center running – because some of our applications are only available as Citrix instances, and only when using a Citrix Desktop to reach them. I used that app yesterday and it never closes correctly, and Tab & Esc had worked since… but suddenly stopped working today.

So I opened that Connection Center and terminated & logged off from the remote servers and apps there. Tab and Esc started working in my session again.

Not sure this is concrete, repeatable proof of a fix, but wanted to document it.