Hacking TextMate to Edit a Blog Post by ID

OK, so the blogging functionality in TextMate is pretty nice. Its a little primitive, but it does have some cool features and all the text editing features that it offers to boot. I wanted to go back and clean up some old entries using TM, just to play around with it a bit; however, I was a little disappointed that I could not see more than the last 20 entries when choosing the command from the menu "Bundles/Blogging/Fetch Post." So, since TM is very customizable, I set about fixing this issue, by creating the ability to fetch a post if you know the id of the post in your blogging engine. In order to do this, you must edit the blogging Ruby file in the blogging bundle (which is located in the TextMate.app bundle), and then add a command using the bundle editor. All this is assuming that you have been able to get the blogging functionality working at the base level in TextMate. So, let's get started - read on...

** Note (added July 31, 2006): After posting this and shutting down, it occurred to me that it would be better to actually add the edited ruby code to an external directory so that it would not be overwritten in the process of updating Textmate; in addition, if you migrate your user to another computer (which I'm about to do soon), Migration Assistant should copy the changes over to the new machine. I've changed the content of the tutorial to reflect this change.

Edit "blogging.rb" Ruby file

  1. Go to the TextMate application.
  2. Right-click (command click) the TextMate application icon and choose "Show Package Contents" -- you will probably have to authenticate.
  3. In the new window open the sub directory of the path "Contents/SharedSupport/Bundles" -- you will see a list of bundles.
  4. Right-click, as before, the bundle called "Blogging.tmbundle" and choose "Show Package Contents" again.
  5. In the new window for the Blogging bundle, go into the directory path "Support/lib" -- you should see some Ruby files, and one called "blogging.rb".
  6. Make a copy of "blogging.rb" and place it into a directory in your user folder. I placed it here: "file:///Users/--username--/Library/Application Support/TextMate/BundleEdits/Blogging/blogging.rb". (the name BundleEdits for the new folder in the TextMate support folder is arbitrary, but its what I chose...)
  7. Open the new "blogging.rb" with your text editor (TextMate is fine). (I chose not to use TextMate, just being superstitious.)
  8. Search for the string "# Command: Fetch" which is the commented line above the Fetch command for the blogging funtionality. Cursor to the line after the "fetch" function definition, and paste the code below:
  9. CODE:
    1. # fetch_post_page
    2.  
    3. def fetch_post_page
    4. begin
    5. _password = password
    6. self.post = client.getPost(post_id, username, _password)
    7. TextMate.exit_create_new_document(post_to_document)
    8. @mw_success = true
    9. rescue XMLRPC::FaultException => e
    10. TextMate.exit_show_tool_tip("Error retrieving post. Check your configuration and try again.")
    11. end
    12. end
    13.  
    14. # Command: Fetch_By_Id
    15.  
    16. def fetch_by_id
    17. if post_id
    18. fetch_post_page
    19. else
    20. TextMate.exit_show_tool_tip("A Post ID is required to fetch the post by id.")
    21. end
    22. end

  10. Now save the code and close out the document.

Create a new command in the TextMate Bundle Editor

  1. Open TextMate and choose menu item "Bundles", then "Bundle Editor", and finally "Edit Commands..." (you can also just use the key command of Control-Option-Command-C).
  2. Open the Blogging section on the left hand side of the Bundle Editor and select "Fetch Post" (but don't do anything with it)
  3. Now click the duplicate button at the bottom left (looks like two +'s) to create a new command. Rename the command something like "Fetch Post By Id"
  4. Paste the following text into the field named "Command(s):". Make sure that you substitute your system user name and if you did not create a directory called "BundleEdits", use the name you used there.
  5. #!/usr/bin/env ruby -rjcode -Ku
    require "Users/--username--/Library/Application Support/TextMate/BundleEdits/Blogging/blogging.rb"
    Blogging.new.fetch_by_id
  6. Leave the rest of it the same and close the Bundle Editor.

Open a Post

I have my settings as the default in WordPress, so that it's fairly easy to find out what the post id is for a particular posting. As the default, the post id is the number at the end of the URL string when you click the permanent link for a posting. Once you've gotten the post id for the post you would like to view/edit, open a new text document in TextMate and type in the text "Post: XX", where XX is your posting id.

Now choose the menu "Bundles/Blogging/Fetch Post By Id," and after authenticating, Textmate will open your post in a new window for editing...

3 Comments

  1. Posted August 10, 2006 at 10:05 am | Permalink

    Dude you’re killlin’ me! Ok, I need to play with TextMate more, cause I didn’t know it could do half the things you have been doing on here!

    Cheers!

  2. Posted January 28, 2007 at 12:20 pm | Permalink

    The latter comment from MMISoftware I think got garbled. Check out the link: http://www.mmisoftware.co.uk/weblog/?p=205 for some alterations to the methods described above that may work for you better.

    Indeed, I don’t doubt there are some better ways to go about this. In fact, one could probably write their own bundle, and use that (which I recommend at this point in my experience :)) rather than altering a built in bundle, which is likely to change in the process of updates.

    [r]

  3. Dimitris Diavatis
    Posted October 29, 2007 at 3:05 pm | Permalink

    This edited version seems to work. I know there might be some problem in it, with the “select blog” dialog appearing two times.
    But the version described here (a year ago) doesn’t work with an updated version of textmate
    please check :)


    SELECTEDID = ENV['TM_SELECTED_TEXT']

    def fetch_post_page
    current_password = self.password
    require “#{ENV['TM_SUPPORT_PATH']}/lib/progress.rb”
    result = nil
    TextMate.call_with_progress(:title => “Fetch Post By Id”, :message => “Contacting Server “#{@host}”…”) do
    begin
    result = self.client.getPost(SELECTEDID, self.username, current_password)
    rescue XMLRPC::FaultException => e
    TextMate.exit_show_tool_tip(”Error: #{e.faultString} (#{e.faultCode})”)
    end
    end
    @mw_success = true
    self.post = result
    TextMate.exit_create_new_document(post_to_document())
    end

    def fetch_by_id
    fetch_post_page
    end

One Trackback

  1. [...] I came across a great post over at plasticular inscriptor, which described how to get TextMate to retrieve blog posts by id. [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*