Author Topic: Running a python script  (Read 13773 times)

0 Members and 1 Guest are viewing this topic.

David Dyer-Bennet

  • Member
  • **
  • Posts: 21
    • View Profile
Running a python script
« on: 2014-10-31 17:23:42 »
Does anybody have the faintest idea how to run a Python script?  I've found the directory the ones supplied with TP are in, which gives me some hints about what libraries to access to do various things, but I don't have the faintest idea how to invoke a Python script from within Thumbs Plus.  

This seems like the most basic thing to document -- with this, we can get started experimenting, but without this, we're groping completely in the dark to do anything at all.

Daan van Rooijen

  • Administrator
  • Sr. Member
  • *****
  • Posts: 933
    • View Profile
Running a python script
« Reply #1 on: 2014-10-31 18:38:40 »
Well, there's a mysterious Python panel under Window | Show Panels that lets you run ..things.

I'd think that's where the Python magic can happen, but I have no idea how it all works.
I'm volunteering as a moderator - I do not work for Cerious Software, Inc.

mschnell

  • Full Member
  • ***
  • Posts: 98
    • View Profile
Running a python script
« Reply #2 on: 2014-11-01 05:56:51 »
Running my own Python scripts would be the main reason for me to upgrade from v7 to a current TP.

I did some research on this and found out how to add a menu that is _supposed_ to start a script. (In fact I would also like to be able to start it with a hotkey, a smart icon and/or a right click on a file or a directory).

Already years ago Laura promised that very soon a decent documentation on user scripts would be available in the help. I did not see it up til now. Of course other tasks are more pressing to the developers.

The documentation would not only need to include how to _run_ a script but a description of the necessary library calls to access the data base and to manipulate files and images content.

-Michael

David Dyer-Bennet

  • Member
  • **
  • Posts: 21
    • View Profile
Running a python script
« Reply #3 on: 2014-11-01 15:18:41 »
Okay, I'm apparently not paying attention.  I knew that existed, but didn't think of it as a way to start a script.  Clearly it can be, thanks for pointing it out!  

That's sufficient for early playing around, debugging, and even quite a lot of use (having to type the name each time or even navigate the drop-down is inconvenient relative to menu or toolbar for some kinds of things, but it's a useful start).

I'm somewhat familiar with Python and very familiar with programming and scripting languages (45 years making my living writing computer software), and as I said the examples of Python scripts that are part of Thumbs Plus should give me some examples of how to access various things I'll want access to (like the database, in particular).  So we'll see what of the things on my list I can actually accomplish.

David Dyer-Bennet

  • Member
  • **
  • Posts: 21
    • View Profile
Running a python script
« Reply #4 on: 2014-11-01 15:24:28 »
Well, let's see how far we can get.  Maybe we can get a pointer now and then if we're actually stuck.  

Yeah, indicating particular images (one or more) (or directories) as info passed to a script is important (right-click, keyboard shortcut, menu, toolbar, or whatever), we'll certainly want that in the long run.  

I'm kind of guessing that the work for a good clean interface for that stuff is only partly done, or something (and as you say has been taking a lower priority than other things; there's a LOT of stuff that would be desirable to do, I'm not sure how I'd prioritize it myself if I had the full list in front of me.  And there's a rumor that not everybody completely agrees with me on what's important :-).  

I had a bunch of Perl scripts (still have them) for moving stuff from TP to my web gallery, and replacing images when I updated them and such, that I could run from the toolbars.  Worst case, maybe we can hook things up that way temporarily.

We'll see how much time I have, might have time and energy to get somewhere on this for a few weeks now.

mschnell

  • Full Member
  • ***
  • Posts: 98
    • View Profile
Running a python script
« Reply #5 on: 2014-11-01 15:58:14 »
I'*m positive that you will keep us posted with any results :)

-Michael

David Dyer-Bennet

  • Member
  • **
  • Posts: 21
    • View Profile
Running a python script
« Reply #6 on: 2014-11-01 17:40:17 »
Okay, that entry box at the left of the Python window seems to be input to an actual Python interpreter.  If you enter
    print "Hello, world!"
there, it prints "Hello, world!" in the space below for example.  You don't actually have to click the "run" button, hitting enter suffices.

You can also set variables there and such; presumably any valid Python but I haven't tested extensively.  

So, what it doesn't actually do is provide a way to run a script.  I guess it can call functions that are defined, though.  And there are probably library ways to do something equivalent to Perl / shell "eval", eh?  You can probably import a library from there; yeah, "import invalid" gives an error indicating it tried to import and couldn't find a module named "invalid", so that looks hopeful.

It's not as simple as just being the same as an interactive Python interpreter, though; you can't type arithmetic expressions and have the result printed out, for example.

Daan van Rooijen

  • Administrator
  • Sr. Member
  • *****
  • Posts: 933
    • View Profile
A few more clues?
« Reply #7 on: 2014-11-01 19:16:48 »
It seems that Python commands such as 'Import' use the Python\Lib\thmpls\ subfolder as their working directory. That folder holds quite a few TP scripts, e.g. for Facebook. Again, I don't speak python but those who do, may want to have a look at them to see what other clues they contain.

Several of the scripts refer to a file 'tpuser.py' which I can't find. For instance, tpcmd.py file says "For implementing your own Python scripts as commands, please see tpuser.py" and tpui.py says "Please edit tpuser.py to add your own scripts". There is a 'user.py' script in Python\Lib but I think that's just part of the Python files set and wasn't made specifically for use with TP.

Btw, note that there's also a Python item in the Files menu, and that both the Commands tab and the Keyboard tab in the Options | Customize dialog contain references to Python functionality. Those on the Commands tab can be dragged onto a toolbar but they don't seem to do much, other than resetting the Python interpreter.
I'm volunteering as a moderator - I do not work for Cerious Software, Inc.

David Dyer-Bennet

  • Member
  • **
  • Posts: 21
    • View Profile
A few more clues?
« Reply #8 on: 2014-11-02 12:26:27 »
There are references to tpuser.py in mkcmddefs.py, tpcmd.py, and tpui.py -- but no actual tpuser.py file is present.  And the references are only in comments; this kind of suggests that the code to use tpuser.py isn't present.  So this is probably not going to lead anywhere.

However, the files defining the official TP commands that are in Python may well be illuminating.

mschnell

  • Full Member
  • ***
  • Posts: 98
    • View Profile
Running a python script
« Reply #9 on: 2014-11-03 14:36:23 »
There are several sample Python file you can take a look at and they in fact do library calls e.g. to access TP the database.

My Python knowledge is close to zero right now and I no'nk know it it is because of that or because it just is not possible: I did not succeed in trying to do such a library call in the TP Python command line window.

I did not check with the current distribution, but last time I played with that, TP came with nearly all Python files as well in "compiled" (*.pyc) as in "script" (*.py) versions.

-Michael

mschnell

  • Full Member
  • ***
  • Posts: 98
    • View Profile
A few more clues?
« Reply #10 on: 2014-11-03 14:54:15 »
> However, the files defining the official TP commands that are in Python may well be illuminating.

Did you fiend those ?

-Michal

David Dyer-Bennet

  • Member
  • **
  • Posts: 21
    • View Profile
A few more clues?
« Reply #11 on: 2014-11-03 15:11:21 »
I believe so -- things like ThumbsPlus/Python/Lib/thmpls/facebook.py for example, and the whole facebook subdirectory there (also there's a flickr subdirectory there with .py files).  I think I've got lots of examples of their code, at least.

mschnell

  • Full Member
  • ***
  • Posts: 98
    • View Profile
A few more clues?
« Reply #12 on: 2014-11-04 14:45:54 »
What I meant were the files (or whatever) that link the python scripts to the GUI (top menus, right-click menus, Smart Icons, ...

-Michael