À la bonne Sainte-Force

Tools

Tags

Create new files with Porkcuts

6 October 2012

Dear friend,

This quick Porkcuts will allow you to get roughly the same behavior as the Finder's New Folder, but for creating new (empty) files, and in every application. As usual, put it where you want, but your home folder is a nice place.

thisDirectory := ((Pork cuts invokedDirectoryURL) path) copy.
newFilePath := (thisDirectory stringByAppendingPathComponent:'New file') copy.
item := NSMenuItem alloc init.
item setTitle:'New file in enclosing folder'.
item setKeyEquivalent:'='.
item setKeyEquivalentModifierMask:0.
item setTarget:[ :sender |
	'' writeToFile:newFilePath atomically:YES encoding:NSUnicodeStringEncoding error:nil.
	NSWorkspace sharedWorkspace selectFile:newFilePath inFileViewerRootedAtPath:thisDirectory.
	script := (NSAppleScript alloc initWithSource:'tell application "System Events"\ndelay 0.2\nkey code 36\nend tell') autorelease.
	script executeAndReturnError:nil.
	].
item setAction:#value:.
Pork cuts menu addItem:item.
Create a new file in the enclosing folder.

Looking forward to hearing from you,
Hubert Sainte-Force