Can I check if I am in a text field?

Hello there,
I would like to use an if/else macro to check if I currently have an active text cursor in a text field (so, if I would start typing something, I would enter something into that textfield). Is this somehow possible (Applescript maybe?).

Thank you!
trych

Assuming you don’t mind losing any contents already therein how about…

  1. Cmd+A
  2. Cmd+X
  3. Type something with a KM step
  4. Cmd+A
  5. Cmd+X
  6. Compare what’s on the clipboard to that something you typed in Step 3

as a rough outline.

If you wanted to keep the existing contents a smallish modification would probably work.

What app?

If it is a web page, you can probably get or set the active field using JavaScript, something like:

document.activeElement

If you need help with a web page, please post an example URL.

This would be specifically for Adobe Lightroom, but I was in the same situation that I wanted this before with other apps (I don’t quite remember which ones), so I wanted to ask in general. So, this is not really on a web page, but rather in apps.

That is gonna destroy a lot of stuff, if the cursor is not in a text field, is it not? I think in most apps that would cut something existing, which might be dangerous, if it can not be properly pasted again.

Often fields/elements in Mac apps have focused property, that is "true" when the field has focus. I don't have lightroom, so I can't test it.

Do you have either UI Browser or Script Debugger?
Both of these are great tools for exploring the UI.

Here is a great macro/script by @ccstone that will list all of the UI elements of the frontmost window. Just activate the Lightroom window of interest, and trigger this macro.

##Macro Library   @Win List UI Elements of Front Window @ccstone @Tool


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/3/360cc0c76ff044a3ef94c5a9c9d1ce6ff2acf2a9.kmmacros">@Win List UI Elements of Front Window @ccstone @Tool.kmmacros</a> (10 KB)
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**

---

###ReleaseNotes

Authored by Christopher Stone Author.@ccstone <scriptmeister@thestoneforge.com>
2016/06/11 06:02 : Created
2016/06/13 15:01 : Modified

**PURPOSE:**

Report the System Events UI Information of the Front Window of the Front app in either TextEdit OR the customized default app for text files on your system.

 MOD by @JMichaelTX:  Change Output to KM Window ##


---

<img src="/uploads/default/original/2X/2/23fcbafda085c2c49d0c0dd0a2980faf237791ce.png" width="458" height="530">

###Example Output
From an Outlook 2011 EMail Compose window
<img src="/uploads/default/original/2X/9/9b289695f5a8c08abd7ae0d8b77d5afcd6c11de2.png" width="678" height="739">

@trych, here's a script for Outllook that maybe you can use as a model for Lightroom:

tell application "Microsoft Outlook" to activate

tell application "System Events"
  
  tell application process "Microsoft Outlook"
    set frontmost to true
    
    tell (first window whose subrole is "AXStandardWindow")
        
      set subjElem to (first text field of splitter group 1 whose description is "Subject:")
      set subjProp to properties of subjElem
      set subjFocused to focused of subjElem
            
    end tell
  end tell
end tell

return subjFocused

It works. If I have clicked in the Subject field, it returns "true", otherwise, "false"

In Script Debugger I can see these properties, which includes the focused property.

1 Like

@JMichaelTX, thanks a lot for your answers.

Yes, I am able to get the reference to the text field via UI Browser and it does indeed have a focused property, the problem however is that the reference constantly changes depending on what sort of panels you have opened in Lightroom. So one time, the path would be something like this:

application "Lightroom"
  window (window 3)
    scroll area (scroll area 1)
      text field (text field 1)

and another time the reference to the very same text field could be

application "Lightroom"
  window (window 8)
    scroll area (scroll area 1)
      text field (text field 34)

So I don’t really know how to reliably target that specific text field. Is there maybe any absolute reference (like an id) for a text field?

Thanks,
trych

Edit: Ok, UI Browser shows me that the text field has a property named identifier with a value of "keywordsEdit". Is there any way to use this information to target the window? I.e. can I somehow look up the window by this value?

Possibily, but I'm such a UI novice that I can't offer any specific help, except for:

  1. Try various whose clauses, like
  • set kwField to first text field whose identifier is "keywordsEdit"
  • You might have to put this in a windows loop (see example script below)
  • Do you know the name/title of the window of this field?
    • if so, then use it.
  • For better help, you may want to post in:
    UI Browser Users Group - Yahoo Groups

Here is an example script for Outlook, where I am searching for the "subject" field in a compose window. It's kinda slow, but it works:

tell application "Microsoft Outlook" to activate

tell application "System Events"
  
  tell application process "Microsoft Outlook"
    set frontmost to true
    
    set winList to windows whose subrole is "AXStandardWindow"
    repeat with oWin in winList
      
      tell oWin
        
        --set index to 1
        --set focused to true
        perform action "AXRaise"
        
        try
          set subjElem to (first text field of splitter group 1 whose description is "Subject:")
          if (subjElem ≠ {}) then exit repeat
        end try
        
      end tell
      
    end repeat
    
    set subjProp to properties of subjElem
    set subjFocused to focused of subjElem
    
  end tell
end tell

return subjFocused

1 Like

@JMichaelTX Thanks for your script, this helped me to get it to work. I am just not sure yet, if this really runs fast enough for my purposes, as this has to loop over quite a few windows and text fields. I will probably need this for several macros in the future, so I will give this a try and test it. Here is the final Applescript that I came up with:

activate application "Adobe Lightroom"
tell application "System Events"
    tell process "Lightroom"
        set winList to windows whose role is "AXWindow"
        
        repeat with oWin in winList
            tell oWin
                try
                    if (first button whose title is "Sync Metadata") ≠ {} then
                        set metadataWindow to oWin
                        exit repeat
                    end if
                end try
            end tell
        end repeat
        
        tell scroll area 1 of metadataWindow
            
            repeat with tf in text fields
                try
                    if value of attribute "AXIdentifier" of tf is "keywordsEdit" then
                        if focused of tf is true then
                            return "TRUE"
                        else
                            return "FALSE"
                        end if
                    end if
                end try
            end repeat
            
        end tell
    end tell
end tell

Thanks,
trych

P.S.: In a cases like this, where I managed to solve my problem with the help of others, which answer is the preferred one to check as the one that answered my question? The one by JMichaelTX that helped me find the final solution for myself? Or the one by myself, where I post my final script?

1 Like

Great! I'd glad you got something that works.

One way to optimize your script is to determine a way to uniquely ID the window of interest.
Once you have found the window in your script, you might output its properties:
properties of oWin

Maybe the window will have a name or ID that you can use.

I don't care about the credit, I just want future readers to easily find the answer.
So, just check your own post where you have posted the solution.

Yes, that's what I tried at first, but the window does not have an ID or name. The only way to reliably identify it, was by checking, if it has the "Sync Metadata" button, which seems a bit clumsy, yes.

Perfect, will do that.

trych