py-appscript expects arguments - python

I want to make a Python script to control VLC. VLC can be controlled through AppleScript and by using py-appscript I can run AppleScript code from Python.
Using AppleScript I can play/pause VLC by
tell application "VLC" to play
This equals to the following in py-appscript
app('VLC').play()
I should also be able to skip to next track by:
app('VLC').next()
But when doing so I get the following Python error:
Traceback (most recent call last):
File "vlclib.py", line 25, in <module>
app('VLC').next()
TypeError: next() takes exactly 2 arguments (1 given)
Does anyone know why I get this error? The above code should equal the following in AppleScript which works perfectly:
tell application "VLC" to next

From the appscript documentation:
Names that match Python keywords or names reserved by appscript have an underscore appended.
As next is a reserved keyword, you can fix this by running
app('VLC').next_()

Related

Python3 script to get status of deluge

I want to make a python script that every 30 minutes checks the status of a torrent on my RPI (where deluge is up and running) and if completed sends an email. I know how to make a timer, loop and email, but I don't know about deluge. I've read this: https://media.readthedocs.org/pdf/deluge/latest/deluge.pdf.
I know I need to import deluge and start with get_session_state() and get it to string, but it doesn't work.
Here's the error:
Traceback (most recent call last):
File "torrents_status.py", line 3, in <module>
get_session_state()
NameError: name 'get_session_state' is not defined
If you are just doing import deluge, you have to use the full module name for the methods.
deluge.get_session_state()
However, I'm imagining there is some connection object to the session that you should actually be calling that method on. I don't want to read through that 300 page manual, though, to find the correct module you should import.
Alternatively, you could do something like
from deluge import get_session_state
Then call
get_session_state()
Once again, you'll need to use the correct module name instead of deluge.

How to use the navigation-policy-decision-requested signal in Python Webkit

How can I use the navigation-policy-decision-requested (or equivalent) in association with webkit_web_policy_decision_ignore()?
The following is a small outtake form my code (the rest is just a wrapper and settings etc):
def check(view, frame, req, nav, policy):
webkit_web_policy_decision_ignore(TRUE)
...
view.connect("navigation-policy-decision-requested", check)
When I load a new page this error is returned:
Traceback (most recent call last):
File "browser.py", line 17, in check_exec
webkit_web_policy_decision_ignore(TRUE)
NameError: global name 'webkit_web_policy_decision_ignore' is not defined
What I'm trying to achieve is that if a certain address have been given, actions will be taken to prevent it from loading via Python. Any suggestions are welcome in the comments and any additional information you may need will be provided upon request.
Now I'm new to Python so please be specific in your answer, criticism and suggestions.
If you are using pygtk, try policy.ignore().
The object names are mapped slightly differently in pygtk. In python shell you can try after executing from gi.repository import WebKit
print dir(WebKit)
to find corresponding object and in your case
help(WebKit.WebPolicyDecision)

Make _tkinter.createfilehandler work again (or provide a workaround)

I've got some old Python scripts which used a different version of tkinter than the current systems are equipped with. Back in the old days, a method named _tkinter.createfilehandler() existed and worked. With the current versions this returns with a RuntimeError exception:
Traceback (most recent call last):
File "src/text.py", line 513, in <module>
tkinter.createfilehandler(pipe_r, READABLE, drain_pipe)
RuntimeError: _tkinter.createfilehandler not supported for threaded Tcl
The script I'm trying to get to run is this (shortened version of course):
#!/usr/bin/env python
import os
from Tkinter import *
(pipe_r, pipe_w) = os.pipe()
# ...
def drain_pipe(a, b):
# handle data from pipe_r
# ...
tkinter.createfilehandler(pipe_r, READABLE, drain_pipe)
tk.mainloop()
Is there a simple way to get this running again? I'm sure there is a way to convert the scripts (or maybe write them anew based on a different paradigm), but I'd be happy with a hack to not have to change very much (maybe there's a switch to enable this again somehow) because I've got several such scripts and would prefer not to have to patch a lot.
If tk is a Tk() object, then use tk.createfilehandler instead.

Emacs interactive Python mode delays evaluation [duplicate]

I'm using Python 3.3 and Emacs 23.4 on Windows 7. I'm getting some odd behaviour when using python-shell. If I type in a command that produces some sort of output I get the result immediately on the next line. If the Python statement I've entered causes an error however, no output is shown. When I type in the next Python statement and hit enter, the error message for the previous line will be displayed.
For example when I'm processing some command line arguments:
>>> args
Namespace(templatedir=None, xmldir=None)
>>> args.bobbins
>>> args.templatedir
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Namespace' object has no attribute 'bobbins'
>>>
The first statement prints out the value of the args variable.
The second statement should print out an error message but nothing is printed.
The third statement is correct but actually prints out the error from the second statement.
Does anybody have any idea what's wrong with my Python / Emacs setup?
This was a bug in Python, and was fixed.

Running fselect using Python

I am trying to run this python script called fselect in windows 7. It can be downloaded from this website: http://www.csie.ntu.edu.tw/~cjlin/libsvmtools/ under this name called Feature selection tool. I am running it on Python 2.7.2.Facing a bit of problem running it..
Typed this first in IDLE:
>>> import pprint
>>> import sys
>>> print pprint.pprint(sys.path)
>>> sys.path.append("C:\Users\HP\Documents\MATLAB\libsvm-3.11\tools")
>>> import fselect
Usage: training_file [testing_file]
Then the problem is when i type the next part:
Tried this:
>>> ./fselect.py TrainVec
SyntaxError: invalid syntax
Next tried this:
>>> fselect.py TrainVec
SyntaxError: invalid syntax
Next tried this:
>>> TrainVec
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
TrainVec
NameError: name 'TrainVec' is not defined
Tried this also:
>>> TrainVec.mat
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
TrainVec.mat
NameError: name 'TrainVec' is not defined
What is the correct way of typing it? Need some guidance on it...
tried running using cmd but there is an error...
If you are trying to run the fselect.py directly from the command prompt, make sure that python is set into the path variable. For guidance with that, please read http://people.cis.ksu.edu/~schmidt/200f07/setpath.html.
The script will also invoke grid.py. grid.py requires gnuplot to be there. So ensure that grid.py is running properly and if necessary check the paths of the svm_train, svm_test in the script along with that of grid.py.
Hope it will work now.
If it is a tool, you should run it, not import it. And of course you should not try to enter random commands, even if they are valid shell commands, in your Python prompt.
Assuming TrainVec is your data (since you use it in the context of TrainVec.mat it must be a Matlab data file) then run it on the Command Prompt like this:
python fselect.py TrainVec.mat
The example of ./fselect.py is intended for Unix systems. Make sure you run the above command in what ever directory you have saved fselect.py in.
If you need to write your own scripts to leverage this .py file then I refer you here for an example of how to do this.
Like the previous answer said, it looks like you are (incorrectly) trying to run the script from inside the Python interpreter. According to the documentation on the page you link to, it is not a module but a free-standing script and should be run as such:
Usage: ./fselect.py training_file [testing_file]

Categories