À la bonne Sainte-Force

Tools

Tags

Tag-based Porkcuts

4 October 2012

Dear friend,

Today, one of our new Sainte-Force apprentices tried out Porkcuts, and he seemed to like it. What we wanted, however, was a little different context sensitivity: Porkcuts works with the filesystem hierarchy, whereas it could be based on tags, or recognizing certain patterns in the current file path (with regular expressions). Actually, if we run at least Lion, we can have both.

Just paste this to a Porkcuts file, for instance in your home directory:

tagBasedPorkcuts := #{
	'images' -> [
		"Porkcuts for images"
		item := NSMenuItem alloc init.
		item setTitle:'Do something to this file tagged into images.'.
		item setKeyEquivalent:'b'.
		item setKeyEquivalentModifierMask:0.
		item setTarget:[ :sender | 
			Pork cuts beep
			].
		item setAction:#value:.
		Pork cuts menu addItem:item.
		],
	'\.png$' -> [
		"Porkcuts for images"
		item := NSMenuItem alloc init.
		item setTitle:'Do something to this PNG file'.
		item setKeyEquivalent:'p'.
		item setKeyEquivalentModifierMask:0.
		item setTarget:[ :sender | 
			Pork cuts beep
			].
		item setAction:#value:.
		Pork cuts menu addItem:item.
		]
}.

invokedPath := ((Pork cuts invokedURL) path) copy.
[ :tagRegex |
	regex := NSRegularExpression regularExpressionWithPattern:tagRegex options:1 error:nil.
	((regex numberOfMatchesInString:invokedPath options:0 range:(NSValue rangeWithLocation:0 length:(invokedPath length))) > 0) ifTrue:[
		"A match has been found, execute the associated block"
		(tagBasedPorkcuts objectForKey:tagRegex) value.
	].
] value:@(tagBasedPorkcuts allKeys).
All we need to have tag-based porkcuts. Plus examples.

All you have to do to add new regex-based porkcuts is to modify the dictionary referenced by tagBasedPorkcuts. The key is a regular expression in a string, and the value is a code block that states what to do if the regex has been recognized. Thanks and praises to Florian for that very classy idea !

Looking forward to hearing from you,
Hubert Sainte-Force