I need to launch an application on reading a Notification center notification on osx. Is there any python library for reading the notification center notifications?
Note: I did not try any of this as I do not have a machine running OSX at the moment, but from what I googled:
You could try adding an AppleScript snippet in your python code that uses System Events and Notification Center to parse all available notifications and return them to your stdout and parse the output directly in your code.
Source for getting all available notifications: https://macosxautomation.com/mavericks/notifications/01A.html. The part that should interest you is the code of the getNotificationTitles function. The bad part is that in that implementation you only get their titles, not the full notification, so you might have to parse different areas to get the full notification body.
Source for running it and getting the result: How do I embed an AppleScript in in a Python script?.
Another approach would be to use the terminal-notifier cli tool's -list argument (as a shell command inside your script) and then parse the results.
There's also a python wrapper for this tool available. The implementation contains a list() function which promises to work just the cli tool just that you wont need to spawn any shells from your own code and parse their stdouts.
I believe you will be able to do this with pyobjc. If you take a look at part of the code in macos-notifications repository, you'd find the userNotificationCenter_didDeliverNotification_ listener. This will get events the moment notifications are delivered. This does not only capture the notifications that you deliver using that application but for any notification. You could tweak the code a little and give it a try :)
Related
I am using a Python script to query our wireless controllers (ArubaOS). They offer an API function called "showcommand" which let's you send any (perhaps not any but anyway) of the normal commands that you would type in at the CLI (often these are like "show ... some stuff", hence showcommand).
It normally does a good job of returning some structured data. However I am running a particular command which has an issue. When you run the command on the CLI it initially displays a warning like "This command may take some time to run, please be patient" and then it displays the actual data. When I run the command from my Python script all I get back is the warning, no actual useful data.
I can't think of a way to solve this. Maybe if there was a way to introduce a delay immediately after the request is made to wait for the data? But I guess that would need to be something within the requests module, and I can't see a likely candidate. Does anyone have any ideas how to get around this?
Thanks,
Guy
as a learning exercise I am trying to create a simplified clone of codepad.org, and the first language I want to support is C.
For context: if I was making a simple note-taking web app (simple Evernote, for example), I can see how the architecture is simply that you would need a server running to route requests, so if I have three buttons (new, save, open) if I click 'new' it renders a page that lets you type words, and if I click 'save' the server takes that data and then stores in a database, however all of this is very self-contained and doesn't involve the extra component of opening up a terminal in the box that my app is running from and then creating a file on the box filesystem using the code that a user inserts into the text input form, and then executing the command-line command to compile/interpret the code depending on the language.
How does one do that?
I can see that the steps are as follows:
Node.js or Flask server is running
I goes to localhost:9000, and a template renders that has a form that I can put code text into
I select 'C' from the languages dropdown
I write some simple and valid C code
I click 'execute'
The server routes this POST request and the data to use as the code to execute and somehow instantiates a new terminal session, saving the file to disk and then executing the needed command (gcc temp1.c -o temp1.x)
temp1.x is executing, and whatever appears in standard output is piped back through the server to the page and the output renders accordingly.
How do I do 6 and 7? Not looking for step by step instructions, but is there a word or programming concept I should look up to find out how I'd do this? I am thinking of writing the app in Flask or as a Node.js application depending on what is available but I am open to any suggestions. I have searched quite a bit and have not found the words to use appropriately in order to inquire further on how to implement the extra step of interfacing between the server application running on an actual physical server and other tools that the operating system would allow you to use otherwise...
Hope this question makes sense, and I understand that this isn't a specific programming question but I did not know where else to put this other than Stack Overflow.
I appreciate any assistance.
To save the files, you can use the Flask API. To run the command, you would use subprocess.Popen.
You would also use subprocess.Popen to get the output. Basically, when calling Popen(), you would set the argument stdin=subprocess.PIPE, and then call subprocess.Popen.communicate() to get the output. For example:
cmd = subprocess.Popen("command to run C code", stdin=subprocess.PIPE)
output = cmd.communicate()[0]
I highly suggest Flask, you could prototype this application very quickly in it.
Is there a way to send some parameter from autohotkey to python.
Using Autohot key I read some number from the notepad and store in a variable and now I want to send this number to the python code in order to do some calculations.
My Autohotkey code is:
controlGetText, telphoneNumber, Edit1, Untitled - Notepad
And I want to send this telphoneNumber to python file.
Is there a way I can do that?
Do I need to create an exe file of a python and then call from autohotkey?
For example:
RunWait, C:\Button\button.exe telphoneNumber
Or do I need to run command prompt commands from autohotkey to run python program? Something like:
Run Cmd Python C:\Button\button.py telphoneNumber
I do not know which is the best way as I am newbie in Autohotkey.
Any suggestion will be appreciated.
EDIT:
However I succeded in sending parameter by using run command from autohotkey, which will execute the python file from command prompt.
Run Cmd \k "Python C:\Button\button.py %telphoneNumber%"
But still want to know if this is the right solution, or if there are others?
Inter-process communication would be capable of sending the information while the Python script is already running.
Forum thread: http://www.autohotkey.com/forum/topic21699.html (there's a nice documentation link in that post)
You could also use TCP/IP Network communication (like in the post below), but that probably wouldn't be quite as slick as using IPC.
Forum thread: http://www.autohotkey.com/forum/topic13829.html
The way you got it working is the easiest, and probably best, method of accomplishing what you want.
Communication between applications can be done with more methods then you probably can imagine, but as long as it doesn't have to be realtime you can call your programs with arguments, as it is easy and reliable.
Python COM server allows directly calling Python functions(with args and return) using AHK.
you use it like this: MsgBox % pythonComServer.method(args)
You do not need to have a python script already running.
ComObjCreate() will instantiate an instance of python.
I don't know how the inter-process communication is done in the background by pywin32, but using it is simple.
2 examples here: Call python function with arguments and get returned value in autohotkey
The situation is like this:
I want to capture the pop-ups of IPmsg.exe in my python program.
There is an easy way of doing it, which is reading from the log file. But I would like to know if this can be done without bringing log files into discussion.
For more on IPmsg.exe: http://ipmsg.org/index.html.en
That was being specific.
Now, what would be a generic approach to capturing the output of a windows based GUI program?
There are generally two ways to talk to GUI programs on Windows, if you hate the log files:
Use their command line interface! I doubt this has one that outputs to stdout as messages come in
Use the win32 api or a wrapper for it to search for specific windows (polling as necessary or installing hooks to find out when they appear) and then grabbing text from them using more api calls. See this question: Get text from popup window
+1 for using the log files by the way, far easier.
You can capture only the output from applications through Python that you start directly from Python e.g. using the subprocess module:
http://docs.python.org/library/subprocess.html
Otherwise you have basically no chance for reading the direct output of other applications.
Imagine a web application that allows a logged in user to run a shell command on the web server at the press of a button. This is relatively simple in most languages via some standard library os tools.
But if that command is long running you don't want your UI to hang. Again this is relatively easy to deal with using some sort of background process or putting the command to be executed onto a message queue (and maybe saving the output and status somewhere for later consumption). Just return quickly saving we'll run that and get back to you.
What I'd like to do is show the output of said web ui triggered shell command as it happens. So vertically scrolling text like when running in a terminal.
I have a vague idea of how I might approach this, streaming the output to a websocket perhaps and simply printing the output to screen.
What I'd like to ask is:
Are their any plugins, libraries or applications that already do this. Something I can either use or read the source of. Ideally an open source python/django or ruby/rails tool, but other stacks would be interesting too.
I'm not sure if it's what you want, but there are some web based ssh clients out there. If you care about security and really just want dynamic feedback, you could look into comet or just have a frame with its own http session that doesn't end until it's done printing.
web-based ssh client would work, into the host (there are java ssh clients out there).
Ruby has a web-based terminal:
http://tryruby.org (link to the source is at the bottom of the page).
You could also embed Ruby via jruby: http://tim.lossen.de/2007/03/jruby/applet.html
http://github.com/jruby/jruby/blob/master/samples/irb-applet.html
I haven't heard of any libraries that do this, but you'll need to setup the system command and call out to the system. You will then need to "pump" the sysout and syserr standard inputs and pipe that data back out to your web client.
As an example for this style of problem, look into code snippits of how people use ruby/python/etc to transcode a video, i.e. http://kpumuk.info/ruby-on-rails/encoding-media-files-in-ruby-using-ffmpeg-mencoder-with-progress-tracking/ - my example was taken from this blog post.
class MediaFormatException < StandardError
end
def execute_mencoder(command)
progress = nil
IO.popen(command) do |pipe|
pipe.each("r") do |line|
if line =~ /Pos:[^(]*(s*(d+)%)/
p = $1.to_i
p = 100 if p > 100
if progress != p
progress = p
print "PROGRESS: #{progress}n"
$defout.flush
end
end
end
end
raise MediaFormatException if $?.exitstatus != 0
end
I don't know if this example is pulling data from both sysout and syserr, but you will definitely need to be pulling data from both of those interfaces, typically if the buffer fills up, the executing command might hang or fail (I have experienced this with Python). This method will also look different if the only thing you do is return line to the web client - in a terminal, the progress indicator of ffmpeg/mencoder remains stationary on the bottom line, but this method will give you a long list of progress indicator updates. Pipe line out to your terminal and you'll see what I'm referring to.
So, I've tried to answer my own question with code as I couldn't find anything to quite fit the bill. Hopefully it's useful to anyone coming across the same problem.
Redbeard 0X0A pointed me in the general direction, I was able to get a stand along ruby script doing what I wanted using popen. Extending this to using EventMachine (as it provided a convenient way of writing a websocket server) and using it's inbuilt popen method solved my problem.
More details here http://morethanseven.net/2010/09/09/Script-running-web-interface-with-websockets.html and the code at http://github.com/garethr/bolt/
Certainly not the best way to run shell commands, but likely the easiest:
#!/bin/sh
echo Content-Type: text/plain
echo
/usr/bin/uptime
http://www.sente.cc/scripts/uptime.cgi
Take a look at Galaxy (online demo) or Yabi.
Except from the requirement to be able to show output during the job run, they are both excellent solutions to this! They are also both written i Python (and Yabi even on django).
They were both built with bioinformatics in mind, but really are both general job runner/workflow tools.
They will let you specify parameters in a web interface, see queued/running/finished jobs in a separate column, and after the jobs are finished, inspect details and results, or re-run the job, with possibly changed parameters.
Galaxy is the easier one to install. The Galaxy installation boils down to downloading and run "sh run.sh"), and adding your own tool boils down to creating an XML file in the line of:
<tool id="mytool" name="My Tool" version="1.0.0">
<description>Does this and that</description>
<command>somecommand --aparam $aparam</command>
<inputs>
<param name="aparam" type="text" label="A parameter"/>
</inputs>
<outputs>
<data name="outfile" format="tabular"/>
</outputs>
</tool>
... and place it in the /tools folder, and add a line in the tool_conf.xml to tell galaxy of your new tool (There you can also get rid of the bioinformatics-tools, so they don't mess up your tools menu).
Yabi is more complicated to install (see the readme file), but the process might be smooth if you are on the right kind of system. On the other hand, it allows you even do the tool configuration in the web interface, rather than as an XML file like in Galaxy.
Galaxy still is the one with the biggest community though, which is reflected in the number of features/already integrated tools (See the toolshed for shared tools/wrapper).
websocketd looks like the perfect tool for that.