RSVP - Please Reply

Armin's Spot on the Web


Home

Impressum

My Code


What is a Weblog?

Pictures (.Mac)


NetNewsWire: More news, less junk. Faster

Discussion

Recent Discussion

Create New Topic


Finder Script - Open Folder in new Terminal

This Script will open a new Terminal window and automatically cd to the directory that is shown in the frontmost Finder window. You could also save the script as an Applet and place that in the toolbar. However then you will have to wait an extra second or two while the Applet starts. That really annoys me so I prefer the Script menu.

The Code

Copy and paste this into Script Editor or open in Script Editor.

tell application "Finder"
	try
		set t to target of Finder window 1
	on error -- there is no window, use desktop instead
		set t to desktop
	end try
	-- there is no 'is subclass of... :-(
	-- you might to to add the 'package' class to the list
	if class of t is not in {folder, disk, class of desktop, class of trash} then ¬
		set t to container of t
	set thePath to (quoted form of POSIX path of (t as alias))
end tell
tell application "Terminal"
	set w to make new window at the beginning of every window
	do script "cd " & thePath & "; ls -al"
	activate
end tell

I guess the code is pretty easy to read and understand. The Script checks if the selected file is a container (folder, disk, desktop, trash) and if not (it is a file or package) cds to the parent of the file. Power users who want to rummage around in packages, may want to add the package class to this list.

This line really shows the limitations of OOP in AppleScript. All the classes in the list are subclasses of the class container but AppleScript has no way of checking for superclasses or implementations. Annoying...

I added ls -al after the cd because that is what I usually want to see and it doesn't really bother if you don't want to see it. Change or remove at your discretion.

BTW: is there any way to make the Terminal execute a script in an exisiting window?

Note: There seems to be a bug in the way Terminal handles unicode characters. If you have a folder with unicode characters in the name this script will fail. Apple is aware of the bug.

PS: the other way, opening a folder from the terminal is simple. In the Terminal simply type open . to open a Finder Window to the current working directory. Type open anything will act as if anything were double-clicked in the Finder. open -e file will open the file in TextEdit. open -a "application" file will open the file with the given application.

More Scripts on RSVP

Discuss

Google
WWW RSVP.atsites.de

This page was last updated: Tuesday, November 18, 2003 at 12:42:18 PM
Copyright 2010 RSVP - Please Reply
Mail me @ arminbatmacdotcom
Create your own Manila site in minutes. Everyone's doing it!