Script that types command+shift+C in the Finder

I’m trying to write a very simple script that types command+shift+C in the Finder (I’m turning into some sort of folder action script tweaker :shock: ). The script has to either do this, or simulate the menu selection of Go > Computer

While sending keystrokes to System Events works, sending them to the Finder doesn’t. Any idea how to make this work?

This sorta makes me wish for Akua for OS X…

It would help if you posted your code so people could see what was wrong.

In any case, you have to use System Events to send keystrokes. Additionally you need to further target the specific application you want to receive them:

tell application "System Events"
   tell application "Finder"
      -- keystrokes go here
   end tell
end tell

Although you should consider whether you really need to take this approach. Generally it’s much simpler and easier to script the actions directly (in this case using something like the standard ‘mount volume’ command, bypassing the Finder altogether).

Hi,

If you still want the keystrokes:

tell application “Finder” to activate
tell application “System Events”
tell process “Finder”
keystroke “c” with {command down, shift down}
end tell
end tell

If you run Panther change ‘with’ in line 4 to ‘using’.

gl,

Well, my old code went something like this:


on opening folder this_folder
	tell application "Finder"
		keystroke "c" using {command down, shift down}
	end tell
end opening folder

Obviously, this didn’t work too well. I tried a few variants of System Events sending the command to the Finder, but it appears as though I just had the syntax slightly incorrect.

The new code:


on opening folder this_folder
	tell application "Finder"
		activate
		tell application "System Events"
			tell process "Finder"
				keystroke "c" using {command down, shift down}
			end tell
		end tell
	end tell
end opening folder

In case you were wondering about the purpose of this script… I like to keep my desktop neat and tidy. So, having all the currently mounted drives listed under folder would be an excellent way to do so (sort of faux Windows “My Computer” style). A folder action is definitely much quicker and easier to pursue than the writing of a background daemon of some sort. And since Apple disallowed the creation of an alias to the “Computer” directory, I had to take these extreme measures.

And no, an alias to the /Volumes directory is not the same :smiley:

Thanks a lot everyone