Sniffen Packets

With a name like Sniffen, it's got to smell good.

Emacsclient AppleScript

I like Emacs. Maybe you like Emacs. I almost always have Emacs running on my Mac. I’d like to use Emacs as a general text editor. My Mac expects to use the open protocol handler, and lots of Mac programs expect to be able to use open or AppleEvents generally to ask for a file to be edited.

But how to connect that to emacsclient -n? Well, Brian McCallister’s blog has a proposed way. It works well enough to work with Marked, at least. Build the following as an AppleScript application called Emacsclient, drop it in /Applications/, and away you go.


on open of finderObjects
	tell application "Terminal"
		try
			-- we look for <= 2 because Emacs --daemon seems to always have an entry in visibile-frame-list even if there isn't
			set frameVisible to do shell script "/usr/local/bin/emacsclient -e '(<= 2 (length (visible-frame-list)))'"
			if frameVisible is not "t" then
				repeat with i in (finderObjects)
					-- there is a not a visible frame, launch one
					set p to POSIX path of i
					do shell script "/usr/local/bin/emacsclient -n " & quoted form of p
				end repeat
			end if
		on error
			-- daemon is not running, start the daemon and open a frame
			do shell script "/Applications/Emacs.app/Contents/MacOS/Emacs.sh --daemon"
			repeat with i in (finderObjects)
				set p to POSIX path of i
				do shell script "/usr/local/bin/emacsclient -n " & quoted form of p
			end repeat
		end try
	end tell
end open

-- bring the visible frame to the front
tell application "Emacs" to activate

Having now built this, Nicholas Kirchner seems to have something similar—his is also careful to use the quoted form of POSIX path.