How do I run Applescript on currently selected folder ?

I want I want I want :frowning:

Hi good people

I would like to run an AppleScript on a folder without having to use the (choose folder) command.
I also do not want to restrict the AppleScript to one particular folder by giving a folder path.

What I do want to do is…

Run the AppleScript on any folder, anywhere on my MAC and through a keyboard shortcut or Right Click of the mouse (and then select the AppleScript as you can with Automator services).

Is this possible ?

Does anyone have a script where you can select the script to run on a currently selected folder ?

I’m begging here.
Thank You
Gaz

Model: iMac Mac OS X (10.6.4) 2.8 GHz Intel Core i7
AppleScript: Automator - Version 2.1
Browser: Firefox 3.6.12
Operating System: Mac OS X (10.6)

tell application "Finder"
	set theSel to item 1 of (get selection) -- returns a reference
end tell

You have to select the folder first, obviously.

or an easier concept to understand (at least for me):


tell application "Finder"
	set theItem to the selection as alias
end tell

this throws an error if you select more than one file

Thanks very much

Could I trouble you to show me how to use your script with the following ?


THE FOLLOWING SCRIPT WAS KINDLY SUPPLIED TO ME BY CAMALOT (A MEMBER OF THE FORUMS HERE)

set theFolder to (choose folder)
tell application "Finder"
	-- rename the existing folders
	try -- use a 'try' block to catch errors, e.g. the folder doesn't exist
		set name of folder "Fonts" of folder theFolder to "02_FONTS"
	end try
	try
		set name of folder "Links" of folder theFolder to "03_IMAGE_LINKS"
	end try
	
	-- make the new folders
	make new folder at folder theFolder with properties {name:"01_MASTER_DOC"}
	make new folder at folder theFolder with properties {name:"04_PDF_LR"}
	
	-- delete the unwanted files
	delete file "Instructions.txt" of folder theFolder
	delete file "Artwork_Folder_Test_UK.indd" of folder theFolder -- this will probably fail!
	
	-- finally, rename the folder (there are reasons why I do this last
	if name of folder theFolder contains " Folder" then
		set fn to name of folder theFolder
		set newName to characters 1 through -8 of fn as text
		set name of theFolder to newName
	end if
end tell

How would I build in your script below, to the above ?

tell application "Finder"
	set theItem to the selection as alias
end tell

Thank you
GazB

That second snippet will make the script fail when you mistakenly select more than one item, as Stefan said.
“get selection” always returns a list, even when only one item is selected.

As regards your original question: you wanted to replace “choose folder”, and you were shown the command which would achieve that. Just replace the “choose folder” line with what I gave you, above. Note that I used “theSel” where your script uses “theFolder”.