Skip to content

Commit

Permalink
Getwindowinfo branch (#12)
Browse files Browse the repository at this point in the history
* revisiting autohotkeyactions.py, beginning to work again...
* extenvvars.py tidied up, the "remember" functionality across calls removed,
  now all directory settings in natlinkstatus are found with %natlinkdir% or even %natlink% as directory content.
  also %unimacrouser% finds the %unimacrouserdirectory% setting (both work)
* bring unimacroactions.py in clipboard functions under try finally  for CloseClipboard() (Issue 11, which also blocked the clipboard at times)
* small improvement to inivars, giving less errors...
* edit some documentation in sendkeys.py
  • Loading branch information
quintijn authored Feb 21, 2024
1 parent a984904 commit e36bc9c
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 236 deletions.
35 changes: 25 additions & 10 deletions src/dtactions/autohotkeyactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,28 @@ def do_ahk_script(script, filename=None):
filename = filename or 'tempscript.ahk'
if not ahk_is_active():
print('ahk is not active, cannot run script')
return
return 0
#print 'AHK with script: %s'% script
if script.strip().endswith('.ahk'):
script = script.strip()
scriptPath = ahkscriptfolder/script
if scriptPath.is_file():
call_ahk_script_path(scriptPath)
return 1
print(f'not a valid filepath for AHK script: "{str(scriptPath)}"')
return 0

scriptPath = ahkscriptfolder/filename

if isinstance(script, (list, tuple)):
script = '\n'.join(script)
if not script.endswith('\n'):
script += '\n'

with open(scriptPath, 'w') as fp:
with open(scriptPath, 'w', encoding='utf-8') as fp:
fp.write(script)
call_ahk_script_path(scriptPath)
return 1

def call_ahk_script_path(scriptPath):
"""call the specified ahk script
Expand All @@ -96,7 +107,7 @@ def call_ahk_script_path(scriptPath):
else:
raise ValueError(f'autohotkeyactions, call_ahk_script_path: path should end with ".ahk", or ".exe"\n path: {scriptPath}')
if result:
print('non-zero result of call_ahk_script_path "%s": %s'% (scriptPath, result))
print(f'non-zero result of call_ahk_script_path "{scriptPath}": {result}')

ProgInfo = collections.namedtuple('ProgInfo', 'progpath prog title toporchild classname hndle'.split(' '))

Expand Down Expand Up @@ -150,7 +161,7 @@ def getProgInfoScript(info_file):
def getProgInfoResult(info_file):
"""extract the contents of the info_file, and return the progInfo
"""
with open(info_file, 'r') as fp:
with open(info_file, 'r', encoding='utf-8') as fp:
progInfo = fp.read().split('\n')

# note ahk returns 5 lines, but ProgInfo has 6 items.
Expand Down Expand Up @@ -289,7 +300,8 @@ def ahkBringup(app, filepath=None, title=None, extra=None, waitForStart=1):

## do the script!!
do_ahk_script(script)
message = open(ErrorFile, 'r').read().strip()
with open(ErrorFile, 'r', encoding='utf-8') as fp:
message = fp.read().strip()
if message:
return message

Expand Down Expand Up @@ -468,7 +480,7 @@ def GetForegroundWindow():
script = script.replace('##hndlefile##', str(HndleFile))
do_ahk_script(script, filename="getforegroundwindow.ahk")

with open(HndleFile, 'r') as fp:
with open(HndleFile, 'r', encoding='utf-8') as fp:
gotHndle = fp.read().strip()
try:
if gotHndle:
Expand Down Expand Up @@ -521,7 +533,8 @@ def SetForegroundWindow(hndle):
mess = f'Error with SetForegroundWindow to {hndle}, InfoFile cannot be found'
return mess

winHndle = open(ProgInfoFile, 'r').read().strip()
with open(ProgInfoFile, 'r', encoding='utf-8') as fp:
winHndle = fp.read().strip()
if winHndle:
try:
winHndle = int(winHndle)
Expand Down Expand Up @@ -613,15 +626,15 @@ def clearErrorMessagesFile():
return the path of the ErrorMessagesFile
"""
ErrorFile = ahkscriptfolder/"errormessagefromahk.txt"
with open(ErrorFile, 'w') as f:
with open(ErrorFile, 'w', encoding='utf-8') as f:
f.write('\n')
return ErrorFile

def readErrorMessagesFile():
"""get the error messages if any
"""
ErrorFile = ahkscriptfolder/"errormessagefromahk.txt"
with open(ErrorFile, 'r') as f:
with open(ErrorFile, 'r', encoding='utf-8') as f:
mess = f.read()
if mess.strip():
return f'autohotkeyactions.ahkBringup failed:\n===={mess}'
Expand Down Expand Up @@ -724,7 +737,9 @@ def test():
Result = ahkBringup("notepad")
print(f'\nresult of ahkBringup("notepad"):\n{repr(Result)}')
if Result.hndle:
killWindow(Result.hndle)
killWindow(Result.hndle, silent=False)
Result = do_ahk_script('showmessageswindow.ahk')
pass



Expand Down
16 changes: 10 additions & 6 deletions src/dtactions/sendkeys.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
and then in a function:
:code:`sendkeys("keystrokes")`
This module now adopts the Dragonfly :code:`action_key` module,
so `"{alt+w}"` is (in the function) converted to `"a-w"` etc.
Optionally, you can also use sendsystemkeys, which is implmented via Dragon SendSystemKeys (via natlink.execScript)
(Quintijn Hoogenboom, 2021-04-04)
Expand All @@ -28,6 +25,8 @@ def sendkeys(keys):
:code:`"{shift+right 4}"`
This functions is similar to the Dragonfly `sendkeys` function, but now works via the Vocola Keys extension.
Tested at bottom of this file interactively...
"""
ext_keys.send_input(keys)
Expand All @@ -47,8 +46,13 @@ def sendsystemkeys(keys):
# sendkeys("{a 3}") #aaa
# sendkeys("x y z ")
# sendkeys("test, test, met komma.{home}")
sendkeys("{ctrl+end}{up 2}{home}{shift+end}{del}this is wrong{shift+left 5}right")
# try:
# natlink.natConnect()
# sendsystemkeys("a{win+e}b")
# finally:
# natlink.natDisconnect()

# sendkeys("{ctrl+end}{up 2}{home}{shift+end}{del}this is wrong{shift+left 5}right")
# ##
"""
"""
Loading

0 comments on commit e36bc9c

Please sign in to comment.