Tag Archives: tools

Ruby Line Formatting

I wrote a little utility to turn something like this

this=that,
something_else="something else entirely"

Into the much easier to read

this           = that,
something_else = "something else entirely"

I use it in conjunction with my clipboard utility so I can just copy, run, paste. I mostly use this when I’m working with ColdFusion, so it also works for those pesky cfsets.

Usage: (After copying your target text to your clipboard)

ruby clipboard_format_set.rb

Download It!

PS: I’d love to set some of my scripts up as Eclipse shortcuts so I’ve been toying with the idea of writing an Eclipse plug-in to act as a proxy. I realize you can set up and run “External Tools”, but I have yet to see a way to bind a key. I’d like the plug-in to take care of the “pasting” to save me that extra ctrl-v. Maybe this would make a good New Years Project?

Ruby Clipboard Directory List Utility

I wrote this little script to copy the filenames of a directory into the clipboard a while back and forgot to post about it. I’ve been particularly surprised about how often it’s come in handy for this or that.

 require 'clipboard'

c = Clipboard.new

list = ''

if ARGV.size == 0
	path =  "."
else
	path = ARGV[0]
end

Dir.foreach(path) {
	|file|
	if(file.length > 2)
		list << file << "n"
	end
}

c.set_data list

To use it, simply call run the script with the directory you want copied to your clipboard as an argument!

Download the code!

ColdFusion MySQL Search Utility

There’s a legacy app I work on that currently requires adding 4 columns and a couple rows (in different tables) every time you add a row to another table. It doesn’t happen often and it would be a nightmare to refactor so I wrote little script that would search the database for any columns named ‘x’ as well as any fields with a value of ‘x’ and return an array of the offending tables.

I cleaned it up a little bit and cfc-ized it in case I ever have to do anything similar. I haven’t tested it at all aside from the one time I ran it today so use it at your own risk. It’s really simple to use, you just need to pass in the ColdFusion datasource name and the search term.

Example Usage:



Looking for column #search_term#



Looking for value  #search_term#

Download ColdFusion MySQL Search Utility!

PS: it won’t search numeric columns if you aren’t searching for a number.

Ruby Resize Utility

I wanted to demo an ad rotator the other day at work and I wanted to load up a bunch of images of various sizes to make it look legit. It would have taken forever to do it ad by ad, so I wrote a little ruby script to automatically resize all the images in a directory. Then I used my directory listing utility to copy the files into my clipboard. Pasted into the database from there!

(Requires GD2)

Usage:

 r = Resizer.new input_path, output_path, width, height
 r.crop_files

Download it!

JavaScript List Utility

Here’s a little something I was working on today. The goal was to model an HTML unordered list in JavaScript so I could easily add/remove and display elements. I haven’t gotten around to actually writing all of the display methods because I haven’t exactly figured out how I want to use it yet.

PS: I kinda re-remembered the toString method on this one.

PPS: Requires prototype

Example Usage:

set up the list

list1 = new List();
list1.add('a');
list1.add('b');

list2 = new List();
list2.add('b1!');
list2.add('b2!');

// nest away!
list1.add(list2);
list1.add('c');
list1.add('d');

li data

alert(list1.get_at(1).data);

remove li

list1.remove(4);

alert the list

alert(list1);

BAM!

Download the code!

Simple JavaScript Slider/Iterator…Thing

In keeping with my current “blog-more-about-less” stratagem I thought I’d post a little JavaScript utility I wrote. The idea was just to add “next” and “previous” buttons to an existing gallery, but I thought I’d have a little fun with it, as it may come in handy in the future.

The idea was to create a simple slider object that would keep track of the users position in an array of items. I could cycle through the items, backwards and forwards and fire of item methods. For the gallery I only had to worry about a few basic properties and methods, but I wanted to design this in such a way that adding properties and functionality to my objects would be a breeze.

PS: Current the slider object expects your items contain the methods “update” and “show”. I *know* this is bad, but I’ve had a hard time coming up with a solution that I liked better.

PPS: The example code I’m posting below was done using Prototype and ColdFusion, neither of which are required but are there because I thought it made it easier to get the point across.

Alright, enough gibber jabber.

Image Object:

function Image(thumb,large,caption) {
      this.thumb = thumb;
      this.large = large;
      this.caption = caption;
      this.show = function() {
          showPhoto(this.large,this.caption);
      }
      this.update = function() {
          $('largeImg').src = this.large;
          $('photoCaption').update(this.caption);
      }
  }

Create and fill it up!

gallery = new Slider();
// don't forget to escape those special javaScript characters!!!!

      gallery.add(
          new Image('#thumb#','#large#','#caption#')
      );

Attach the “previous” and “next” methods to my links. <3 unobtrusive JavaScript!

Event.observe(
      'previous',
      'click',
      function() {
          gallery.previous();
      }
  );
  Event.observe(
      'next',
      'click',
      function() {
          gallery.next();
      }
  );

And finally, on the actual thumbnails on the page:


      
      
            
      

Download the code!

As always, this solution is far from perfect, and I’d love feedback!

Ruby File Renaming Utility

I wrote a little renaming utility to help me with the all too common task of file re-naming that seems to keep coming up. The class itself is pretty general; the idea is to extend it and override the “valid_file” and “format” methods for whatever specific task I’m doing.

The “valid_file” method is intended to recognize which files need to be renamed in the case you’ve got files you don’t want to touch in the directory. By default the function just ignores the “.” and “..” in the directory listing.

The “format” method is used to to specify the actual rules and routine to use when renaming your file. By default it replaces spaces and dashes with underscores.

Finally, this isn’t “finished” software. No error checking, no notes. It works for me and maybe it’ll work for you too.

Here’s an example class I wrote using the renaming utility. It’ll prepend the string “prepend_” onto all png files in the specified directory. Note that there’s a “safe_mode” variable passed in the constructor. Setting the “safe_mode” to true will prevent the files from being renamed while you’re working on it.

require 'renamer'

safe_mode = false

class Example < Renamer

  def format file
    file = custom_format(super(file))
    puts file 
    return file
  end

  def valid_file file
    valid = super(file)
    valid = valid && file.include?('.png')
    return valid
  end

  def custom_format file
    return "prepend_#{file}"
  end
end

if __FILE__ == $0  
  path = ARGV[0]
  r = Example.new(path,safe_mode)
  r.rename_files
end

Download the code: Ruby File Renaming Utility