À la bonne Sainte-Force

Tools

Tags

A nice Porkcuts file for your home folder

27 September 2012

Dear friend,

There's something (not an octagon) I totally forgot to bring into yesterday's Porkcuts announcement. I'd like to share a nice (and useful) Porkcuts file with you. It demonstrates some of Porkcuts' capabilities: a way to access, add and remove favorite files right away, and some shortcuts to get new Finder, Terminal and Safari windows corresponding with your current file.

To install it, just go to your home directory with Finder, invoke Porkcuts, select Edit… and you'll be teleported to your favorite text editor, with a new Porkcuts file filled with example code. That's the Porkcuts for your home directory (and everything inside). Just clear everything in the file and replace it with this code :

"If this is not the topmost porkcut loaded, add a separator."
(Pork cuts menu numberOfItems > 0) ifTrue: [
	Pork cuts menu addItem:(NSMenuItem separatorItem).
].

"Create the current directory if it doesn't exist"
currentProjectsPath := sys homeDirectory stringByAppendingPathComponent:'.porkcutsfavorites'.
(NSFileManager defaultManager fileExistsAtPath:currentProjectsPath) ifFalse:[
	NSFileManager defaultManager createDirectoryAtPath:currentProjectsPath withIntermediateDirectories:YES attributes:nil error:nil.
].

"List ~/Current"
currents := NSFileManager defaultManager contentsOfDirectoryAtPath:currentProjectsPath error:nil.
currents do:[ :e |
	"Menu to access it"
	item := NSMenuItem alloc init.
	item setTitle:e.
	item setAction:#value:.
	item setTarget:[ :sender |
		NSWorkspace sharedWorkspace openFile:(currentProjectsPath stringByAppendingPathComponent:e)
	].
	Pork cuts menu addItem:item.
	"Menu to delete it"
	item := NSMenuItem alloc init.
	item setTitle:'- ' ++ e.
	item setAction:#value:.
	item setTarget:[ :sender |
		"Remove the alias from currentsPath"
		NSWorkspace sharedWorkspace performFileOperation:NSWorkspaceRecycleOperation source:currentProjectsPath destination:'' files:{e} tag:nil.
	].
	item setKeyEquivalentModifierMask:NSAlternateKeyMask.
	item setAlternate:YES.
	Pork cuts menu addItem:item.
].

ourURL := Pork cuts invokedURL.
((ourURL isEqual:(Pork cuts currentDirectoryURL)) | (currents containsObject:(ourURL path lastPathComponent))) ifFalse:[
	ourPath := ourURL path.
	ourName := ourPath lastPathComponent.
	ourDirInCurrentsPath := (currentProjectsPath stringByAppendingPathComponent:ourName).
	item := NSMenuItem alloc init.
	item setTitle:('+ ' ++ ourName).
	item setTarget:[ :sender | 
		"Create an alias in currentsPath"
		bookmarkData := ourURL bookmarkDataWithOptions:NSURLBookmarkCreationSuitableForBookmarkFile includingResourceValuesForKeys:nil relativeToURL:nil error:nil.
		(bookmarkData ~~ nil) ifTrue:[
			NSURL writeBookmarkData:bookmarkData toURL:(NSURL fileURLWithPath:ourDirInCurrentsPath) options:NSURLBookmarkCreationSuitableForBookmarkFile error:nil.
		].
	].
	item setKeyEquivalent:'D'.
	item setAction:#value:.
	Pork cuts menu addItem:item.
].

(Pork cuts menu numberOfItems > 0) ifTrue: [
	Pork cuts menu addItem:(NSMenuItem separatorItem).
].

"------------------------------------------"

thisDirectory := ((Pork cuts invokedDirectoryURL) path) copy.
item := NSMenuItem alloc init.
item setTitle:'New Safari window…'.
item setKeyEquivalent:'s'.
item setKeyEquivalentModifierMask:0.
item setTarget:[ :sender | 
	src := NSAppleScript alloc initWithSource:'tell application "Safari" to make new document'.
	src executeAndReturnError:nil.
	(((NSRunningApplication runningApplicationsWithBundleIdentifier:'com.apple.Safari') objectAtIndex:0) activateWithOptions:NSApplicationActivateIgnoringOtherApps). 
	].
item setAction:#value:.
Pork cuts menu addItem:item.

thisDirectory := ((Pork cuts invokedDirectoryURL) path) copy.
item := NSMenuItem alloc init.
item setTitle:'Finder here…'.
item setKeyEquivalent:'f'.
item setKeyEquivalentModifierMask:0.
item setTarget:[ :sender | 
	NSWorkspace sharedWorkspace openFile:thisDirectory withApplication:'Finder'.
	].
item setAction:#value:.
Pork cuts menu addItem:item.


"Terminal here"
thisDirectory := ((Pork cuts invokedDirectoryURL) path) copy.
item := NSMenuItem alloc init.
item setTitle:'Terminal here…'.
item setKeyEquivalent:'t'.
item setKeyEquivalentModifierMask:0.
item setTarget:[ :sender | 
	NSWorkspace sharedWorkspace openFile:thisDirectory withApplication:'Terminal'.
	].
item setAction:#value:.
Pork cuts menu addItem:item.
The Porkcuts.fscript in your home directory

And… voilà !

Looking forward to hearing from you,
Hubert Sainte-Force