NSOpenPanel unable to select files, why? - python

Being very new to objective-c, MacOS, and not having programmed for years I hope this is not too obvious and thus a waste of anyone's time:
I would like to open a dialog and get the file location back as a references. From what I can tell this should be doable from within my window controller, no? Using XCode I've plumbed to the openDocument action, with an associated function like this:
class MyWindowController(NSWindowController):
#objc.IBAction
def openDocument_(self, sender):
dialog = NSOpenDialog.openPanel()
dialog.setCanChooseFile_(True)
dialog.setCanChooseDirectories_(False)
results = dialog.openModal()
# test the results and do something
The problem is that while the dialog opens right up and lets me browse around it cannot actually ever select any files. What gives?
TIA for any assistance, even if it's to tell me I missed something in the manual (just point it out please!).

You don't specify which files can be opened (using the setAllowedFileTypes_ method), or by telling the panel that all files can be selected with dialog.setAllowsOtherFileTypes_(True)

Related

Print contents of WebView in Python (Gtk3.0)

I have designed a very simple Python3 app that uses a WebView to render Markdown files.
Now I'd like to add the possibility to print the rendered file (with the current stylesheet that's being applied). I'm desperately trying to find some information about how to do it. Is there any tutorial or howto out there that could explain how to do it?
I've found a few examples explaining how to print PDF files from disk, but I'd like to print things directly from the WebView. Apparently there is even a Method for that but I'm failing miserably on figuring out on how to use it...
See here
Best Regards
Merlin
OK,
I finally figured it out. It appears that only the Webkit2 library is modern enough to handle printing from a Webview. I was using WebKit1 and that was the reason it wouldn't work. So I had to change my dependencies and after that it was actually quite easy...
Lets say that you have a Webview whose name is myWebView, then your function to print it's content would look like:
def onPrintButtonClicked( self, widget ):
#Set the operation up
printOperation = WebKit2.PrintOperation.new( self.myWebView )
#Run the dialog
printOperation.run_dialog()
After the dialog, you could actually check if the user has chosen to print or to cancel by checking for WebKit2.PrintOperationResponse.CANCEL or WebKit2.PrintOperationResponse.PRINT return values.
Reagards

How to type into a text field in Windows Explorer?

I am trying to upload a file to a website, and when I click on the upload button (using module WebBot) it opens Windows Explorer. Am I able to output the name of the file into the File Name field? I have the full path of the file, I just need to get the actual text into the File Name box.
I'd consider two approaches here:
Use a python library specifically for interaction with the Windows GUI. I've had good experiences with Pywinauto once, seems still pretty usable at first glance. Hook this in when you expect the explorer window to open. Code may conceptually look like this - do some test run and print all available handles from the upload dialog (just guessing here as a hint, see Pywindocs):
app = Application().connect(title_re=".*Upload file", path=r"c:\windows\explorer.exe")
dlg = app.window(title_re=".*Upload file", path=r"c:\windows\explorer.exe")
app.dlg.print_control_identifiers()
Check if you could simply do a POST or similar with the corresponding data. This is a very vague alternative as you do not provide information about what to upload and what the underlying backend/concept of the website is, but in the simplest case this may even be a more elegant option. A quick search brought up this short and simple example for this: https://stackoverflow.com/a/43942648/10192615

How to close a QFileDialog.getOpenFileName in QTest?

I am developing an application in Python 3.5, using PyQt4, and I want to test it with unit test using QTest. So far I managed to do whatever I want with QTest except for this problem :
My window as a folder button, when you click it, a File Dialog open to choose a folder (classic). This is (more or less) written like that in my code :
self.file_dialog = QtGui.QFileDialog()
[...]
tmp_path = self.file_dialog.getOpenFileName(self, caption='toto', filter="*.csv")
My problem is that in QTest, I can't find a way to close this dialog when it shows up.
I already tried calling its close() or reject() method (directly or with a single shot timer), but none of these seems to work...
Is there any solution I didn't thought of ? I wouldn't mind closing all windows if that's necessary (but I can't do that either)
Thanks !
As far I have understood, you should not use the static methods (such as getOpenFileName) in tests, because you cannot control the opening of the dialog window. You should instead use the constructor, set the options you need and then call self.file_dialog.exec_() only in the real code (not in the tests).

Get content from open window in Linux

I want to collect data and parse it eventually from an open window in linux.
An example- Suppose a terminal window is open. I need to retrieve all the data that appears on that window. After retrieval, I would parse it to get specific commands entered.
So is it possible to do that? If so, how? I would prefer to use python to code this entire thing.
I am making a guess that first I would have to get some sort of ID for the open window and then use some kind of library to get the content from the window whose ID I have got.
Please help. I am quite a newbie.
You can (ab)use the assistive technologies support (for screen readers and such) that exist in the toolkit libraries. Whether it will work is toolkit specific—Gtk and Qt have this support, but others (like Tk, Fltk, etc.) may or may not.
The Linux Desktop Testing Project is a python toolkit for abusing these interfaces for testing GUI applications, so you can either use it or look how it works and do similar thing.
I think the correct answer may be "with some difficulty". Essentially, the contents of a window is a bitmap. This bitmap is drawn on by a whole slew of primitives (including "display this octet-string, using that encoding and a specific font"), but the window contents is still "just pixels".
Getting the "just pixels" is pretty straight-forward, as these things go. You open a session to the X server and say "given me the contents of window W" and it hands it over.
Doing something useful with it is, unfortunately, a completely different matter, as you'd potentially have to (essentially) OCR the bitmap for what you want.
If you decide to take that route, have a look at the source of xwd, as that does, essentially, that.
Do you have some sort of control over the execution of the terminal? In that case, you can use the script command in the terminal session to log all interaction to a file and then read and parse the file.
$ script myfile
Script started, file is myfile
$ ls
...
$ exit
Script done, file is myfile
$ parse_file.py myfile
If the terminal is running inside of screen, you have other options as well. Screen has logging built in, screen -X sends commands to a running screen session (man screen).

Python Desktop Integration - Drag and drop

I have a pygame window that I want to know when a file has been dragged and dropped onto it. I only need to be able to fetch the name of the file. How can this be accomplished?
Here's a forum thread that might be what you're looking for.
And another forum.
And a link to the msdn page. You'll probably want the pythoncom library.
one option for a similar effect is is to use pygame's scrap module so you can copy-paste into the window, your program would just need to look for ctr-V events.
On this XFCE desktop I'm using
If I hit ctrl-C with a file selected, the file name shows up when I type
pygame.scrap.init()
types= pygame.scrap.get_types()
print dict(
[type,pygame.scrap.get(type)]
for type intypes
)

Categories