#!/bin/sh	
#v space v. 1.0 by duus        (Jul 09, 2008)
#v   location: http://thedu.us/scripts/THISSIR/space
#h space script by duus

##################################################### VERSION INFO
#v space, a bash wrapper for Spaces, v 0.1, by duus
#v
#v 	  type <space help> for more information
#v    or open source code with a text editor and read it:
#v    http://thedu.us/scripts/space/space/
#v
###################################################### INSTALL INFO
#i How to install space
#i  1. put the space bash script on your path (for example, ~/bin/)
#i  2. navigate to that directory
#i  3. type "chmod +x space" to make the script executable
#i  4. open the space script with your favorite text editor (pico space, for example)
#i  5. Navigate to the line called #SETTINGS
#i  6. Adjust settings as necessary.  Save the file.
#i  7. type "space help" for usage information
#i
###################################################### SETTINGS
# general settings; name and location of this script, for self-referencing.
_SCRIPT_NAME=space            
_SCRIPT_LOCATION=~/bin           
# you can also hardcode if you would like.  For example, your own bin:
# _SCRIPT_LOCATION=~/bin
# or, if you are running reuben, you can access reuben's bin:
# _SCRIPT_LOCATION=~/.reuben/.bin/
# don't forget that this program is merely a text file, and you must make 
#   it executable, regardless of where you place it.
# Script_Location is used in the standard functions, below.
#
###################################################### STD, FUNCTIONS (help, version, install)
#  (h)elp, (v)ersion, and (i)nstall are standard.  I do not recommend changing them.
#    Reading them, on the other hand, to see how they work is encouraged.
#    Feel free to add another standard function.  If you do, please let me know if it is useful,
#     and whether you would mind if I included it in the standard code, with credit for you
#     in comments.  (Or an about section?  .... hmmmm)
#    These functions allow the output of the standard functions to be 
#    hardcoded into the source code.  (See #i and #v above for examples.) 
#
if [ "$1" = "version" -o "$1" = "v" ]                
	then   
	grep "^#v" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more                              
	exit 1;
elif [ "$1" = "help" -o "$1" = "h" ]
	then
	grep "^#h" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
	exit 1;
elif [ "$1" = "install" -o "$1" = "i" ]
	then
	grep "^#i" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
	exit 1;
elif [ "$1" = "src" ]
	then
	echo "source code of ${_SCRIPT_NAME}, a duusscript"
	echo ""
	more $_SCRIPT_LOCATION/${_SCRIPT_NAME}
	exit 1;
fi 
#
#  don't understand how this works?  see http://thedu.us/scripts/duushelp/ or run duushelp.sh
#
################################################################
#---------------------------------------------------------#
# begin space   (Jul 09, 2008)
# --------------------------
if [ "$1" = "" ]
	then
	echo "current space is `space show`"
	exit 1;
fi

while [ "$1" != "" ]
	do
	_casevar=${1}
	shift
	case "${_casevar}" in
##################################################################   get current space
	     's' | 'show' | 'get' ) # case 'show'
#h   (s)how :  print the current space
			 echo | osascript <<EOF
			-- from
			--   http://www.macosxhints.com/article.php?story=20080227075244778
			-- 10.5: Find current Space number via AppleScript 
			set xxVar to 0
			tell application "System Events"
				tell process "SystemUIServer"
					set xVar to value of attribute "AXChildren" of menu bar 1
					set cVar to count of xVar
					repeat with iVar from 1 to cVar
						set zVar to value of attribute "AXDescription" of item iVar of xVar
						try
							if zVar = "spaces menu extra" then
								set xxVar to iVar
								exit repeat
							end if
						end try
					end repeat
				end tell
			end tell
			if xxVar = 0 then
				display dialog "Spaces Menu Extra not installed"
			else
				tell application "System Events"
					tell process "SystemUIServer"
						set theCurrentSpace to value of menu bar item xxVar of menu bar 1
					end tell
				end tell

				-- Do what you want with "theCurrentSpace" variable starting here
				get theCurrentSpace as number
				-- Do what you want with "theCurrentSpace" variable ending here

			end if
EOF
		
	         ;;
##################################################################   go to current space
	     'g' | 'go' ) # case 'go'
#h   (g)o #  : go to space '#' 
			_spacenum=${1}
			shift
			if [ `isnumeric ${_spacenum}` = 0  ]
				then
				echo "${_spacenum} is non-numeric, and therefore not a valid space.  exiting."
				exit 1;
			fi
			_asciinum=`expr ${_spacenum} + 48` # this is the ascii character of this guy
			echo "switch to space ${_spacenum}..."
			# from: http://cerebrum.leftbrained.co.uk/post/41179273/rebinding-spaces-in-leopard
#h        Note: this assumes that option-# is set to take you to space #!  set in spaces preferences
			echo | osascript <<EOF
tell application "System Events" to keystroke (keystroke (ASCII character ${_asciinum}) using {control down})
EOF
			;;
		*) # default case
#h   # :  space num will assume space go num.
			expr ${_casevar} + 1 > /dev/null
			if [ `isnumeric ${_casevar}` = 0 ]
				then
				# not a number, not sure what to do.
				echo "not found: ${_casevar}"
				exit 1;
			else
				#if it's a number, switch to that space
				space go ${_casevar}
			fi
			;;
	esac
done


# --------------------------
# <tt>                 :               end space
#                      . 
#            ...---.,o8:i__,8Pd8   
#         _//      `"' :  d8'",8d8   
#       ,/''  vVv      . dP   b8P.8|
#      ./;|  d^_^b     .     ,P O||]
#       |||  duus@thedu.us\     |||| 
#       |||  duus.thedu.us/      || 
#       ||\            .        /_' 
#        ;+,           :       |\8
#          '.`>+.     _./'_/2/b/_
#           '-oh\._.._j:;'`P;8/,
#              `'->:o:.:src/='
#                      :    
#                      :    http://thedu.us/scripts/space/space 
# </tt>                .    Last invoked: Jul 09, 2008, when duus was feeling quixotic.
# -------------------------- 

