JolinM Posted October 7, 2020 Posted October 7, 2020 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" activate new document tell application "System Events" to keystroke ("{query}") end tell tell app "System Events" to keystroke "k" using command down T hanks!
deanishe Posted October 7, 2020 Posted October 7, 2020 23 minutes ago, JolinM said: The problem I encounter is that the diacritics aren't preserved. You can’t keystroke non-ASCII with AppleScript. You need to put the text on the pasteboard and paste it instead.
JolinM Posted October 7, 2020 Author Posted October 7, 2020 Thanks @deanishe or the quick answer. I now have this, but encounter the same problem… 😕 set the clipboard to "{query}" tell application "Antidote 10" activate new document tell application "System Events" to keystroke (the clipboard) end tell tell app "System Events" to keystroke "k" using command down
deanishe Posted October 7, 2020 Posted October 7, 2020 45 minutes ago, JolinM said: I now have this, but encounter the same problem… Well, yeah. The problem is keystroke and you’re still using it.
JolinM Posted October 7, 2020 Author Posted October 7, 2020 That helps; I’m progressing! I now have a paste that keeps the formatting, found here. It works in my workflow (action clic) Only thing is, I don't know how to chain them. Any help would be appreciated! Here’s my current workflow
deanishe Posted October 7, 2020 Posted October 7, 2020 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. JolinM 1
JolinM Posted October 7, 2020 Author Posted October 7, 2020 (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 October 7, 2020 by JolinM
vitor Posted October 7, 2020 Posted October 7, 2020 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) delay(0.01) }) } type_text('{{whatever you want}}') deanishe 1
deanishe Posted October 7, 2020 Posted October 7, 2020 (edited) 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 e´, 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 October 7, 2020 by deanishe
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now