Home > Tags > mac

mac

Write a Quicksilver plugin with RubyCocoa

I wrote a Quicksilver plugin with RubyCocoa, that adds “hello world” to the passed string.

  • Xcode project codes : in CodeRepos
  • License : revised BSD

It should work on Tiger/Leopard if RubyCocoa is installed.

How does it work

As I mentioned in QuartzComposer CustomPatch with RubyCocoa, we just call RBBundleInit() function in plugin initializaiton phase to write some plugin with RubyCocoa. But wait, where should be the initializaiton code in Quicksilver plugin ?

We write actual plugin behavior in the class that inherits QSActionProvider, so I tried to call RBBundleInit() in the init() method in that class… however, that resulted in crashing Quicksilver :(

Then I called RBBundleInit() at only first time in performActionOnObject() of the actual Action class, and made references between Objective-C and Ruby class instances.

After that, I implemented an actual action behavior in Ruby class, and delegates from Objective-C to Ruby method, thats’ all. This is ugly, confusing, … I know, but it does work well :)

Code Snippets

ActionProvider in Objective-C:

 (QSObject *)performActionOnObject:(QSObject *)dObject{
  // initialize RubyCocoa
  static bool loaded = false;
  if (!loaded) {
    if (RBBundleInit("qs_action.rb", [self class], self)) {
      NSLog(@"[RubyCocoaPluginAction.performActionOnObject] RBBundleInit failed"
);
      abort();
    }
    loaded = true;
  }
 
  // delegate actual action to Ruby class
  QSObject *ret = [QSObject objectWithString:[rb_ act:dObject]];
  return ret;
}

RubyCocoa side:

class Action
  def initialize(logger)
    @logger = logger
  end
 
  # write something great :)
  #  - arg : QSObject
  def act(arg)
    val = arg.stringValue
    @logger.info(val)
    'Hello world, ' + val
  end
end # Action
 
require 'osx/cocoa'
OSX.init_for_bundle do |bdl, owner, log|
  # bdl    - the bundle related with the 2nd argument of RBBundleInit
  # owner  - the 3rd argument of RBBundleInit as optional data
  # log    - logger for this block
 
  act = Action.new(log)
  owner.setInstance act
end

Future works

  • Better initializaiton. It should be in constractor of some class)
  • Inherits QSActionProvider by Ruby class (more pure Ruby)
  • Use ns_import ?

It should not be “With RubyCocoa”, but rather “By RubyCocoa”.

Conclusion

I made a start point to write a Quicksilver plugin by RubyCocoa. It is timing of initialization that is to be considered about writing some bundle in RubyCocoa. I made it in this article somehow. This article is for someone who wants to create Quicksilver plugin by Ruby, not learning unfamiliar Objective-C.

Reference

Quicksilver Twitter Plugin

I wrote a small Quicksilver plugin to send messages to Twitter directly.

Advantages

  • Easy to install. No additional software required to be installed
  • Configurable via Quicksilver Preference
  • You can send messages containing non-ASCII characters

Download

QSTwitter.zip (1.2) (2008.01.23)

Environment

Tested under Leopard 10.5.1, Quicksilver B53 3814.

Install

Unzip the downloaded file and double-click it. that’s all !

Configuration

You will find “Twitter option” in the Quicksilver preference pane. Enter your screen name and password there.

Usage

  1. Activate Quicksilver (by Ctrl-SPACE or so)
  2. type . (period/dot) key to enter text input mode
  3. type a message
  4. hit TAB to move into Action
  5. type “Twitter”
  6. return !

The more familar you are with it, the faster you can send messages than ever.

Screencast

Seeing is Believing, as you know :) I don’t know why the video is collapsing for first 15 seconds. Sorry for inconvinience.

Code

You can see the code in CodeRepos.

License

Considering…

Restriction

  • I could not post messages throught HTTP proxy. Let me know if anyone succeeded to make it.

ChangeLog

  • 1.2 (2008.01.23) : fixes the bug that ‘+’ is not shown in status, uses pretty girl icon .
  • 1.1 (2007.12.22) : now it shows “QSTwitter” in your post on “from …” .
  • 1.0 (2007.12.13) : initial release.

There seems no comprehensive documents to develop Quicksilver plugin as far as I searched. In order to share my experience to avoid falling into pitfalls, I will write an tutorial developing a Quicksilver plugin under up-to-date environment later.

SocketReaderPatch

SocketReaderPatch

What is SocketReaderPatch ?

A Coustom Quartz Composer Patch that recieves strings from TCP Socket.

Download

Install

Copy SocketReaderPatch.plugin into /Library/Graphics/Patch .

How to use

Open QuartzComposer, find SocketReader from the Patch Library list (type “Socket” in Search in Libraries field to search it) .

Then drag SocketReader into right pane, and play it. SocketReader listens port number 12345 in default. Make some client application and write UTF-8 strings into the socket, and Quartz Composer will show that strings.

Example

Simple Example

ScreenCast !

Note

  • Very buggy. If two or more sockets are connected, CPU usage will be so high.
  • To change the port number, edit port_ variable in SocketReaderPatch.m.

Reference

I used Quartz Composer Custom Patch Xcode template from fdiv.net:Xcode Template for Custom Quartz Composer Patches. (Thanks!)

In order to play with socket in Cocoa, O’Reilly Network::mac devcenter::Networking in Cocoa was pretty helpful.

Motivation

I came up to an idea that IRC messages flow beautifully by using QuartzComposer, in RubyKaigi2007 . To push dynamic text into QuartzComposer, it seemed to be only RSS feed out there.

License

CC : Attribution-NonCommercial-ShareAlike, because above Xcode template claims that license.

Contact

Any feedbacks are welcome, mail me.

Home > Tags > mac

Feeds

Return to page top