![]() ![]() ![]() Hammerspoon is basically a software-bridge between the lower levels of macOS (using the Accessibility API in macOS) and a Lua scripting engine. I recently discovered Hammerspoon, as it's popped up a few places as being the next big thing for automation geeks on macOS. We still need to trigger something on pressing the F18 button. ![]() This gets us part of the way, but not quite. Karabiner-Elements is a new set of sub commands from the original Karabiner, for now basically a scaffold for the next version of Karabiner.Įven though it doesn't (yet!) support firing multiple keystrokes on a single key, Karabiner-Elements is not all bad, as it allows for rebinding Caps Lock to some F-keys not usually used on the Mac keyboard (Specifically F13-20), which will allow for remapping Caps Lock to fire something which you wouldn't normally use.Īfter installing Karabiner-Elements, open it up and setup the following: My muscle memory caused some issues on this after upgrading to macOS Sierra, as I've grown kinda used to using Caps Lock for OmniFocus, suddenly requiring me to skip that habit. However, Karabiner no longer works in macOS Sierra and the alternative Karabiner-Elements has yet to support multiple keystrokes for a single key. As you may recall, I used Karabiner and Seil to remap my Caps Lock previously, allowing me to use it for Alfred or OmniFocus. Local reportCallback = function (position ) Worst case, the cursor is all the way to the right of the field and cannot select anything to the right, so it times out at 50ms trying to wait for a field update.įunction getCurrentPositionAsync (callbackFn ) For example, it worked fairly fast in the input field in the Hammerspoon console (10ms to get the selection, 4ms restore time), but was much slower in Slack (Electron) and started doing weird things with the cursor. I’ll leave the code here, but I was really unable to make it stable across multiple fields. This takes around 20ms on my computer to wait for the clipboard to update with the copied value.Īnother idea I had was to delete all the text to the right of the cursor, check the new length of the input, and restore the text by hitting cmd + z and clearing the selection with left arrow. i ( "Got contents in millseconds: ", diffMs ) Local diffMs = (endTimeNs - startTimeNs ) / 1000000 set the cursor selection at position 0, with a selection length of 3ĬurrentElement : setSelectedTextRange (, "c", 0 ) systemWideElement ( )ĬurrentElement : setValue ( "new text value" ) You can update these values as well: local systemElement = ax. ![]() Local position = currentElement : attributeValue ( "AXPosition" ) with coordinates of the field on the screen. You can even figure out where the input is being drawn on the screen, in case you want to overlay something on top of the field: - Returns an object like: Local selection = currentElement : attributeValue ( "AXSelectedTextRange" ) and `loc` is where the cursor currently is in the text, between `` where `len` is the length of the current selection (0 if no text is selected) Local textLength = currentElement : attributeValue ( "AXNumberOfCharacters" ) Local value = currentElement : attributeValue ( "AXValue" ) You can ask it for the current value, length, and selection range: local systemElement = ax. Local currentElement = systemElement : attributeValue ( "AXFocusedUIElement" ) You can easily grab the currently focused input field: local systemElement = ax. To use it, follow the install instructions and require it in your scripts: local ax = require ( "hs._asm.axuielement" ) You can get cursor position, selection range, the value of the field, and even character index ranges for each wrapped line in a multi-line input, which helps when doing things like linewise Vim has written an awesome library for interacting with the Accessibility API from Hammerspoon called hs._asm.axuielement, which I’ve made extensive use of. Mac’s Accessibility API is really awesome, and in the best case, exposes more data than you could ever need about the currently focused input field. If anyone has any other clever ideas or techniques, I would really love to hear about them–please email me at and we can chat! The Accessibility API: almost perfect # Trying to work around when the Accessibility API fails to get a cursor position.Using the MacOS Accessibility API, and caveats/hacks.I’ve tried a number of different ways to achieve this, and I want to document those experiments here, along with the pros and cons. Mode to every input in the Mac operating system.Ī big part of this library’s success is being able to read the current value and cursor position of the currently focused input field from Hammerspoon. In my spare time, I work on a library called Can we get around the Electron selection problem?.Retrieving input field values and cursor position/selection with Hammerspoon ![]()
0 Comments
Leave a Reply. |