My Mac mini is connected to a full mechanical keyboard such as there's a Home and End key.
I created two Global Macro when pressing Home/End to go beginning and end of of the line.
However, in Safari, I want Home to behave differently e.g. going to Top and Bottom of page. No problem, I created macros too.
I have two problems:
In Safari, when pressing Home, a conflict palette appear. Ok, I can live with this though it is an inconvenience.
If I am using Safari to edit text e.g. Gmail or Discourse forum such as this, how can I tell the macro to behave differently on the same app but different context/use case?
If I understand correctly, you want to find out if the cursor is in a text field or not, and then have your macro do the right thing for the situation.
In that case, you could test for a text field (area) with an AppleScript:
tell application "System Events"
set frontApp to name of first process whose frontmost is true
tell application process frontApp
set theRole to role of value of attribute "AXFocusedUIElement"
return theRole = "AXTextArea"
Here an example macro:
Test for Text Area.kmmacros (4.6 KB)
This seems to work in most applications, though in Safari it is pretty slow, at least for me.
Yes, way better for Safari with a JS condition:
Test for Text Area in Web Browser.kmmacros (4.4 KB)
Thanks. You gave me a good idea. Instead of having two macros and therefore needing a conflict palette, I can use your code to check if I’m in edit mode. If yes, then Home goes to beginning of line. Otherwise, it jumps to top of window. Elegant. Thank you!
The Apple Script works well for me. I appreciate it very much. But, it’s also slow in Drafts and TextEdit so I’m wondering if there’s another option to this. But it does it job well albeit a second delay.
The AppleScript UI script ("tell application "System Events"… etc.") is a "catch all" approach that should work for most/many applications even if they don't have their own AppleScript dictionary.
If an application comes with a dedicated AppleScript dictionary (e.g. TextEdit; for Drafts, I don't know), it's always better to use that instead of generic UI scripting. Of course, this means that you have to make a special macro for that application (or branch with conditionals within the macro).
Yeah, I figure that since I spent more time in apps like Drafts and TextEdit, I first check whether Safari is the front most app. If it is, then only check if it's Text Area. That helps tremendously!
Edit: in case anyone is interested, here's the macro, thanks 100% to @Tom
Home Key.kmmacros (7.5 KB)