MACでもtextwranglerでconfファイルを編集しよう!

★ブログ移行のお知らせ
サイトを移行しました。以下をクリックください。
きよしの倶楽部 – 生活を潤わす情報を発信中。GTDを得意としてます。


ーーー

MACテキストエディターというと定番はtextwranglerだと思います。BBeditという上位有料エディターの無料版という位置づけでしょうか。

今回はapachehttpd.confといったターミナル場ではsudoを使わないと編集できないファイルをMACのfinderからtextwranglerを呼び出して編集するという小技をご紹介します。共有PC等でのセキュリティーまでは考慮してないので、その辺りは自己責任でお願いします。

使用するtextwranglerのバージョンは4.5.1になります。かつMac App Store Buildとなります。通常のダウンロード型の場合は何もしなくても編集できるはずです。


さて、まず通常、どのようなるかのおさらい。今回は例としてhttpd.confを編集します。普通にfinder等からhttpd.confを開くと

のような表示があります。要は編集権がないけど編集するの?と聞いてきている訳です。これは回避する方法もありますがそれはさておき、編集してみます。saveしようとすると

権限がないのでエラーが発生します。

これを回避するのはここを参考に以下を実行します。

  1. Authenticated Save Scriptをダウンロードする。
  2. 「/Users/USERNAME/Library/Application Support/TextWrangler/Attachment Scripts/」フォルダを作成(USERNAMEは各自のフォルダ名)。既に存在していれば作成は不要。
  3. 先ほどダウンロードした”document.documentShouldFinalizeAuthenticatedSave.scpt”を上記フォルダに設置

一応、Authenticated Save Scriptをここにも記載しておきます。

on documentShouldFinalizeAuthenticatedSave(theDocument, tempFilePath, destinationPath)
	
	-- on input: tempFilePath points to the contents of the document written to a temp file, ready to move to the destination; destinationPath is where the file should be copied.
	
	-- on exit: if the operation succeeded, delete the temp file (or else the application will assume the operation failed) and return YES for success
	
	--	this is pretty straightforward: "cp tmpFilePath destinationPath"
	
	do shell script "cp" & " " & quoted form of tempFilePath & " " & quoted form of destinationPath with administrator privileges
	
	--	now remove the temp file, this indicates to the application that we did the work
	
	do shell script "rm" & " " & quoted form of tempFilePath
	
	return true
end documentShouldFinalizeAuthenticatedSave

これでtextwranglerを一度終了してから使うと、sudoと同じようにパスワードは必要ですが、GUIでconfファイル等を編集可能になります。