Skip to content

Instantly share code, notes, and snippets.

@blizzrdof77
Forked from haifengkao/open_finder_tab.sh
Last active April 6, 2020 06:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blizzrdof77/5b5543a6a402b54e3eeb14312bfcdf2b to your computer and use it in GitHub Desktop.
Save blizzrdof77/5b5543a6a402b54e3eeb14312bfcdf2b to your computer and use it in GitHub Desktop.
A shell script to open a folder in Finder in a new tab or new window (if no window present)
#!/bin/bash
# open the current folder in Finder's tab
if [ -z "$1" ]; then
folder_name=$PWD
else
folder_name=$1
fi
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"
my makeNewTab()
set target of front window to (new_tab_path as POSIX file)
end tell
on makeNewTab()
tell application "System Events" to tell application process "Finder"
set frontmost to true
tell front menu bar to tell menu "File" to tell menu item "New Tab"
perform action "AXPress"
end tell
end tell
end makeNewTab
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