Skip to content

Instantly share code, notes, and snippets.

@river24
Last active December 2, 2015 09:22
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 river24/abe6b885094a4b53fd90 to your computer and use it in GitHub Desktop.
Save river24/abe6b885094a4b53fd90 to your computer and use it in GitHub Desktop.
Open directory (folder) in a new tab of Finder from terminal
#!/bin/sh
function show_usage_and_exit() {
echo "Usage: finder.sh DIRECTORY_NAME"
exit
}
if [ $# -lt 1 ]
then
show_usage_and_exit
else
TARGET=$1
if [ -d "${TARGET}" ]
then
TARGET_FULL_PATH=`cd "${TARGET}" ; pwd | iconv -f UTF-8-MAC -t UTF-8`
TARGET_FULL_PATH=`/bin/echo -n ${TARGET_FULL_PATH}`
/bin/echo -n ${TARGET_FULL_PATH} | iconv -f UTF-8-MAC -t UTF-8 | pbcopy
else
echo "'${TARGET}' is not a directory."
show_usage_and_exit
fi
fi
osascript << EOS
on run
try
tell application "Finder"
activate
set t to target of Finder window 1
set toolbar visible of window 1 to true
end tell
tell application "System Events"
keystroke "t" using command down
end tell
tell application "Finder"
set target of Finder window 1 to t
end tell
tell application "System Events"
keystroke "g" using {command down, shift down}
end tell
tell application "System Events"
keystroke "v" using command down
keystroke return
end tell
on error
tell application "Finder"
open ("${TARGET_FULL_PATH}" as POSIX file)
activate
end tell
end try
end run
EOS
# Getting the size of display
DISPLAY_SIZE=`osascript -e 'tell application "Finder" to get bounds of window of desktop'`
DISPLAY_WIDTH=`echo ${DISPLAY_SIZE} | awk -F', ' '{print $3}'`
DISPLAY_HEIGHT=`echo ${DISPLAY_SIZE} | awk -F', ' '{print $4}'`
# You may fail to get the display size.
# In such a case, you can set the display size manually by uncommenting and editing the following 2 lines.
# DISPLAY_WIDTH=1920
# DISPLAY_HEIGHT=1080
# Setting the height of Finder window to 2/5 of display height
FINDER_HEIGHT=`expr ${DISPLAY_HEIGHT} \* 2 / 5`
# You can set the height of Finder window by uncommenting and editing the following line.
# FINDER_HEIGHT=480
FINDER_Y=`expr ${DISPLAY_HEIGHT} - ${FINDER_HEIGHT} - 4`
osascript -e 'tell application "Finder" to set position of first window of application process "Finder" to {0,"'"${FINDER_Y}"'"}'
osascript -e 'tell application "Finder" to set size of first window of application process "Finder" to {"'"${DISPLAY_WIDTH}"'","'"${FINDER_HEIGHT}"'"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment