headers in md: script to add something at the row beginning

42 Aufrufe
Direkt zur ersten ungelesenen Nachricht

Duns

ungelesen,
09.05.2022, 09:57:1309.05.22
an autokey-users
I have found Zettlr a very good app, but it doesn't allow to customize shortcuts.
Therefore I wish use autokey to add a string of text at the beginning of a row (tipically a "#" for "h1", or "##" for "h2" and so on).
I guess that I need a script. But, could you help me to the right code? 
Basically with these steps:
  1. go to the beginning of a line (row)
  2. remove there any (previous) "#", if exists
  3. put there the desired string ("#", or "##", and so on)
Thank you!

Little Girl

ungelesen,
09.05.2022, 20:04:4509.05.22
an autokey-users
Hey there,

This sounds like something that AutoKey would be perfect for, but we'll need a bit more information. How will AutoKey be getting the text to work on? Will you be selecting it, copying it, or should the script read a text file or some other kind of file to get it? Also, removing previous hash marks at the beginning of any line will be easy, but AutoKey will need to be told how to identify which lines to put a single hash mark on, which lines to put a double hash mark on, etc. What will AutoKey be looking for that will let it know that? Oh, and if you're copying the text or if AutoKey is reading it in from a file, what would you like AutoKey to do with it once it's done?

Duns

ungelesen,
10.05.2022, 05:56:1310.05.22
an autokey-users
No: not a selection of any text. Only to go at the beginning of the _line_ where there is the _cursor_.  
And the hash mark are only these at the beginning of that line (without interruption: "##" should be removed , in # #anytext should be removed only "#" and kept "#anytext").
Thank you!

Little Girl

ungelesen,
10.05.2022, 16:55:1010.05.22
an autokey-users
Hey there,

Not having seen all of the possible lines this script might encounter, I can't promise that this script will take care of them properly, but it will change the # #anytext line to #anytext and will also change the ## ##anytext line to ##anytext. It gets rid of everything at the beginning of the line up to and including the first hash-mark with a space after it. If it encounters a line that doesn't contain a hashmark followed by a space, it doesn't change that line.

I tried for quite some time to figure out how to get a selection with AutoKey without actually getting it on my own first and I failed miserably (I really thought that getting it to press and release the Home key followed by pressing and releasing the Shift and End keys at the same time would do it, but it doesn't). Perhaps someone else will figure out how to do that part. This might be a good time for me to go into the issues and see if anyone has made a wish for a double-click or triple-click for the mouse, because we don't currently have those and they would do this nicely.

Anyway, as a result, you won't just be able to put your cursor in the line that you want edited. You'll have to select that line, either by triple-clicking anywhere on it or by putting your cursor at the beginning of the line and pressing Shift and End at the same time, etc. Once you've got the selection, you can run the AutoKey script. Here's what I've got so far:

# Get the current selection and put it into a variable:
target = clipboard.get_selection()

# If they exist, remove leading hashmark(s) followed by a space from the selection:
target = target.split("# ", 1)[-1]

# Paste the edited selection into the active window:
keyboard.send_keys(target)

Duns

ungelesen,
11.05.2022, 01:53:1911.05.22
an autokey-users
Unfortunately, by selecting the whole line and then running the script, nothing happens, unless the cursor at the end of the (now unselected) line.

Duns

ungelesen,
11.05.2022, 02:06:3511.05.22
an autokey-users
Maybe we could try with a less difficult task: replace "##" in (any point of) a line, with "#" (and then another script replacing "#" with "##").

jack

ungelesen,
11.05.2022, 02:20:5711.05.22
an autoke...@googlegroups.com, Little Girl
>I tried for quite some time to figure out how to get a selection with AutoKey without actually getting it on my own first and I failed miserably (I really thought that getting it to press and release the Home key followed by pressing and releasing the Shift >and End keys at the same time would do it, but it doesn't)


try this:

keyboard.send_keys("<home>")
time.sleep(0.2)

keyboard.send_keys("<shift>+<end>")
time.sleep(0.2)

selText = clipboard.get_selection()
time.sleep(0.2)

dialog.info_dialog(title='Clipboard contents',
    message=selText)



jack
Deep down I'm very shallow
--
You received this message because you are subscribed to the Google Groups "autokey-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to autokey-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/autokey-users/1c5b7f5f-1960-49a0-adbc-82f9ca264186n%40googlegroups.com.

Die Nachricht wurde gelöscht
Die Nachricht wurde gelöscht

Little Girl

ungelesen,
11.05.2022, 09:25:4111.05.22
an autokey-users
Hey there,

Jack, that was amazing. I don't know why I keep forgetting about time.sleep() when it solves so many little issues. Thank you for a timely reminder. Hopefully it will now be hammered firmly into place.

Okay, here's the first working script that doesn't need you to make a selection and will process the current line that the cursor is on. Note that this one will turn # # foo into # foo or it will turn # # foo into # foo.

"""
This script removes everything up to and including the first hashmark followed by a space that it encounters on the current line.
"""
# Move the cursor to the beginning of the current line of text:
keyboard.send_keys("<home>")

# Delay for a moment:
time.sleep(0.2)

# Select everything to the end of the line:

keyboard.send_keys("<shift>+<end>")

# Delay for a moment:
time.sleep(0.2)


# Get the current selection and put it into a variable:
target = clipboard.get_selection()

# Remove everything up to and including the first occurrence of the specified characters from the selection:

Little Girl

ungelesen,
11.05.2022, 09:30:2411.05.22
an autokey-users
Hey there,

Here's the next script that replaces double hasmkarks with single hashmarks:

"""
This script replaces all double hashmarks that it encounters on the current line with single hasmkarks.

"""
# Move the cursor to the beginning of the current line of text:
keyboard.send_keys("<home>")

# Delay for a moment:
time.sleep(0.2)

# Select everything to the end of the line:
keyboard.send_keys("<shift>+<end>")

# Delay for a moment:
time.sleep(0.2)

# Get the current selection and put it into a variable:
target = clipboard.get_selection()

# Replace all double hashmarks with single hashmarks in the selection:
target = target.replace("##", "#")

Little Girl

ungelesen,
11.05.2022, 10:22:2811.05.22
an autokey-users
Hey there,

And here's the next script that replaces single hasmkarks with double hashmarks:

"""
This script replaces all single hashmarks that it encounters on the current line with double hasmkarks.

"""
# Move the cursor to the beginning of the current line of text:
keyboard.send_keys("<home>")

# Delay for a moment:
time.sleep(0.2)

# Select everything to the end of the line:
keyboard.send_keys("<shift>+<end>")

# Delay for a moment:
time.sleep(0.2)

# Get the current selection and put it into a variable:
target = clipboard.get_selection()

# Replace all single hashmarks with double hashmarks in the selection:
target = target.replace("#", "##")

Duns

ungelesen,
11.05.2022, 12:02:5011.05.22
an autokey-users
OK, your code works to transform a "##" to "#", and viceversa.  Thank you!
Now, if possible, the best would be also a script to add "#" or "##" at the beginning of a line that doesn't have one.  
And, if I don't ask too much, merge the two scripts :)

Little Girl

ungelesen,
11.05.2022, 13:22:0811.05.22
an autokey-users
Hey there,

I'm glad those work. No problem adding a bit more to the code. I don't trust Google not to mess it up, though, since the code now has indented sections in it and Google got rid of code blocks. I pasted each of them into the code-sharing site that the Python IRC channel uses so that the indentations will be preserved.

This one replaces double hashmarks with single hashmarks or adds a single hashmark at the beginning of the line if it doesn't have one:

https://bpa.st/2TTQ

This one replaces single hashmarks with double hashmarks or adds a douible hashmark at the beginning of the line if it doesn't have one:

https://bpa.st/54UA

Note that those scripts will expire from that pasting site in one month, but I'll be keeping a copy of them if they're ever needed again.

Duns

ungelesen,
11.05.2022, 15:02:1711.05.22
an autokey-users
Perfect! It work like a charm. (I had only to add a space at right of #)  
Thank you very much!

Little Girl

ungelesen,
11.05.2022, 16:53:0211.05.22
an autokey-users

Hey there,

I'm glad you got it working the way you wanted it to. I'm also glad you asked for this. Until now, I had always relied on being the one to make the selection or copy the text rather than letting the script do the work, so this little adventure taught me something that will surely be useful in many ways from now on.



Allen antworten
Antwort an Autor
Weiterleiten
0 neue Nachrichten