last modified date in status bar?

  • Scott, the difference is that notepad++ is closed in my case, before opening the file, in that case only initial script doesn’t work

  • @patrickdrd said:

    notepad++ is closed in my case, before opening the file

    Yeah, that’s a case I don’t think about, or code for. Because, as the best program on my PC, Notepad++ never gets closed. :-D

  • @patrickdrd

    does READY callback work for you?
    It seems that this one can’t really be used when running on linux.
    BUFFERACTIVATED might be another callback which is useful in such cases.


  • …without overriding the language name?

    Ah…so I guess I missed this part of @SalviaSage 's posting. Okay, so there is no prebuilt function to get the statusbar data, like there is to set the statusbar data…but we can create one:

    import ctypes
    from ctypes.wintypes import BOOL, HWND, LPARAM
    def npp_get_statusbar(statusbar_item_number):
        FindWindow = ctypes.windll.user32.FindWindowW
        SendMessage = ctypes.windll.user32.SendMessageW
        EnumChildWindows = ctypes.windll.user32.EnumChildWindows
        GetClassName = ctypes.windll.user32.GetClassNameW
        create_unicode_buffer = ctypes.create_unicode_buffer
        npp_get_statusbar.STATUSBAR_HANDLE = None
        def get_data_from_statusbar(statusbar_item_number):
            retcode = SendMessage(npp_get_statusbar.STATUSBAR_HANDLE, SB_GETTEXTLENGTHW, statusbar_item_number, 0)
            length = retcode & 0xFFFF
            type = (retcode >> 16) & 0xFFFF
            text_buffer = create_unicode_buffer(length)
            retcode = SendMessage(npp_get_statusbar.STATUSBAR_HANDLE, SB_GETTEXTW, statusbar_item_number, text_buffer)
            retval = '{}'.format(text_buffer[:length])
            return retval
        def EnumCallback(hwnd, lparam):
            curr_class = create_unicode_buffer(256)
            GetClassName(hwnd, curr_class, 256)
            if curr_class.value.lower() == "msctls_statusbar32":
                npp_get_statusbar.STATUSBAR_HANDLE = hwnd
                return False
            return True
        EnumChildWindows(FindWindow(u"Notepad++", None), WNDENUMPROC(EnumCallback), 0)
        return get_data_from_statusbar(statusbar_item_number) if npp_get_statusbar.STATUSBAR_HANDLE else None

    Thus to add the modification timestamp info to the first section of the status bar, one would call npp_get_statusbar(0) and then use the returned string (if not None) by adding the time to it, e.g.:

    notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, npp_get_statusbar(0) + ' ' * 8 + last_modified_date)

    I’m sure the basis for this status bar code goes back to some @Claudia-Frank code, but I can’t find the reference to it right now. Sorry, CF.

  • yes, it works CF

    I tried BUFFERACTIVATED but it didn’t work

  • @patrickdrd, have you been able to get this working? I’ve been trying here from my end. I can’t get the last modified date to display on the statusbar. If you have been able to get this working, can you please post the code?

    Thank you.

    import os
    from datetime import datetime

    def StatusbarSelOverride(args):
    modified_time = os.path.getmtime(notepad.getCurrentFilename())
    last_modified_date = datetime.fromtimestamp(modified_time)
    notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, ‘Mod: {:%d/%m/%Y %H:%M}’.format(last_modified_date))

    editor.callback(StatusbarSelOverride, [SCINTILLANOTIFICATION.UPDATEUI])
    notepad.callback(StatusbarSelOverride, [NOTIFICATION.FILESAVED])
    notepad.callback(StatusbarSelOverride, [NOTIFICATION.READY])

  • I still can’t figure out how to add the timestamp and still keep the original contents of a statusbar section.

    Scott Sumner, I would appreciate it very much if you can fix this code and post it here. thank you.

  • I don’t think you need the original contents, if we’re talking about the first tab,
    is DOCTYPE really that important for you?
    If so, why?
    I can’t think that’s important, that’s why I’m overwriting it

  • Using LuaScript I’ve been able to keep the doc type and display the file modification time. Should be easily translatable to PythonScript.

    Note this works with a yet-to-be released version of LuaScript

    npp.AddEventHandler({"OnSave", "OnSwitchFile", "OnLangChange"}, function()
    	-- Make sure it is a "real" file first
    	if npp:GetCurrentDirectory() == "" then
    	local text = npp.LanguageDescription[npp.BufferLangType[npp.CurrentBufferID]] .. " - " ..'%d/%m/%Y %H:%M', winfile.attributes(npp:GetFullCurrentPath(), "modification"))
    	npp.StatusBar[STATUSBAR_DOC_TYPE] = text

    Gives the results:

  • @dail

    unfortunately not, as python script hasn’t been updated with recent npp messages yet.


  • @Claudia-Frank

    Ah. Thanks for clarifying.

  • @dail
    Nice, I will be using your script then.
    When is the release of the next version of luascript?


  • Not sure. You can download a prelease though.

    Just drop the DLL in with the other plugins. Edit the startup script with Plugins > LuaScript > Edit startup script and put the above code in there, save, and restart Notepad++.

  • Thanks. the code works.

  • I think I’ve found a bug:

    I had around 10 tabs opened in npp, and my work shift is close to the end so I decided to close npp, so I pressed the x in the top right corner to find out that:

    there was a file open which was attached to an email,
    it was opened as read-only originally,
    I clicked on edit -> clear read-only and I edited it,
    I never saved it,

    the date on the bottom left corner is updated continuously/ every minute that passes (on the clock), even though I’m not saving it (I can see a star on the top left corner - the star that means that the file is not saved),
    I opened the containing folder and the file doesn’t exist there,
    I’m using autosave plugin to run every time focus is switched off
    from the current file as well

    edit: more likely its an autosave bug

  • @patrickdrd

    something is strange here - how can python get a modified timestamp if the file doesn’t exist at all??
    The autosave plugin could explain why you see the continuous update of the timestamp
    but only if there is a file which can be saved.
    Sorry, but, are you really sure that the file doesn’t exist?


  • yes, I checked in windows explorer, it should be easy to reproduce it yourself

  • I’m also using the lua script too, not the python one

  • @patrickdrd

    The Lua I posted above simply askes the operating system (i.e. Windows) when the file was last modified. Even if the file isn’t being saved from Notepad++, it would appear something else is changing the file’s modification timestamp.

Log in to reply