Menu

Persisting data? Hidden documents? Custom call backs?

Help
Moon
2017-07-27
2017-08-03
  • Moon

    Moon - 2017-07-27

    Help! I've gone down a rabbit (snake?) hole....

    I'm trying to create a script that can "remember" the current active document (Buffer ID I think) on the first run of the day (ie: since npp was launched). Then on subsequent runs perform some action against that document even if a different document is currently active.

    How do I persist the buffer ID from one script execution to the next?

    I think I'm missing something obvious scope wise, but I've been looking at creating a new document (overkill and how do I hide it?)
    Perhaps even Notepad.createScintilla() - a whole editor to store an int!!

    Or is a custom call back possible (where I hack the buffer Id into the call back function)?

    On a related note, is there a recommended way to save configuration data for the script to the user profile?

    Thanks.

     

    Last edit: Moon 2017-07-27
  • CFrank

    CFrank - 2017-07-28

    You could use the global namespace or the editor.setProperty function to save data need
    on subsequent calls

    global namespace:

    counter = globals().get('counter', 1)
    t = globals().get('t', 'set')

    print '{} {}'.format(t, counter)
    counter += 1

    or

    editor.setProperty('MY_VAR','42')
    print editor.getProperty('MY_VAR')

     
  • Moon

    Moon - 2017-07-28

    Thanks Claudia will check it out.

     
  • Moon

    Moon - 2017-08-03

    globals() thing works perfectly thanks.

    I didn't try too hard but editor properties seem to be per document rather than global?
    I understand there are separate editor objects for views 1 & 2, but in my quick testing in the same view, I could only get 42 out when on the same document as it was set in.

     

    Last edit: Moon 2017-08-03
  • Sasumner

    Sasumner - 2017-08-03

    What do you mean by "42"?

     
  • CFrank

    CFrank - 2017-08-03

    Correct, setProperty is per document as globals() returns the current global scope which is used by
    every script you run as long as your in the same namespace.

    Scott, I think moon refers to my example where I used 42 in setProperty call.

     
  • Sasumner

    Sasumner - 2017-08-03

    Ah, now I see what the 42 meant; I simply thought it was the answer to "everything".

    Seriously, though, what I do, and maybe it is wrong, but it works, is:

    script test1.py:
    a=3
    script test2.py:
    print 'a:', a

    run test1.py
    run test2.py
    console shows: a: 3

    After running test1.py, "a" is in the global namespace, to be read out or updated by test2.py.

    So...why do I need the globals() technique or the set/getProperty technique?

     
  • Sasumner

    Sasumner - 2017-08-03

    So okay the set/getProperty technique could be useful for any document-specific need, but I'd be inclined to just do something like this for that kind of need:

    b[notepad.getCurrentFilename()] = 7

    where again "b" is a dict in the global space

     
  • CFrank

    CFrank - 2017-08-03

    get/setProperty is normally used to get/set lexer specific properties like allow folding etc...
    Recently npp added fold.quotes.python property to allow doc comments to be folded, this wasn't possible before unless you would have set it with python script for example.
    But this gaves also the possibility to add any info/value you want to a document and it is only
    avaliable through the lifetime of that document. Once document closes everything is gone
    and you don't have to take care of it.(well, in theory ... I didn't check external memory allocation etc...)

    On the other side you can use the global scope (global namespace isn't really correct) to share
    variables like you did but then, even after document has been closed, data is still in global scope.

    And globals function, in general, allows checking a variable even on the first run.
    How would you check if script was already running by using ONE variable only?

     
  • Sasumner

    Sasumner - 2017-08-03

    Hmmm...for the 'already running' question, I use: try/except NameError. Some things to think about, at least...

     
  • CFrank

    CFrank - 2017-08-03

    yep, another possible way :-D

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.