Skip to content

Instantly share code, notes, and snippets.

@xiaolai
Created September 5, 2021 05:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xiaolai/ce1d47b4dc7c28237ccc847664fdbbf4 to your computer and use it in GitHub Desktop.
Save xiaolai/ce1d47b4dc7c28237ccc847664fdbbf4 to your computer and use it in GitHub Desktop.
Open Terminal Here
tell application "Finder"
if (count of windows) > 0 then
set thePath to target of front window
else
display dialog "There are no open Finder windows." with icon caution buttons {"OK"} with title "Open Terminal Here..."
return
end if
end tell
if not (exists thePath) then
tell application "Finder" to set theName to name of front window
display dialog "The location of the Finder window \"" & theName & "\" is not a real location (e.g. smart folder, search, network, trash, etc) and cannot opened in Terminal." with icon caution buttons {"OK"} with title "Open Terminal Here..."
return
end if
set thePath to quoted form of POSIX path of (thePath as alias)
tell application "Terminal"
if it is running then
-- This seems to be the only way to tell Terminal to open a new tab
tell application "System Events"
tell application process "Terminal"
set frontmost to true
try
keystroke "t" using command down
on error errMsg
set msg to "Error: " & errMsg & "
This probably means that you're using Mojave and you will need to add this app to the \"Accessibility\" section in the \"Privacy\" tab of the \"Security & Privacy\" System Preferences.
Please see the \"Mojave\" section of the README for more details"
display dialog msg buttons {"OK"} with icon caution with title "Open Terminal Here..."
return
end try
end tell
end tell
do script "cd " & thePath & " && clear" in last tab of front window
else
do script "cd " & thePath & " && clear"
end if
activate
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment