Skip to content

Instantly share code, notes, and snippets.

@henryscala
Created January 19, 2022 09:44
Show Gist options
  • Save henryscala/f5de21f84f23a224a6564b31cb72a08e to your computer and use it in GitHub Desktop.
Save henryscala/f5de21f84f23a224a6564b31cb72a08e to your computer and use it in GitHub Desktop.
play the contents in a md file inside Typora
# used to play in the typora application
import pyperclip as pc
import pyautogui as pa
# specify input file before running
srcFileName=r"C:\tmp\pyautogui-play\grok-determinant.md"
# srcFileName=r"C:\tmp\pyautogui-play\test.md"
srcFile = open(srcFileName,mode='r', encoding='utf-8')
allFileLines = srcFile.readlines() # note, the '\n' is kept in each line
allFileLines = [ line.strip() for line in allFileLines]
srcFile.close()
# use pa.position() to get the position before running
x = 1276
y = 258
pa.PAUSE = 0.2 #pause seconds before each operation
# move the mouse to upper left corner of the screen to exit running of the program
def play_at_pos(x,y,lines):
pa.moveTo(x,y)
pa.click()
is_begin_code_block = False
is_begin_math_block = False
is_just_closing_a_block = False
is_enter_just_pressed = False
for line in lines:
if line == '$$':
is_begin_math_block = not is_begin_math_block
if is_begin_math_block: # because typora automatically add the closing '$$'
#print("enter due to before beginning math block")
if not is_enter_just_pressed:
pa.hotkey('enter')
pa.write('$$')
print("enter due to after beginning math block")
pa.hotkey('enter')
continue
else:
pa.hotkey('backspace')
pa.hotkey('down') # get out of the math block
pa.hotkey('down')
is_just_closing_a_block = True
continue
if line == "```":
is_begin_code_block = not is_begin_code_block
if is_begin_code_block: # because typora automatically add the closing '```'
#print("enter due to before beginning code block")
if not is_enter_just_pressed:
pa.hotkey('enter')
pa.write("```")
print("enter due to after beginning code block")
pa.hotkey('enter')
continue
else:
pa.hotkey('backspace')
pa.hotkey('down') # get out of the code block
pa.hotkey('down')
is_just_closing_a_block = True
continue
for _,char in enumerate(line):
#print('char=',ord(char))
pc.copy(char)
pa.hotkey('ctrl','v')
if is_begin_code_block or is_begin_math_block:
print("enter due to inside block")
pa.hotkey('enter') # enter if it is inside the block
elif line == '' and is_just_closing_a_block:
is_enter_just_pressed = False
pass
elif line == '':
# enter if it is blanck line
print("enter due to blank line")
pa.hotkey('enter')
is_just_closing_a_block = False
is_enter_just_pressed = True
else:
is_enter_just_pressed = False
play_at_pos(x,y,allFileLines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment