python tool for ultraedit

Mark S Pryor marks.pryorSHRUB at CHENEYverizon.net
Wed Jan 21 08:32:05 CET 2004


dear c.l.p,

I'm posting a Py script to change the wordfile
from inside the UE editor. Define this into
the adv. tool menu.

I found 100 posts in groups.google discussing
Python and UltraEdit.

Since UltraEdit is only happy with 10 parts in
a Wordfile, it is convenient to have multiple
wordfiles, with no more than 10 styles per wf.

To keep a wf associated to a script, add a tag
in the comment header:

   """
    wordfile: e:/path/mywordfile.txt
   """
http://mysite.verizon.net/res1ur2j/SwitchWF.zip

------ begin SwitchWF.py ------------
#!e:/Python22/python
"""
 script: SwitchWF.py
 keywords: enum window win32api handle callback
 author: tlviewerSHRUB at CHENEYyahoo.com
 relatedDoc=Selkey.png
 wordfile: "C:/Progra~1/UltraEdit/WORDFILE.TXT"  3

 UE macro to highlight the wf tag
 ------- start macro ------
  InsertMode
  ColumnModeOff
  HexOff
  UnixReOn
  GotoLine 1
  Find RegExp "\swordfile:"
  IfFound
  Find RegExp "\S"
  StartSelect
  Key END
  EndSelect
  EndIf
 ------ end macro ---------

 synopsis:  (2 arguments)
 python SwitchWF.py "path to new wf" Ind-of-Ext-SynHighlight

"""
import os, sys
import win32api as ap
import win32gui as wi
import win32ui as ui
import win32con as wc
import win32clipboard

myhwnd = 0
myti = ''
mycl = ''

# function callback
####################
def Callback(hwnd, skip):
 global myhwnd
 global myti
 ti = wi.GetWindowText(hwnd)
 if ti.find(skip) >=0:
  myhwnd = hwnd
  myti = ti
  print myhwnd, "uedit32 found",skip
  return 0
 else:
  #print myhwnd, ti
  return 1

# function pysendkey
#######################
"""
 pysendkey is a pure-python replacement for the
    WSHShell SendKeys method!
"""
def pysendkey( keycode ):
 dwFlags= wc.KEYEVENTF_EXTENDEDKEY
 bVk = keycode
 ap.keybd_event( bVk, bScan, dwFlags, 0)
 dwFlags= wc.KEYEVENTF_EXTENDEDKEY | wc.KEYEVENTF_KEYUP
 ap.keybd_event( bVk, bScan, dwFlags, 0)

# function menu_activate
########################
"""
 use below for activating sub menu items of the
  UE main menu.
 mid = main menu id
 iid = sub item id

"""
def menu_activate(myhwnd, mymenu, mid, iid ):
 pya = mymenu.GetSubMenu(mid)
 print pya.GetHandle()
 #vid = mymenu.GetMenuItemID(mid)
 #res = wi.SendMessageTimeout( myhwnd,wc.WM_COMMAND, vid, 0, 2, 75 )

 # configuration menu item id
 cid = pya.GetMenuItemID(iid)
 print "cid=", cid

 #check for OS, and pack the ID if necessary
 env = os.environ
 if env['WINDIR'].find('WINNT'):
  print "Got NT"
 else:
  print "Win98 pack the ID"
 res=wi.SetForegroundWindow(myhwnd)
 try:
  res = wi.SendMessageTimeout( myhwnd,wc.WM_COMMAND, cid, 0, 2, 75 )
  print "res=", res
 except:
  print "done - error "
  # ok
 ap.Sleep(50)


# Main
########################
try:
  wi.EnumWindows(Callback, "UltraEdit-32")
except:
 print "error", myhwnd


print myti, myhwnd

# get a Python handle object
# enumwindows was needed above since FindWindowEx is broken in Python 2.2-
#   must use the --exact-- title or fail (not the case in VB)
pyhw = ui.FindWindowEx( None, None, None, myti )

pymenu = pyhw.GetMenu()

# when called from the UE tool menu, we need a valid selection
if sys.argv[1] == '%sel%':
 sys.exit(0)

menu_activate(myhwnd, pymenu, 9, 0)

# get the hwnd of the UltraEdit Configuration dialog
#hwuc = cM.FindWindowLike( cl, "UltraEdit Configuration" )
hwuc = wi.FindWindowEx(0, 0, "#32770", "UltraEdit Configuration" )
print "ue config=", hwuc

# get handle to the child systabcontrol32 class
hwtab = wi.FindWindowEx( hwuc , 0, "SysTabControl32", None )
print hwtab

# open the 7 item via message
# TCM_FIRST= &h1300
#Const TCM_SETCURFOCUS As Long = (TCM_FIRST + 48)
# not found in Win32Con
TCM_SETCURFOCUS = 0x1330

SYNTAX_HIGHLIGHT = 7
wi.PostMessage( hwtab, TCM_SETCURFOCUS, SYNTAX_HIGHLIGHT , 0)

# get the myhwnd to the SynHigh dialog
hwsh = wi.FindWindowEx( hwuc, 0,  "#32770", "Syntax Highlighting")
print "SynHigh myhwnd=",hwsh

# get the myhwnd to its only edit box
hwedit = wi.FindWindowEx( hwsh, 0,  "Edit", None)

res = wi.SetForegroundWindow(hwedit)

# set the focus here
lres = wi.SendMessageTimeout(hwedit, wc.WM_SETFOCUS , None , None , 2 , 50 )

## press <SHIFT-END> down
#########
dwFlags= wc.KEYEVENTF_EXTENDEDKEY
bVk = wc.VK_SHIFT
bScan = 1  # or 0
ap.keybd_event( bVk, bScan, dwFlags, 0)

bVk = wc.VK_END
bScan = 1  # or 0
ap.keybd_event( bVk, bScan, dwFlags, 0)
########### end down keys#################

# up the two keys
################
dwFlags= wc.KEYEVENTF_EXTENDEDKEY | wc.KEYEVENTF_KEYUP
ap.keybd_event( bVk, bScan, dwFlags, 0)

bVk = wc.VK_SHIFT
ap.keybd_event( bVk, bScan, dwFlags, 0)
########### end up keys ##################

# press BACKSPACE up , then down
pysendkey( wc.VK_BACK )

####################################

#print sys.argv[1]

if len(sys.argv)>1:
 win32clipboard.OpenClipboard()
 win32clipboard.EmptyClipboard()
 win32clipboard.SetClipboardText(sys.argv[1])
 win32clipboard.CloseClipboard()

 # paste the clipboard, then push enter
 #
 lres = wi.SendMessageTimeout(hwedit, wc.WM_PASTE, 0 , 0 , 2 , 50 )

 pysendkey( wc.VK_RETURN )
 print "done"
 print sys.argv[1]
# if len(sys.argv)>2:
#  # get the View menu
#  menu_activate( myhwnd, pymenu, 5, int(sys.argv[2]))
#  ap.Sleep(50)
------ end script ------------

good luck,
Mark Pryor








More information about the Python-list mailing list