Is there AppleScript for determining if a text document is being edited

I have a macro in Keyboard Maetro that makes edits to the current line of text in an RTF document (moves the cursor to the beginning of the line, adds text, etc.). I want to prevent that macro from firing unless a text document is actively being edited (the cursor is “live”).

I was thinking of using an AppleScript test to return this state, but am not sure how or if it is possible.

I know “content record” will tell me if a document is open:

if not (exists content record) then error "Please open a document."

But is there a similar way to test if the cursor is “live” within that document? I tried “current line”:

set this_line to the current line of current tab of think window 1

but that seems to remember the last selected position in the document, even if the cursor is not alive (e.g. if you place the cursor or line 10, select another document, then return to the first without placing the cursor, it will return “10”)

Thanks,
Dan

There isn’t a document property for this. Development would have to assess including that.
In the interim, this may be of a little use…

tell application "System Events" to tell application process "DEVONthink"
	if enabled of (menu item "Save" of menu "Data" of menu bar 1) then say "Document is being edited"
end tell

This actually checks whether a document was modified and not yet saved.

What about using the menu item Go > Move Focus to Preview (or its shortcut) to ensure that the document has the focus?

Yes, that would work as a failsafe (and prevent my macro from editing the file name instead of the content).

Vamping off of this idea, I remembered that Keyboard Maestro has ability to test if a menu item is enabled or not. And the text formatting menu items are only enabled if the document is actively being edited. So I added the following to my KM macro, and it works!

Thank you both for your help!

2 Likes

You’re welcome :slight_smile: