Archive for the ‘Scripts’ Category

Installing the acts_as_taggable Rails Plugin

Thursday, December 20th, 2007

You don’t have to write a lot of code to get tagging support in your Ruby on Rails application - you just have to install a plugin. This is how you install the ‘acts_as_taggable’ plugin…

First go to the Ruby on Rails application folder and open a terminal at that location. Now run this commad…

ruby script/plugin install acts_as_taggable

If your have previously installed some plugins from the same repository, that command will work. If not, you will get this error…

Plugin not found: ["acts_as_taggable"]

This is because the ‘acts_as_taggable’ plugin is not in any of the repositories you check. To see the all the repositories on your check list, run this command…

ruby script/plugin list #Shows the list of all the repositories you check.

To add new repositories to your list, you have to run this command…

ruby script/plugin discover

This will shows a list of repositories - just press ‘y’ to select all the repositories you need. I added every repository in the list.

Now run the first command again…

ruby script/plugin install acts_as_taggable

Installing the Gem

The above instructions are for installing the acts_as_taggable plugin - not the acts_as_taggable gem. To install the gem run the command…

gem install acts_as_taggable

Related Links

Creating PDF in Ruby on Rails - PDF::Writer

Monday, November 12th, 2007

PDF::Writer is my choice for creating PDF files in Ruby on Rails. Its simple, easy to use, and has all the features I am looking for.

Install PDF::Writer in Linux systems using this command…

gem install pdf-writer -y

Includes

We will require rubygems and pdf/writer.

require "rubygems"
require "pdf/writer"

The next line creates a new instance of PDF::Writer

pdf = PDF::Writer.new

Create a Heading(big font size)

pdf.select_font "Times-Roman"
pdf.text "Sample PDF Document", :font_size => 32, :justification => :center

The text function will add a string of text to the document, starting at the current position. It will wrap to keep within the margins - so you can specify text as big blocks. The text will go to the start of the next line when a return code “\n” is found.

The other arguments available for this function are…

:font_size
The font size to be used. If not specified, is either the last font size or the default font size of 12 points. Setting this value changes the current font_size.
:left
Gap to leave from the left margin
:right
Gap to leave from the right margin
:absolute_left
Absolute left position (overrides :left)
:absolute_right
Absolute right position (overrides :right)
:justification
This can be :left, :right, :center, :full
:leading
This defines the total height taken by the line, independent of the font height.
:spacing
Line spacing - usually set to one of 1, 1.5, 2 (line spacing as used in word processing)

Writing Text

pdf.text "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aliquam sodales velit ac ante. Suspendisse felis mi, convallis at, semper id, malesuada eu, mauris. Integer orci. Sed consectetuer orci. In hac habitasse platea dictumst. Duis nec pede. Ut lacinia eros ut magna. Maecenas lectus dui, lacinia vel, porttitor a, fringilla nec, turpis. Nulla odio nisi, mattis ac, porttitor at, malesuada ac, nibh. Nam suscipit mi ut justo. Phasellus aliquam lorem non velit ornare bibendum. Nullam mollis. Ut elementum rutrum justo. Pellentesque ac sapien. In facilisis lorem a enim. Curabitur vitae felis. In eget tellus nec ligula egestas semper. Nulla facilisis urna nec magna. Pellentesque fringilla pulvinar risus. Aliquam rutrum, nisi ut lobortis consequat, nisl felis posuere risus, at sollicitudin nibh dui et felis.

Fusce tristique dapibus neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Ut felis mi, dictum ut, vehicula non, fermentum quis, elit. Quisque ultricies purus quis enim. Integer turpis elit, porttitor quis, volutpat consequat, interdum vitae, quam. Donec tempus, dolor eget bibendum euismod, metus dolor imperdiet purus, vitae nonummy est mi non orci. Aenean eu massa. Fusce euismod. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Nullam quis massa id sem commodo eleifend. Cras eu velit rutrum leo egestas adipiscing. Nulla volutpat, lectus sit amet sagittis gravida, erat tortor condimentum sem, vitae sollicitudin lectus diam eget felis. Aliquam augue. Vestibulum viverra est. Fusce tellus ligula, euismod sed, placerat vel, cursus dictum, erat.", :font_size => 12

I used the text() - but this time with 12 as the font_size. The long text will wrap around and the \n will be converted to line breaks.

Inserting Images

PDF::Writer provides an easy way to insert images…

pdf.image "default.png"

That’s it - it will insert an image called ‘default.png’. The other option are…

:pad
The number of PDF userspace units that will be on all sides of the image. The default is 5 units.
:width
The desired width of the image. The image will be resized to this width with the aspect ratio kept. If unspecified, the image’s natural width will be used.
:resize
How to resize the image, either :width (resizes the image to be as wide as the margins) or :full (resizes the image to be as large as possible). May be a numeric value, used as a multiplier for the image size (e.g., 0.5 will shrink the image to half-sized). If this and :width are unspecified, the image’s natural size will be used. Mutually exclusive with the :width option.
:justification
The placement of the image. May be :center, :right, or :left. Defaults to :left.
:border
The border options. No default border. If specified, must be either true, which uses the default border, or a Hash.

Tables

Inserting tables are slightly more complicated than the last two items. If you are using SimpleTable, you have to add
require "pdf/simpletable"
along with the other includes.

table = PDF::SimpleTable.new
table.title = "Sample Tables"
table.column_order.push(*%w(first second))

table.columns["first"] = PDF::SimpleTable::Column.new(”first”)
table.columns["first"].heading = “First”

table.columns["second"] = PDF::SimpleTable::Column.new(”second”)
table.columns["second"].heading = “Second”

table.show_lines    = :all
table.show_headings = true
table.orientation   = :center
table.position      = :center

data = [
	{"first"=> "1", "second"=> "2"}, # First row
	{"first"=> "One", "second"=> "Two"}, # Second row
	{"first"=> "Mono", "second"=> "Di"}, # Third row
]

table.data.replace data
table.render_on(pdf)

The above code will create a table with two column and three rows of data. The code should be easy to decipher - so I am not going into any more explanation.

Saving the PDF

Finally, saving the created PDF…

pdf.save_as("report.pdf") 

Links

Tk Verses Gtk(And Python verses Tcl)

Wednesday, October 31st, 2007

Back when I was using Windows, I searched for some language that would let me create GUI application. I knew C++ - but creating a GUI using C++ was very, very hard. After some searching I found Tcl/Tk - it was perfect. I liked Tk so much that I used it to create GUI in perl programs. And in Ruby. And in Python. I even wrote tutorials for Tcl/Tk and Perl/Tk. Then I discovered Linux. initially I was glad that I did not choose VB - that would mean that I have to throw away all my custom programs. But since I used open languages like Tcl, Perl, Ruby etc, they will run on Linux as well.

But when I actually ran the programs in Linux, I got the shock of a lifetime. The applications I created looked bad - really bad…

TK Screenshot - Ugly

I still use many Tk programs - but because of the looks issue, I decided to stop using Tk for my new programs. So I decided to try GTK. I even created a small application using PyGTK(my first) to view the harddisk space usage in Linux - Frees.

Frees Screenshot

It was a very simple application - all I had to do is run the ‘df’ command, parse its output, and display a small table using that data. I was expecting around 100, 150 lines of code at the most. But after I created the app, I have 500+ lines of code.

At first, I thought that it was a mistake on my part - I thought that I was not using the best method. But then I came across an article in Reddit - gnocl or PyGtk?.

PyGtk is still the most recommended binding for Gtk. A lot of programs in Linux is written using PyGtk. So naturally there are a few advantages for using it…

  • It will be the more ‘road-tested’ binding.
  • Many system will have it installed by default.
  • Bigger community - so…
    • more support
    • more examples
    • more tutorials
    • etc.

One the other hand, I have to write more code.

Conclusion

If it is a private script - only useful for myself, then I will use Tcl/Tk or Gnometcl. If the script could be distributed, like Frees, then I will use Python GTK.

What about the other options…

  • wxWidgets
  • PyQT
  • Ruby/GTK
  • Perl/GTK

Any recommendations for me on which library to use? Please leave it in the comments.

Article Templates - WordPress Plugin

Thursday, October 4th, 2007

Article Templates wordpress plugin lets you create templates that can be used when writing a new post or page. This will be very useful if you are writing many post with the same structure.

Download Article Templates 1.00.A Beta

Article Template Screenshot

This is my first WordPress Plugin. It was easier to create than I expected.

My plugin used the principles used in Post Templates plugin. It is another plugin to do the same thing - create templates for the post.

My plugin have a few advantages over the other plugin…

  • Easier to use
  • The templates created by each user is accessable only to them

There are a few disadvantages too…

  • Javascript must be enabled for my plugin to work
  • Beta release - expect bugs

Nexty, The Online Application

Wednesday, September 26th, 2007

Nexty Logo

I just released the latest version of Nexty - and to celebrate that, I am making Nexty an online application. That is, you can use Nexty from the web - without having to install it on your local system.

Of course, Nexty code is available as well - you can download and install it locally if you wish. In fact, I will recommend that you install it locally. That way, the application will be much faster than the online alternatives.

This happens to be my first online app. I have worked on many online projects before - but those were for various clients. This is the first online application that I have created for the use of others! I am working on anther online application - but by the look of things, it is not going to be over soon.

And thanks to Brad Nickel for suggesting this idea.

Finally! Nexty 1.01.A Released

Saturday, September 22nd, 2007

I have released Nexty 1.01.A. If you are a user of Nexty, I will recommend that you switch to the new version.

Changelog

Search
Search for tasks, projects and reminders
Next Action
Dedicated page for the next action - and only that
Inbox Feature
Add a lot of tasks in one go using the inbox
Linux Command Line
A script to control Nexty from the Linux command line.
Code uses iFrame
iFrame is my PHP framework
Bug Fixs
Too many to list
Better Installer
Will not overwrite the old data
Printing Support
Well, its better than before

To Learn more…

Nexty Updates

Wednesday, August 22nd, 2007

Nexty Logo

I made some more updates to nexty. Still not packaged - give me a little more time. In the meantime, try out the demo

There is one really cool feature in this release - but it is for Linux Users only. I made a command line script that integrates with Nexty - you can add tasks directly from the console. It needs the curl command - so not available in windows.

But then again, who uses the terminal in windows?

Anyway this uses the concept I introduced in CLI Twitter. If you want to try it, login to Nexty and go to the settings page. If you are on a linux system, you will see a Download Nexty Shell Script section. Just follow the instructions there.

Demo for Nexty 1.01.A Uploaded

Thursday, August 16th, 2007

Nexty Logo

I uploaded the demo for the new beta version of Nexty today. The code is too ‘beta’ to be packaged and released - so I am just uploaded the demo. If the bugs are found and fixed soon, I will release the code into the wild. If you cannot wait for the code, it is available at the SVN repository.

Whats New…

A few new features are…

  • Search
  • Next Action
  • Inbox Feature
  • Code uses IFrame
  • And more…

Please try out the demo and let me know if you spot any errors. That is the whole point of uploaded the demo.

Nexty 1.01.A Demo

Choosing a License

Friday, August 10th, 2007

License

As you may know, I publish a lot of code for others to use. Often I forget to specify a license for the code - and I get a few comment asking me what license it is under. So I have created a license page for both Bin-Co and OpenJS. I hate restricting use of my code - so I choose the most permissive one around - the BSD License.

I know that many people feel strongly about software license. I still get a lot of flank from my LUG because I prefer using BSD License over their favorite GPL.

GPL was not an option as most of my code are snippets that are meant to be used with other code. GPL will not allow my code to be used within proprietary projects. Another option is LGPL - but I still prefer BSD.

Basically, all scripts, codes and programs from any of my sites are in the BSD License. That means that you can do anything with the scripts - except claim that you wrote it. You can use, copy, sell, modify or whatever you want with it - but you cannot claim authorship.

Importance of Choosing a License

If you are planning to release your code publicly, it is very important that you say what license you are using. If you fail to specify a license, many people will not use your code. It was a big mistake waiting this long before putting up a license on my sites.

If you want help in deciding the license, these links should be of help…

More Links

Nexty 2 will use PHP 5

Saturday, August 4th, 2007

GoPHP5

The next version of nexty is coming out very soon - and unlike the last version, Nexty 2 will require PHP 5.

The main reason behind this change is GoPHP5. Go PHP 5 is a movement to promote the use of PHP5 in hosting servers and for projects.

Another reason for this decision is that iFrame, the framework I used in Nexty, uses PHP 5.

If you are stuck with PHP 4, I cannot help it. You can still use the first version. But if you have a choice, PHP 4 is dead - move on.

More on GoPHP5

Subscribe to Feed