Help with my 1st Python Script
#1
I could use some scripting guidance. I am trying to write a simple script for my own personal use which would delete the active video (ListItem) after it has stopped playing when the Delete key is pressed on the Active window.. I am using the Confluence skin under Eden on a Win7 platform.
Here is what I have:
---------------------------
Code:
import xbmc, xbmcgui, os,  sys
class Main:
        """ Delete file from the OS """
                      def delete_file(self, file):
               if os.path.exists(file):
                os.remove(file)
                """ Deleted """
                self.notify(__settings__.getLocalizedString(30014) + ' ' + file)
                
        """ Finally clean the library to account for any deleted videos """
            if doClean and self.cleanLibrary:
                xbmc.executebuiltin("XBMC.CleanLibrary(video)")
        
                
run = Main()

Will this work or am I missing something?
Reply
#2
What duo you need help with?
Reply
#3
seems like it's missing a lot or this is just a code snippet.
Also indenting will be an issue
Read/follow the forum rules.
For troubleshooting and bug reporting, read this first
Interested in seeing some YouTube videos about Kodi? Go here and subscribe
Reply
#4
(2012-06-02, 00:35)Bstrdsmkr Wrote: What duo you need help with?

I guess to put it simply, I would like to duplicate the "Remove from Library"/Delete from Computer/...Cleaning LIbrary sequence that occurs when one invokes the Remove command in the Context Menu. I think all of this must happen at core level, because I cannot find anything in the Skins files that do this.
Reply
#5
You're headed in The right direction. You won't be able to call the built-in window directly, but you can certainly duplicate the feature. I wouldn't even try to make it a class, just a script that gets a reference to the file and deletes it. Then bind that to a key as a script
Reply
#6
(2012-06-04, 20:19)Bstrdsmkr Wrote: You're headed in The right direction. You won't be able to call the built-in window directly, but you can certainly duplicate the feature. I wouldn't even try to make it a class, just a script that gets a reference to the file and deletes it. Then bind that to a key as a script

Thanks for the input... but you lost me at "bind that to a key". Maybe an example would help?

Edit - nm, i know what you mean now.
Reply
#7
Assuming you are in a context where xbmc is importable:
If you are not, you cannot call clean lib, the rest will be fine.

Code:
import xbmc, xbmcgui, os,  sys

def delete_file(file):
    if os.path.exists(file):
        os.remove(file)
        xbmc.executebuiltin("XBMC.CleanLibrary(video)")

Reply
#8
Where is the default location to store this file, which I will call deletefile.py? Is it in the user/addon section or in the default Program Files/XBMC/system location?
Reply
#9
I don't know, I guess you have to find a skin that uses script and take look.
I do know how to call an add-on from a skin, if nothing else work I can quickly make it into a program add-on for you.

I still do not see how this will help you, have you found a why to safely get the file name from somewhere that is guaranteed to be correct?
Reply
#10
You should be able to store and call it from anywhere. xbmc.RunScript('C:\my\custom\directory\deletefile.py')
Reply
#11
(2012-06-05, 09:23)Bstrdsmkr Wrote: You should be able to store and call it from anywhere. xbmc.RunScript('C:\my\custom\directory\deletefile.py')

Thanks, that helps! The Wiki on storing python scripts is very outdated I think. It kept referring to a directory "XBMC/scripts" which I think is no longer valid.
Reply
#12
(2012-06-05, 07:24)vikjon0 Wrote: I don't know, I guess you have to find a skin that uses script and take look.
I do know how to call an add-on from a skin, if nothing else work I can quickly make it into a program add-on for you.

I still do not see how this will help you, have you found a why to safely get the file name from somewhere that is guaranteed to be correct?

Vikjon, I am going to go back to my original scheme, which was to create a Delete button on VideoDialogInfo.xml and run this script. The coding suggestions by Jezz_x did not seem to work. They simply hung up XBMC. So I am going to put an onclick on the VideoOSX stop button, which will return me to the VIdeoDialogInfo screen. It won't work in all circumstances, (ie. minimizing the show and doing something else) but if it works in this limited circumstance, it will fit my need. I will let you know how this works, and maybe someone more skilled than I can expand it's range.
Reply
#13
You guys have been wonderful with your assistance. I finally was able to put the python script in a directory seen by xbmc ("skin/scripts"). However, while Python does initialize, it just sits there and does nothing. I am thinking that I have not yet defined what "file" is. Since my active window will be DialogVideoInfo, can I use "file = ListItem.FilenameAndPath" or do I have to reference this in a different manner to pull the actual filename object? I presume that having 'file' being a local variable is not a problem.
Reply
#14
I am not sure exactly what you mean but you should call delete_file(file) where file is a s string containing the path.
http://docs.python.org/library/os.html

If it does nothing but do not crash I assume it did not pass the if os.path.exists(file):
I suggest that you insert a few print ("hello") to see what goes on.

If it does not run I would suspect that it cannot import the xbmc stuff and I would remove that for now and start with

import os

def delete_file(file):
if os.path.exists(file):
os.remove(file)



used like
delete_file("C:\movies\mymovie,avi")
Reply
#15
(2012-06-05, 22:14)vikjon0 Wrote: I am not sure exactly what you mean but you should call delete_file(file) where file is a s string containing the path.
http://docs.python.org/library/os.html

If it does nothing but do not crash I assume it did not pass the if os.path.exists(file):
I suggest that you insert a few print ("hello") to see what goes on.

If it does not run I would suspect that it cannot import the xbmc stuff and I would remove that for now and start with

import os

def delete_file(file):
if os.path.exists(file):
os.remove(file)

used like
delete_file("C:\movies\mymovie,avi")

Here is what I have so far:

Code:
import xbmc, xbmcgui, os,  sys
file = xbmcgui.ListItem.FilenameAndPath
def delete_file(file):
    if os.path.exists(file):
        os.remove(file)
        xbmc.executebuiltin("XBMC.CleanLibrary(video)")

I presume that the variable named "file" needs to pull a string value from the active window, but I am not at all sure that my syntax is correct. In fact, this does NOT work, as it says that FileNameAndPath is not a proper attribute. I will stick a few print statements in to see where it is getting hung up.
Reply

Logout Mark Read Team Forum Stats Members Help
Help with my 1st Python Script0