Limit file selection and how to reference within Applescript?

I want to write an Automator action that take the currently selected folder or file and opens the terminal in that particular location.

I believe I need to do this with an Applescript, but I am finding the Applescript documentation lacking.

Here’s what I have so far:

  1. Get Selected Finder Items, which passes to
  2. Run Applescript:
on run {input, parameters}
	
	(* Your script goes here *)
	
	tell application "Terminal"
		do script "cd /Users/Shared"
	end tell
	
	return input
end run

I am having difficulties with two things:

  1. limiting file selection to only one item
  2. referencing the passed in file(s) in the Applescript.

Some pointers would be much appreciated.

try this.

on run {input, parameters}
	tell application "Finder" to set thePath to POSIX path of (container of item 1 of input as string)
	tell application "Terminal" to do script "cd " & quoted form of thePath in window 1
	return thePath
end run

Wow, thanks for that! I will give it a try.

How is one to learn that?

Thanks

the “bible” of AppleScript is Matt Neuburg’s AppleScript: The Definitive Guide

and lots of trial and error :wink:

Model: G5 dual 2,5 GHz
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Here’s what I have so far and it works great. Just posting to attempt to give something back:

on run {input, parameters}
	tell application "Finder"
		set theItem to item 1 of input
		set thePath to POSIX path of (theItem as string)
		
		if (thePath does not end with "/") then
			set thePath to POSIX path of (container of theItem as string)
		end if
	end tell
	tell application "Terminal" to do script "cd " & quoted form of thePath in window 1
	return thePath
end run