Open Terminal in Current Folder & Create New Text in Current Folder by AppleScript

on

如下图所示,在Finder的toolbar中最后有两个自定义的图标,第一个是iTerm2图标,点击该图标会在Finder当前目录中打开iTerm2程序,第二个是TextWrangler图标,点击该图标会在Finder当前目录中用TextWrangler创建一个新文本文件,文件名为当前日期时间。
Finder

我们使用AppleScript来完成这两项工作。

1. 打开AppleScript Editor

2. 将后文给出的两段脚本copy到AppleScript Editor中,分别保存为两个Application。也就是在Save as的时候选择File format为“Application”(默认为Script)。
保存位置随意,我保存在/Applications目录下。比如第一个脚本保存为“Open iTerm2 Here”,第二个脚本保存为“New Text Here”。

3. 修改新建Application的图标,在Mac中复制粘贴的强大在这里体现了。
以“New Text Here”为例,在Finder中同时选中“New Text Here”和TextWrangler这两个程序,Cmd+I快捷键调出属性窗口,在TextWrangler的属性窗口中鼠标点击最上面的小图标(如下图),按Cmd+C复制,然后鼠标点击“New Text Here”属性窗口中最上面的小图标(原本是AppleScript的默认图标),按Cmd+V粘贴。
Copy Icon

4. 将修改完图标的两个App拖动到Finder顶端的Toolbar中,完成。

AppleScript:
–Open iTerm2 Here
–这里我使用的是iTerm2,你可以改成自己习惯的Terminal程序,比如iTerm,那么只需要将以下脚本中的tell application “iTerm2″改为tell application “iTerm”即可。

-- cd to the current finder window folder in iTerm2. Or drag a folder onto this script to cd to that folder in iTerm2.
-- found this script in the comments of this article: http://www.macosxhints.com/article.php?story=20050924210643297
-- script was opened by click in toolbar
on run
	tell application "Finder"
		try
			set currFolder to (folder of the front window as alias)
		on error
			set currFolder to (path to desktop folder as alias)
		end try
	end tell
	CD_to(currFolder, false)
end run

-- script run by draging file/folder to icon
on open (theList)
	set newWindow to false
	repeat with thePath in theList
		set thePath to thePath as string
		if not (thePath ends with ":") then
			set x to the offset of ":" in (the reverse of every character of thePath) as string
			set thePath to (characters 1 thru -(x) of thePath) as string
		end if
		CD_to(thePath, newWindow)
		set newWindow to true -- create window for any other files/folders
	end repeat
	return
end open

-- cd to the desired directory in iterm2
on CD_to(theDir, newWindow)
	set theDir to quoted form of POSIX path of theDir as string
	tell application "iTerm2"
		activate
		delay 1
		-- talk to the first terminal 
		tell the first terminal
			try
				-- launch a default shell in a new tab in the same terminal 
				launch session "Default Session"
			on error
				display dialog "There was an error creating a new tab in iTerm2." buttons {"OK"}
			end try
			tell the last session
				try
					-- cd to the finder window
					write text "cd " & theDir
				on error
					display dialog "There was an error cding to the finder window." buttons {"OK"}
				end try
			end tell
		end tell
	end tell
end CD_to

–New Text Here
–这里我使用的是/usr/local/bin/edit来直接创建新文档,此命令在安装完TextWrangler之后才会有,如果你使用其它的文本编辑器(比如系统自带的TextEdit),那么可能无法使用该脚本。
–该脚本中获取当前日期时间拼成文件名的实现方法有些丑陋,如果你有更好的方法请告诉我。

-- Create new text file in Finder's Current Folder
-- 2011.7.1 Written by Kamus

-- script was opened by click in toolbar
on run
	tell application "Finder"
		try
			set currFolder to (folder of the front window as alias)
		on error
			set currFolder to (path to desktop folder as alias)
		end try
	end tell
	create_text_file(currFolder)
end run

-- cd to the desired directory in iterm
on create_text_file(theDir)
	set theDir to quoted form of POSIX path of theDir as string
	set yearStr to year of (current date) as integer
	set monthStr to month of (current date) as integer
	set dayStr to day of (current date) as integer
	set timeStr to time of (current date) as integer
	
	try
		do shell script "/usr/local/bin/edit " & theDir & yearStr & monthStr & dayStr & timeStr & ".txt"
	on error the error_message number the error_number
		display dialog "Error: " & the error_number & ". " & the error_message buttons {"OK"} default button 1
	end try
end create_text_file

Leave a Reply

Your email address will not be published. Required fields are marked *