Jump to content

Pasting {query} losses diacritic

Recommended Posts

Hi, I am trying to paste the query in a text document (Antidote 10) to be corrected.

The problem I encounter is that the diacritics aren't preserved.


Imput is:

pourquoi les éléphants ne fonctionnent pas à côté


Output is:

pourquoi les ealeaphants ne fonctionnent pas aa coatea


Current AppleScript goes like this:

tell application "Antidote 10"
	new document
tell application	"System Events" to keystroke ("{query}")
end tell
tell app "System Events" to keystroke "k" using command down




Link to post

Thanks @deanishe or the quick answer. I now have this, but encounter the same problem… 😕

set the clipboard to "{query}"
tell application "Antidote 10"
	new document
tell application	"System Events" to keystroke (the clipboard)
end tell
tell app "System Events" to keystroke "k" using command down


Link to post
10 minutes ago, JolinM said:

I don't know how to chain them


Chain what? I'm not sure what the workflow is supposed to do.


11 minutes ago, JolinM said:

I now have a paste that keeps the formatting


If you're getting the input via Alfred, there is no formatting: Alfred only supports plaintext. If you want to preserve formatting, you should initiate copy and paste yourself.


Your menu-click function is probably overkill: for copy and paste, you can just simulate ⌘C and ⌘V keypresses.

Link to post
Posted (edited)

Yessss, that works! Here’s the final AppleScript related to the clipboard.

set the clipboard to "{query}"

tell app "System Events" to keystroke "v" using command down
tell app "System Events" to keystroke "k" using command down


Thanks for the support @deanishe, appreciate it!

Edited by JolinM
Link to post

For completeness, I’ll add that it is possible for keystroke to type diacritics, but you have to decompose the text: Application('System Events').keystroke('pourquoi les ´el´ephants ne fonctionnent pas `a c^ot´e'.. Characters are typed in sequence as it’s as if AppleScript sometimes can’t “keep up”, so it may even happen that you want to write Example and it comes out as EXample, as if ⇧ weren’t released soon enough. In those cases, it can be useful to add a delay:


const type_text = text_to_write => {
  text_to_write.split('').forEach(character => {
    Application('System Events').keystroke(character)

type_text('{{whatever you want}}')


Link to post

Huh. That’s totally messed up. I can keystroke ß, but not ü, presumably because the latter can be represented by multiple Unicode codepoints (and also a single one), but the former can't. What an awful implementation.


29 minutes ago, vitor said:

you have to decompose the text


In a very non-standard way :(


So, it’s possible to get JXA to properly keystroke accented Latin characters. But decomposed Unicode represents as , while keystroke requires you to enter it as ´e.

That would require some jiggery-pokery with NFD normalisation and combining diacritical marks to reorder the string in the way keystroke expects.


It shouldn’t be too complex to do, but I wonder if keystroke would handle all of those combining marks correctly, or would choke on a bunch of them.


Personally, I think I’d import Foundation and use the ObjC APIs, as in the SO thread I linked above: they tend to work much better for simulating keypresses. Like, if you simulate ⌘V in Foundation while you’ve got your finger on the ⌥ key, you get ⌘V. If you do that in AppleScript, you get ⌥⌘V.


Edited by deanishe
Link to post

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...