October 29, 2010

在finder中開啟終端機

先前曾經找過從finder直接開啟該目錄下的終端機的作法。今天又找了一個用小機器人(Automator)達到這件事的作法。參考網址

照參考網址做大概就可以了。只不過有一點小問題就是,在新的automator中並沒有save as plugin的選項。後來找到這篇討論裡面提到新的automator把這個選項取消掉了,所以如果要做同樣的事情,請在新增的時候選擇"服務"就可以了。

具體的步驟如下
1.打開automator,開啟一個新的服務。
2.從左邊拉一個AppleScript進來,script內容如下。on run {input, parameters}
tell application "Terminal"
activate
if (the (count of the window) = 0) or ¬
(the busy of window 1 = true) then
tell application "System Events"
keystroke "n" using command down
end tell
end if
do script "cd \"" & (POSIX path of ¬
(input as string)) & "\"" in window 1
end tell
return input
end run

3.存檔即可。

操作畫面如下。

直接用action script的作法有一個缺點就是,每次開起來都會多開一個在~/底下的terminal,雖然無礙,但是惱人。用小機器人的這個作法雖然不會出現兩個視窗了,但缺點就是有一點慢... orz 實在是有一好沒有兩好呀...

10/31後記。
仔細研究過兩邊的apple script之後找出可以讓他開出來只有一個視窗的辦法。雖然有點笨,不過總算可以達到目的。
程式碼如下-- this subroutine processes does the actual work
--
on process_item(this_item)
set thePath to quoted form of POSIX path of this_item
tell application "Terminal"
activate
if (the (count of the window) = 1) then
tell application "System Events"
keystroke "w" using command down
end tell
end if
do script with command "source ~/.bashrc; cd " & thePath
tell window frontmost
set custom title to this_item
end tell
end tell
end process_item

其中tell application "System Events"
keystroke "w" using command down
end tell
這一段就是把前一個視窗關掉的指令。
這樣就不會再跑兩個視窗出來了。

0 Comments: