Replace English quotes with French quotes

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mbradbury714
Posts: 5
Joined: 19 Sep 2021, 05:30

Replace English quotes with French quotes

Post by mbradbury714 » 19 Sep 2021, 05:37

Hi, I need a hotkey script that would allow me to select a phrase surrounded by single straight quotation marks and replace it with the same phrase surrounded by French quotation marks with non-breaking spaces after the opening quotation mark and before the closing quotation mark so that
This is a 'test'
would be replaced with
This is a « test »
Can anyone help me out?
Thank you in advance.
Last edited by gregster on 19 Sep 2021, 14:57, edited 1 time in total.
Reason: Topic moved from "AHK v2 Help".
User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Replace English quotes with French quotes

Post by boiler » 19 Sep 2021, 05:59

Assuming you aren't really using v2 of AHK but posted in the v2 section by mistake (because most first-time posters aren't really using v2 even if they post here), I'll provide code for AHK v1 (the latest official release). Run the following script, highlight the text of interest, and press Ctrl+' to replace the quotes:

Code: Select all

^'::
	ClipSave := ClipboardAll
	Clipboard := ""
	Send, ^c
	ClipWait, 1
	if ErrorLevel
		return
	Clipboard := RegExReplace(Clipboard, "'(.*?)'", "«" Chr(0x00A0) "$1" Chr(0x00A0) "»")
	Send, ^v
	Sleep, 500
	Clipboard := ClipSave
return

Note that it would run into trouble with cases like Here's a 'test'. It could be make to handle such cases, but even then it would not be able to correctly address cases like this: Pumpkin pie is 'the members' favorite'. I take it you wouldn't be mixing apostrophes and single quotes like that, however.

If you really are using v2, it would be a good task for you to translate it. If you run into trouble, I can help you translate it.
mbradbury714
Posts: 5
Joined: 19 Sep 2021, 05:30

Re: Replace English quotes with French quotes

Post by mbradbury714 » 19 Sep 2021, 14:48

Thank you so much for your help and sorry for using the wrong forum. I wasn't sure where to post. Anyway, the hotkey works like a charm when I use the English keyboard. The only problem is that I usually use the French keyboard so it doesn't work with the single apostrophe in the hotkey. I tried replacing it with another letter in the hotkey but it didn't work. Can you help?
User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Replace English quotes with French quotes

Post by boiler » 19 Sep 2021, 15:46

What key did you change it to that didn’t work? Please show exactly what you changed the first line to and tell me what keys you pressed on your keyboard.
mbradbury714
Posts: 5
Joined: 19 Sep 2021, 05:30

Re: Replace English quotes with French quotes

Post by mbradbury714 » 19 Sep 2021, 16:43

I tried replacing the first line ^':: with ^q:: because the letter q would be the same on both the English and French keyboards. However, it didn't work and nothing happens after I select the phrase and press on Ctrl-q
User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Replace English quotes with French quotes

Post by boiler » 19 Sep 2021, 17:46

Are you certain you saved the changes to the script and closed other instances before running the new one? And the copied text definitely has straight single quotes as in This is a 'test', not This is a ‘test’? Does the French keyboard produce the same single quote characters as the English keyboard? Can you post the text you are trying to operate on?

Does the hotkey itself work? What does the following script do?

Code: Select all

^q::MsgBox, Hello
mbradbury714
Posts: 5
Joined: 19 Sep 2021, 05:30

Re: Replace English quotes with French quotes

Post by mbradbury714 » 19 Sep 2021, 18:23

Sorry, it does work now in both keyboards. Now if you could just change the script so the hotkey would work with both straight single and double quotes, it would be perfect.
User avatar
boiler
Posts: 16709
Joined: 21 Dec 2014, 02:44

Re: Replace English quotes with French quotes

Post by boiler » 19 Sep 2021, 18:38

Code: Select all

^q::
	ClipSave := ClipboardAll
	Clipboard := ""
	Send, ^c
	ClipWait, 1
	if ErrorLevel
		return
	Clipboard := RegExReplace(Clipboard, "'(.*?)'", "«" Chr(0x00A0) "$1" Chr(0x00A0) "»")
	Clipboard := RegExReplace(Clipboard, """(.*?)""", "«" Chr(0x00A0) "$1" Chr(0x00A0) "»")
	Send, ^v
	Sleep, 500
	Clipboard := ClipSave
return
mbradbury714
Posts: 5
Joined: 19 Sep 2021, 05:30

Re: Replace English quotes with French quotes

Post by mbradbury714 » 19 Sep 2021, 19:04

Thank you so much. It works like a charm :D
Post Reply

Return to “Ask for Help (v1)”