Detect whether a document has been Edited since last saved?

Wondering if KM can detect whether a document is in an Edited/Unsaved state?

For Example:

Title of front window in TextEdit currently looks like this:

I tried this:

But to no avail -- it returns false rather than true. (Meaning, I assume, that '--Edited' is not technically part of the window title.)

If anyone can tell me whether or not there is something in KM that can detect the document's Saved/Edited state, I would very much appreciate it. Thanks.

The only way I can see would be a screen search, which wouldnā€™t work well since you could have multiple edited windows visible.

What about checking if the Save menu is enabled or not?

Yes, you would think that would work, but TextEdit at least leaves the Save menu permanently enabled, even when no save is required (poor programming, but it is Apple, so I suppose they can do what they like).

Thanks for the help, Phillippe and Peter. Hopefully some day Apple will toggle the state of the Save menu item appropriately.

Is there a way Iā€™m unaware of to detect a fileā€™s ā€˜Editedā€™ state via AppleScript? (Iā€™m assuming the answer is ā€˜noā€™, so no need to reply unless my assumption is incorrect.)

Thanks again.

Hi,

So, it looks like the File > ā€™Revert toā€™ menu will have n == 1 items if itā€™s currently in an unchanged state, and will have n >= 2 items if itā€™s modified and unsaved. I just started using KM a couple of days ago, so unfortunately, I donā€™t know how to proceed with that information, but if you do figure it out, please post the solution as I think the general workflow for this would be really useful!

Thanks

1 Like

Ahh, good catch. In that case you can use the "Menu with name matching ā€˜^Last Saved.*ā€™ is enabled" condition.

A good catch, indeed ā€“ thanks woof; improved one of my macros vastly. Much appreciated.

Peter, forgive my naivete, but can I ask what function the ā€œ^ā€ preceding ā€œLast Savedā€ serves? (I am guessing that the ā€œ.*ā€ following it is Regex for ā€˜any number of any charactersā€™, but I am not entirely certain.)

Thanks very much.

1 Like

^ means ā€œat the startā€
. means ā€œany characterā€

  • means "zero or more of the preceding element.

So the regular expression matches:

[at the start only]Last Saved[zero or more any characters]

1 Like

Got it. Thanks you, sir.

1 Like

You would think that the modified property of a document would tell you, but with both Script Editor and TextEdit, modified of the front document is false even after typing a character in that document. I havenā€™t tried other applications. Can someone explain this?

Iā€™m getting inconsistent, and in some cases somewhat mysterious, results across applications:

TextEdit

  • Unmodified document:

  • Type a character:

Script Editor

  • Unmodified document:
  • Type a character.

Script Debugger

  • Unmodified Document:
  • Type a character:
    Menu item unchanged.

Xcode

  • Unmodified Document:
  • Type a character:

I could go on.

In most cases checking the state of the buttons at the extreme upper left of the window works (I had to snapshot all three and set fuzziness all the way to the left to get it to work), but not all: in Xcode, the button doesn't change when a file is modified.

Also, you would expect the File>Save menu item to be grayed out when there are no modifications, but that isnā€™t the case ā€” at least I haven't noticed an application where it is. Upon reflection, though this was always right in front of my face, it does seem bizarre.

Hey Mitchell,

Appleā€™s Versions purely sucks.

I have it turned off as much as possible.

And the modified property works fine for me in both TextEdit and Script Editor.

-Chris

Thatā€™s really weird. I certainly expected them to do so, but they don't for me. All I did was type a character in a saved file then did something like

the modified of the front document of application "TextEdit"

How? I never use it, but it does sometimes get in the way, especially with files that havenā€™t been saved for the first time.

Hey Mitchell,

System Prefs > General

Google ā€œos x how to turn off versionsā€

I was thinking I'd used Onyx or TinkerTool System to turn off app-nap and a couple of other things, but I'm not finding those features in either app.

-Chris

What do those have to do with versions?