Sometimes I like to open folders in Terminal by dragging the folder to the Terminal dock icon. However this creates a new Terminal window instead of creating a new tab in my current Terminal window. How do I make Terminal open the dragged folder in the same window similar to the right click services option for 'New Terminal at Folder'.
MacOS – How to get ‘New Terminal at Folder’ functionality when dragging a folder to the terminal dock icon
dockmacosterminal
Related Solutions
You can kludge something together with some AppleScript and some clever icon image pasting. Here's how you'd do it for iTerm/iTerm2.
Open the AppleScript Editor and paste in the following AppleScript:
set myapp to "iTerm"
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
if appIsRunning(myapp) then
# What you do here will depend on the program. This works for
# iTerm but you'll need to look up the actions for other programs
# since this bit is iTerm-specific.
tell application "iTerm"
set myterm to (make new terminal)
tell myterm
launch session "Default"
end tell
end tell
else
tell application myapp
activate
end tell
end if
You can use the Run button in the editor to test that it works. If you want a different profile opened change "Default" on line 12 to the name of the profile you want opened.
Now save this AppleScript. In the Save dialog under "File Format" at the bottom select "Application" from the list.
I saved mine as "New iTerm.app" to my Desktop.
Now every time I click on "New iTerm.app" on my Desktop I get a new iTerm window. I can drag "New iTerm.app" to my Dock and it'll be a Dock icon I can click on any time I want to get a new iTerm window open. But the icon is the generic AppleScript icon.
Let's change it to the iTerm icon. Select "New iTerm.app" on the Desktop and click Cmd-I to bring up the properties screen for the application.
Now navigate to /Applications
in a Finder window, find iTerm in the list of installed applications, select it and click Cmd-I to open the properties screen for the iTerm application.
Click the icon in the iTerm info window so it gets a soft blue shadow around it. Press Cmd-C to copy the icon to the clipboard.
Now click the icon in the "New iTerm.app" info window and press Cmd-V to paste the iTerm icon on to the "New iTerm.app" application.
It should now have an iTerm icon.
You can move the "New iTerm.app" to your /Applications
folder if you like. Drag it from the folder you decide to keep it in, back to your dock, and you can now click on it in the Dock to open up a new iTerm window with every click.
If you wanted to be able to distinguish it from the running iTerm.app Dock icon you could consider pasting the icon in to an image editor like Pixelmator and adding a big red +
to it so it's clearly identified as the icon to press for a new iTerm window instead of the icon to press to see the exiting, open iTerm application.
Here are some more scripts for some other applications to get you started customizing things:
Google Chrome (New Window)
set myapp to "Google Chrome"
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
if appIsRunning(myapp) then
tell application "Google Chrome"
make new window
end tell
else
tell application myapp
activate
end tell
end if
Google Chrome (New Tab)
set myapp to "Google Chrome"
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
if appIsRunning(myapp) then
tell application "Google Chrome"
make new tab at end of tabs of window 1
end tell
else
tell application myapp
activate
end tell
end if
Sublime Text 2
Unfortunately ST2 doesn't seem to have an AppleScript accessible extensions. You can always take a look at the OS X command line tool that ST2 ships with. You can call that from a simple script in the Dock to open a new window in ST2. So:
set myapp to "Sublime Text 2"
on appIsRunning(appName)
tell application "System Events" to (name of processes) contains appName
end appIsRunning
if appIsRunning(myapp) then
# Assumes you've installed the subl command line tool for ST2
# in to /usr/local/bin. Adjust accordingly.
do shell script "/usr/local/bin/subl --new-window"
else
tell application myapp
activate
end tell
end if
It is possible in nightly release of the alternative terminal program iTerm2 via Preferences → Advanced → Tabs: New tabs are added at the end, not next to current tab set to No.
Best Answer
I was able to do it two different ways:
First, drag and drop the folder directly into the window you want the path copied to by clicking and dragging the folder, using Cmd+Tab to open the Terminal window you were working in, then dropping it on the Terminal window.
Second, drag and drop the folder onto the Terminal icon in the Dock like you were before, but hold it there for a few seconds until the window you had open appears, then continue dragging the folder into the Terminal window and dropping it there.