I want to create shortcuts like "Copy path of current folder" and "Open terminal in current folder" in the screenshotted context menu. I know how achieve this with Services and Automator but only for when a file or folder is right-clicked. How do I do it when a blank area is right-clicked?
2 Answers
You asked, "How do I do it when a blank area is right-clicked?", well sans the content of the default context menu, I believe something has to be selected to be acted upon otherwise you'll always just get the default context menu. That said though, you could create an Automator Service that receives no input in Finder and assign it a keyboard shortcut. Then using AppleScript, you would code it to do what you want based on the target
property of the front window of Finder.
The following two example AppleScript code segments can be used in a Run AppleScript action in an Automator Service to which a keyboard shortcut can be assigned or as an AppleScript Application created in Script Editor and the app dragged and dropped onto the Toolbar in Finder.
This example copies to the clipboard the POSIX path of the current folder of the front Finder window:
try
tell application "Finder"
set the clipboard to text items 1 thru -2 of POSIX path of (target of front window as string) as string
end tell
end try
This example opens Terminal to the current folder of the front Finder window:
try
tell application "Finder"
set theTargetPath to text items 1 thru -2 of POSIX path of (target of front window as string) as string
end tell
tell application "Terminal"
do script "pushd " & theTargetPath & "; clear"
activate
end tell
end try
That said though, the only other thing I can think of would be to use Xcode and create a Service or App Extension to do what you're asking, if it's even doable.
Note: The example AppleScript code is just that and, sans the try
statement as an error handler, does not include any other error handling as may be appropriate/needed/wanted, the onus is upon the user to add any appropriate error handling for any example code presented.
There are 2 apps that add an icon to the finder window which will open a terminal window in the directory displayed. The apps are 'GotoShell' and 'ShellHere'. I have both apps and they both seem to work equally well. GotoShell allows the user to select either Terminal or iTerm for your command line program. You can also define a script to run when the window is opened.