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

如下图所示,在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

How to using Quick Look to view SQL scripts in Mac OS X

Update@2021-11-06

直接安装SourceCodeSyntaxHighlight,可以完美解决该问题,而且还增加了代码高亮。

===以下所有内容可以不再关注===

在开始本文之前,先介绍两个与本文毫无关系但是很酷的QuickLook插件。
Zip Quick Look Plugin - 直接在QuickLook中查看zip包中的内容。
Folder Quick Look Plugin - 直接在QuickLook中查看文件夹的内容。

Mac OS X的Quick Look是很强大的功能。在Finder中高亮需要查看的文件,然后按空格键就可以在弹出的新窗口中直接预览该文件的内容,支持几乎所有需要预览的文件类型,比如txt文本,pdf,微软office文档,苹果iWork文档,可以用Quicktime播放的视频等等。

但是正如Apple的很多产品一样,预置的功能已经很好,但是却几乎不给使用者任何调整的方法,比如在我的机器里有很多.sql扩展名的SQL脚本文件,我很想能够用Quick Look来快速查看,但是在Mac OS X中天生的Quick Look是不支持快速预览sql文件的,并且没有给于任何选项来将某些文件类型加入到Quick Look中。

在默认情况下,用Quick Look打开SQL脚本,将仅仅显示如下文件信息窗口。我个人的系统设置是如果双击SQL脚本,则用TextWrangler打开,因此这里显示为“TextWrangler text document”。

我们需要手工修改一些文件,来让Quick Look也可以直接预览SQL脚本。

  1. 修改Info.plist文件,在我的例子中是修改TextWrangler的plist文件,如果你选择不同的应用程序打开SQL脚本,那么需要修改相应程序的plist。
vi /Applications/TextWrangler.app/Contents/Info.plist

在最后这两行的前面:

</dict>
</plist>

添加下面这些内容:

<key>UTExportedTypeDeclarations</key>
<array>
<dict>
  <key>UTTypeConformsTo</key>
  <array>
    <string>public.text</string>
    <string>public.plain-text</string>
  </array>
  <key>UTTypeDescription</key>
  <string>SQL Script file</string>
  <key>UTTypeIdentifier</key>
  <string>com.barebones.textwrangler</string>
  <key>UTTypeTagSpecification</key>
  <dict>
    <key>com.apple.ostype</key>
    <string>TEXT</string>
    <key>public.filename-extension</key>
    <array>
      <string>sql</string>
    </array>
  </dict>
</dict>
</array>

其中com.barebones.textwrangler可能需要改成你使用的应用名称,比如TextMate则是com.macromates.textmate,VSCode则是com.microsoft.VSCode,通常这个名字可以在plist文件的前面部分找到。

  1. 使改动生效
touch /Applications/TextWrangler.app

然后再次使用Quick Look就可以直接查看SQL脚本内容了。

参考文档:
Add Quick Look support for certain file formats

OPatch failed with error code 73 when patch Oracle 10.2.0.4 on Mac OS X

现在Mac OS X中的Oracle数据库最新版本是10.2.0.4 (没有10.2.0.5更没有11g)。如果想给该版本数据库打上PSU补丁的时候,会遇到以下错误。

Running prerequisite checks...
Prerequisite check "CheckPatchApplicableOnCurrentPlatform" failed.
The details are:
Patch (  ) is not applicable on current platform.
Platform ID needed is : 46
Platform IDs supported by patch are: 293
UtilSession failed: Prerequisite check "CheckPatchApplicableOnCurrentPlatform" failed.

OPatch failed with error code 73

这个错误是由于Bug # 8647770引起的。解决方法如下:
1. 下载patch 8647770.
2. 安装补丁

$ export OPATCH_PLATFORM_ID=293
$ cd 8647770
$ opatch apply

安装完此补丁以后,再重新运行PSU Patch,就可以正常打上补丁了。