Sie sind auf Seite 1von 4

#$language = "Python"

#$interface = "1.0"

# ColorSchemeAutoRotation.py
# Last Modified: 5 Sep 2013
#
# Description:
# This script will automatically rotate to the next Color Scheme
# each time a connection is made with the current session.
# For example (in a default SecureCRT configuration), if the
# color scheme for the current session is currently set to
# "Black / Cyan", the color scheme will automatically be
# set to "Floral White / Dark Cyan". The next time the
# session is used for making a connection, the color
# scheme will change from "Floral White / Dark Cyan" to
# "White / Black", and so on each time the session is used
# to connect.
#
# Although it is suggested that you use this script as a logon script
# you can also choose to create a button to run the script manually
# (for example if you don't like the current color scheme and want to
# manually rotate to the next one). For information on creating a
# button to run this script, read through the button bar tip online:
# http://www.vandyke.com/support/tips/buttonbar.html
#
# ... or view the Button Bar video on the VanDyke Software YouTube
# channel:
# https://www.youtube.com/VandykeSoftware
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import os, sys, platform, re

MsgBox = crt.Dialog.MessageBox

# Preload a list of existing color schemes from the configuration


# location specified in the user's registry.
bColorsLoadedFromConfig = False

# Set up two dictionaries for use in collecting color scheme names


# and cross-referencing each name with an index so that the color
# scheme rotation is much easier (we can deal with indices/numbers
# instead of names).
g_cColorSchemes = {}
g_cSchemeIndices = {}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def main():
# Make our global variables available to us in this routine
global bColorsLoadedFromConfig
global g_cColorSchemes
global g_cSchemeIndices

strConfigPath = ""

if sys.platform.startswith('darwin'):
# Load configuration folder from user "defaults" on
# Mac OS X:
from Foundation import CFPreferencesCopyAppValue
strConfigPath = CFPreferencesCopyAppValue(
"Config Path",
"com.vandyke.SecureCRT")

elif os.name == 'nt':


# On Windows, read config location from the HKCU registry hive
from _winreg import *
try:
key = OpenKey(HKEY_CURRENT_USER, "SOFTWARE\\VanDyke\\SecureCRT")
strConfigPath = QueryValueEx(key, "Config Path")[0]
CloseKey(key)
# Expand any environment vars that may be present (like %APPDATA%)
if "%" in strConfigPath:
strConfigPath = ExpandEnvironmentStrings(strConfigPath)
#except Exception as objInst:
except Exception, objInst:
MsgBox("Failed to read Config path from registry.\r\n\r\n" +
str(objInst))
return

elif os.name == 'posix':


# On Linux, find the config dir location from the user's
# personal .config/VanDyke/SecureCRT.conf file:
strHome = os.path.expanduser("~")
strConfigFile = strHome + "/.config/VanDyke/SecureCRT.conf"
if os.path.isfile(strConfigFile):
import ConfigParser
try:
# Using the ConfigParser, we can read the config dir
# location much easier than parsing the file ourselves.
config = ConfigParser.ConfigParser()
config.read(strConfigFile)
#except Exception as objInst:
except Exception, objInst:
MsgBox("Failed to parse config:\r\n\r\n" + strConfigFile +
"\r\n\r\n" + str(objInst))
return

strConfigPath = config.get("General", "Config%20Path")


else:
MsgBox("Unknown os: " + os.name + "\r\n\r\nScript cannot continue.")
return

if strConfigPath <> "":


if os.path.isfile(strConfigPath + "/Color Schemes.ini"):
objFile = open(strConfigPath + "/Color Schemes.ini", "r")
strColorSchemeData = objFile.read()
objFile.close

# Now, let's use a regular expression to parse


# out all the color scheme names from this file.
# RegExp pattern for color schemes:
# B:"colorSchemeName"=00000044
regexp = re.compile("^[\s\S]*?B\:\"(.*?)\"=\d+$", re.I + re.M)

# Now let's iterate through all the color scheme names and populate
# two dictionaries (hash tables) one indexed by scheme name, the
# other by index of appearance (so that rotating is easier -- by
# knowing where we are currently, it will be easier to find out the
# next one in line):
nIndex = 0
for strColorSchemeName in regexp.findall(strColorSchemeData):
if not strColorSchemeName in g_cColorSchemes:
g_cColorSchemes[strColorSchemeName] = nIndex
g_cSchemeIndices[nIndex] = strColorSchemeName
if not bColorsLoadedFromConfig:
bColorsLoadedFromConfig = True
nIndex += 1
else:
MsgBox("Unable to locate Color Schemes.ini file in config path: " +
strConfigPath)
return

else:
# Unable to read existing color schemes from the Color Schemes.ini file
MsgBox(
"Error: Unable to determine location of current configuration " +
"path in order to load existing color schemes.\r\n\r\n" +
"You're probably running SecureCRT with a configuration folder " +
"that exists where SecureCRT.exe lives, or you're running with " +
"the /F <special_path_to_config_folder> option, and we can't " +
"cope with that in SecureCRT yet.\r\n\r\n" +
"Script is unable to proceed further.")
return

# Get a reference to the current script tab so that we can be "tab safe"
objCurTab = crt.GetScriptTab()

# Get the current color scheme setting:


strCurColorScheme = objCurTab.Session.Config.GetOption("Color Scheme")
if strCurColorScheme in g_cColorSchemes:
# Use the color scheme's index (position as defined in the .ini
# file) instead of its name when performing rotations.
nIndex = g_cColorSchemes[strCurColorScheme] + 1

# This is where we rotate from the bottom of the list back


# up to the top. Since our hash table (dictionary) has indexes
# at each value, we'll simply start at 0 (zero) if our index is
# past the end of the index of the last color scheme defined.
if nIndex >= len(g_cColorSchemes):
nIndex = 0
else:
# start at the beginning of the list if somehow the session config
# has an unknown/non-existent color scheme:
nIndex = 0

# Convert the index we have into a color scheme name


strNewColorScheme = g_cSchemeIndices[nIndex]

objCurTab.Session.Config.SetOption("Color Scheme", strNewColorScheme)

# note also that if your session is currently configured to use ANSI


# color, the "Use color Scheme" option must be enabled (or else ANSI
# defined fg/bg will be used).
if objCurTab.Session.Config.GetOption("ANSI Color"):
strOption = "Color Scheme Overrides Ansi Color"
if not objCurTab.Session.Config.GetOption(strOption):
objCurTab.Session.Config.SetOption(strOption, True)
# Reflect the chosen color scheme name in the status bar
# so the operator can know it by name.
crt.Session.SetStatusText(strNewColorScheme)

main()

Das könnte Ihnen auch gefallen