Skip to content

Instantly share code, notes, and snippets.

@haifengkao
Last active October 24, 2023 19:04
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save haifengkao/072136412953619d51bc8d21e28e61b4 to your computer and use it in GitHub Desktop.
Save haifengkao/072136412953619d51bc8d21e28e61b4 to your computer and use it in GitHub Desktop.
A shell script function to open a Finder tab from terminal
# open the current folder in Finder's tab
function oft() {
local folder_name=$1
if ! [[ -d $1 ]]; then
# it is a file, get the enclosing folder
folder_name="$(dirname "$1")"
fi
# if no arguments are given, we use the current folder
# 'pwd -P' will resolve the symbolic link (Finder always resolves the symbolic link)
oft_absolute_path=$(cd ${folder_name:-.}; pwd -P )
# execute the applescirpt
osascript 2>/dev/null <<EOF
on currentFinderPath()
tell application "Finder"
try
set finder_path to POSIX path of (target of window 1 as alias)
on error
set finder_path to ""
end try
end tell
end currentFinderPath
# Finder returns a path with trailing slash
# But PWD doesn't have one, so we add one for it
set new_tab_path to "$oft_absolute_path" & "/"
tell application "Finder"
activate
if not (exists window 1) then
make new Finder window
end if
set finder_path to my currentFinderPath()
if finder_path = "" then
# the finder's window doesn't contain any folders
set target of front window to (new_tab_path as POSIX file)
return
end if
end tell
if new_tab_path = finder_path then
# the finder's tab is already there
return
end if
# get the last path component name e.g., /usr/local/ -> local
# we need it to compare with the name of radio buttons (the name of tabs)
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"/"}
# assume there is a trailing slash at the end of path
set last_folder_name to text item -2 of new_tab_path
set AppleScript's text item delimiters to ASTID
# iterate through all radio buttons to check if the tab has been opened or not
# if it is not working for the future versions of Finder
# iteration all UI components by 'entire contents of window 1'
# see [Finding Control and Menu Items for use in AppleScript User Interface Scripting](http://hints.macworld.com/article.php?story=20111208191312748)
tell application "System Events"
tell process "Finder"
set radio_buttons to radio buttons of window 1
set button_num to length of radio_buttons
repeat with i from 1 to button_num
try
set button_i to item i in radio_buttons
if not title of button_i = last_folder_name then
# the tab name doesn't match
# simulated 'continue'
error 0
end if
# click the button will change the Finder's target path
click button_i
set finder_path to my currentFinderPath()
if new_tab_path = finder_path then
# the finder's tab is already there
return
end if
# if we switch tab, the buttons will become invalid
# so we have to retrieve them again
set radio_buttons to radio buttons of window 1
end try
end repeat
end tell
end tell
# the folder is not opened yet
# open a new tab in Finder
tell application "System Events" to keystroke "t" using command down
# set the Finder's path
tell application "Finder"
set target of front window to (new_tab_path as POSIX file)
end tell
return
EOF
# clear the tempory veriable
unset oft_absolute_path
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment