Creating Unique ID's For… Anything!

There are numerous reasons you might need a unique identifier on something; you upload files with common names to the same ftp site, you use the id attribute for internal links on a blog that might display multiple posts (the chances of repeating yourself are good), you want to sign your emails with a unique, one time id for your own verification… Whatever the case, a unique id could come in handy.

The easiest thing to do is use a timestamp and a shell script can do that easily enough for us. For everyday use, the year, month, day, hour, minutes and seconds should suffice (unless you need multiple unique id’s per second). Here is how the bash script would look:

date +%y%m%d%H%M%S

Entering this string into the Terminal.app would yield a twelve digit number like this:

091027191546

The first pair of digits is the year, the second pair is the month, the third pair is the day, then the hour, minutes and seconds (all in pairs). Run this script in Terminal.app as many times as you like and you will never get the same number.

So you could enter this into your terminal every time you wanted a unique id… or you could make this shell script work *for* you from anywhere.

## TextMate Snippet ##

TextMate Snippet for Unique ID TextMate makes running shell scripts dead easy — all you need to do is create your own snippet. To do this:

* open your Bundle editor — `Bundles > Bundle Editor > Show Bundle Editor` ***or*** `⌃⌥⌘B`
* create a new bundle (if you don’t already have your own) from the + icon
* within that bundle create a new snippet from the + icon
* in the edit window, type:

`date +%y%m%d%H%M%S`
* define a Tab Trigger or Key Equivalent
* leave the scope selector blank to have the snippet apply to all language types

Now you have a snippet that is accessible in any language scope you use TextMate for. This is handy if you write reference links in HTML articles, such as:

See this note

This is the special note I wanted you to see.

Or for reference style links in MarkDown:

Check out our site, [seyDoggy][a_091027194631], for some
really cool tips and tricks.

[a_091027194631]: http://www.seydoggy.com/ “seyDoggy’s really cool site”

### Download ###

Download your fresh copy of “[Unique ID.tmSnippet][a_091027205118]” from seyDoggy.com.

## AppleScript to clipboard ##

UniqueID AppleScript for creating unique id'sSay you’re not a TextMate user but you still want a quick way to run this shell script with as little effort as possible. AppleScript makes quick work of this task with it’s `do shell script` functionality. We need to make an AppleScript that runs the shell script, then copies the results to the clipboard. Then we can paste that result into what ever we’re working with at the time; file renaming, email signing, etc…

Here is how the script looks:

set uniqueID to (do shell script “date +%y%m%d%H%M%S”)
set the clipboard to uniqueID
display alert “”” & uniqueID & “”” & “Has been copied to your ¬
clipboard” giving up after 1

### Download ###

Get your fresh copy of “[UniqueID.scpt][a_091027205534]” from CodeCollector.net

[tags]applescript,shell,script,textmate,snippet,terminal,unique id[/tags]

[a_091027205118]: http://www.seydoggy.com/downloads/UniqueID.tmSnippet.zip “Download from seyDoggy.com”
[a_091027205534]: http://www.codecollector.net/view/55260B3D-599D-489A-BC7E-C51AF597B2D0 “Download from CodeCollector.net”

 

Adam Merrifield

 

One thought on “Creating Unique ID's For… Anything!

Leave a Reply