Safari contextual menu for applying Drupal patches

I wanted an easy way to test patches, so here's what I did.

Downloaded OnMyCommand.

Copied the OMCEdit application folder to Applications.

Ran the script Install OnMyCommandCM (I checked it out in Script Editor first.)

Logged out and logged back in.

Followed this tutorial.

I created a contextual menu item named Apply patch to Drupal installation with the following code:

#!/bin/bash
DRUPAL_HOME=/Library/WebServer/Documents/
cd $DRUPAL_HOME
touch drupaltemp
rm -R drupaltemp
cp -R drupal drupaltemp
cd drupaltemp
osascript -e 'tell Application "Safari"' -e 'get source of document 1' -e 'end tell' | perl -0ne 'print "$1\n" while (/a href="(http:\/\/drupal.org\/files.*?)">.*?<\/a>/igs)' | tail -1 | sed "s/\(.*\)/--url \1/" | curl --config - > patch
bbedit patch
patch -p0 < patch

Now when I right-click on an issue and choose Apply patch to Drupal installation, the script:

  • Creates a working copy of Drupal.
  • Obtains the source of the current page from Safari.
  • Parses out URLs that designate patches (begin with drupal.org/files).
  • Find the last one, since there may be multiple patches in the issue.
  • Download the patch.
  • Open the patch in BBEdit.
  • Apply the patch to the working copy of Drupal.

Note: this is actually a workaround. I wanted to modify Safari's contextual menu for links, but was unable to find a way to hook in.

Sadly, this will probably not increase the number of patches I review, as it's not really the patching that takes time. This was more of an, I can't sleep...I wonder if I could create a contextual menu that... kind of a project.