Open Total Commander from Cygwin command line

A script to open the file explorer Total Commander from the Cygwin command line. Allows user to:

  • Open paths in currently running instance.
  • Open paths in existing or new tabs.
  • Specify paths for left and/or right panels.

Purpose. To provide a flexible way to open paths in Total Commander from Cygwin. Now I can use this or Windows Explorer from the Cygwin command line.

Input. Paths to open. Options to open new tabs.

Output. Total Commander opened to given paths.

Dependencies. Total Commander must be installed! Paths must exist.

<>Examples. See usage section in script.

Here is the script.

#!/bin/bash
#===============================================================================
#
#          FILE:  totalCommander.sh
#
#         USAGE:  see usage
#
#   DESCRIPTION:  Open Total Commander to whatever path.
#
#       OPTIONS:  - see usage
#  REQUIREMENTS:  - Total Commander be installed http://www.ghisler.com/
#                 - Paths requested to be opened exist
#          BUGS:  ---
#         NOTES:  ---
#        AUTHOR:  Robert Mark Bram (RMB), robertmarkbram@gmail.com
#       COMPANY:  http://robertmarkbramprogrammer.blogspot.com/
#       VERSION:  1.0
#       CREATED:  07/31/09 16:54:46 AUSEST
#      REVISION:  ---
#===============================================================================

set -o nounset                              # Treat unset variables as an error


#-------------------------------------------------------------------------------
#  Variables for this script.
#-------------------------------------------------------------------------------
# Open new tabs? Blank for no, '/T' for yes.
newTab=
# Path to open on left.
tcpathLeft=
# Path to open on right.
tcpathRight=
# Total Commander command.
totalCommander=/apps/totalcmd/TOTALCMD.EXE
# Should we be verbose?
verbose=no


#-------------------------------------------------------------------------------
#  Common functions for this script.
#-------------------------------------------------------------------------------


#===  FUNCTION  ================================================================
#          NAME:  checkVars
#   DESCRIPTION:  Check initial state of vars in this script.
#    PARAMETERS:
#       RETURNS:
#===============================================================================
function checkVars() {
   if [ -z "$tcpathLeft" -a -z "$tcpathRight" ] ; then
      tcpathLeft=.
   elif [ ! -z "$tcpathLeft" -a ! -e "${tcpathLeft}" ] ; then
      usage "Left location [${tcpathLeft}] does not exist."
      exit 2;
   elif [ ! -z "$tcpathRight" -a ! -e "${tcpathRight}" ] ; then
      usage "Right location [${tcpathRight}] does not exist."
      exit 2;
   fi
   type $totalCommander &> /dev/null
   if [ ! -e "$totalCommander" ]; then
      echo "There is no spoon. Or total commander @ '$totalCommander'."
      exit 2;
   fi
}


#===  FUNCTION  ================================================================
#          NAME:  message
#   DESCRIPTION:  Output message if verbose is on
#    PARAMETERS:  message to be printed
#       RETURNS:  -
#===============================================================================
message ()
{
   if [ "$verbose" == "yes" ] ; then
      echo -e "${1}"
   fi
}    # ----------  end of function message  ----------


#===  FUNCTION  ================================================================
#          NAME:  processArgs
#   DESCRIPTION:  Check command line arguments.
#    PARAMETERS:  $@ from this script invocation
#       RETURNS:  Exit or take other action with error messages
#===============================================================================
processArgs ()
{
   # Process all the opening arguments.
   while [ "$#" -gt 0 ]
   do # until we have no more args to process.
      case "${1}" in
         "-h"     )  usage; exit 0;;
         "--help" )  usage; exit 0;;
         "-l"     )  tcpathLeft="${2}"; shift; shift;;
         "-r"     )  tcpathRight="${2}"; shift; shift;;
         "-t"     )  newTab="/T"; shift;;
         "-v"     )  verbose=yes; shift;;
         *        )  usage "Unknown option: $1" ; exit 2;;
      esac
   done
}    # ----------  end of function processArgs  ----------


#===  FUNCTION  ================================================================
#          NAME:  help
#   DESCRIPTION:  Output usage statement and exit.
#    PARAMETERS:  Error message.
#       RETURNS:
#===============================================================================
function usage() {
# Here document for help.. less or cat
errMessage=
if [ "$#" -gt 0 ] ; then
   errMessage=$1
else
   errMessage=HELP
fi
# Here document for help.. less or cat
cat << STOP_HELP
$errMessage
usage: $0 [-t] [-l leftPath] [-r rightPath]
Open Total Commander to paths, if specified, or current directory otherwise.
Always uses currently running instance. Opens new instance if none are running.
   -l    - Path to open in left tab.
            If neither -l or -r are given, default value is "-l .".
   -r    - Path to open in right tab.
            If neither -l or -r are given, default value is "-l .".
   -t    - Open paths in new tabs.
   -v    - verbose
Examples
1) Open current directory in left panel, current tab.
      $0
2) Open current directory in left panel, parent directory in right panel in new
   tabs.
      $0 -l . -r .. -t
STOP_HELP
}


#-------------------------------------------------------------------------------
#  Script Logic
#-------------------------------------------------------------------------------


processArgs "$@"
checkVars

if [ ! -z "$tcpathLeft" ] ; then
   tcpathLeft=/L=`cygpath -d -a "${tcpathLeft}"`
fi
if [ ! -z "$tcpathRight" ] ; then
   tcpathRight=/R=`cygpath -d -a "${tcpathRight}"`
fi

message "TC with left [$tcpathLeft], right [$tcpathRight] with new tab [$newTab]."

$totalCommander /O $newTab "${tcpathLeft}" "${tcpathRight}" &

# echo Done.

Popular Posts