Sublime Forum

(ST3) Pywin32 Plugin Beta: pywin32 support in Sublime

#1

I know this gets asks occasionally: “How do I import win32api?”. Well you can’t…at least not out of the box. This is where Pywin32 comes in. I recently started throwing together a plugin that needed pywin32 so I could show Windows notification bubbles from sublime:

So I threw this together.

example to show path of all open explorer windows:
[pre=#232628]import sublime_plugin
import Pywin32.setup
from win32com.client.gencache import EnsureDispatch

def run():
for w in EnsureDispatch(“Shell.Application”).Windows():
print(w.LocationName + “=” + w.LocationURL)

run()[/pre]

Output:

User=file:///C:/Users/facelessuser/AppData/Roaming/Sublime%20Text%203/Packages/User A Test Plugin=file:///C:/Users/facelessuser/AppData/Roaming/Sublime%20Text%203/Packages/A%20Test%20Plugin Downloads=file:///C:/Users/facelessuser/Downloads

Notice, you just need to call import Pywin32.setup once before your plugin accesses any win32api, win32com, etc. calls. Technically it only needs to be called once when Sublime starts, but there is no guarantee your plugin will start after Pywin32 gets loaded. That is why your plugin just needs to make sure by manually including it.

  • This is only for ST3

  • Not everything may work. I have only tested what I needed to use.

Good luck! And I am not responsible if something breaks :smiley:.

repo: github.com/facelessuser/Pywin32

1 Like

Sublime.get_clipboard() return file path
#2

Interesting. Another step closer to finally being able to utilize Sublime Text as a cross-platform shell replacement / portable environment platform. At least reducing the alt+tab frequency while working. :smile:

0 Likes

#3

I am interesting to see what people may do with this. This does open up a number of possibilities on Windows.

0 Likes

#4

I need to get plain text from clipboard (sublime.get_clipboard() not work with copied file object Sublime.get_clipboard() return file path),
but sth like this doesn’t work:
OpenClipboard()
d=GetClipboardData(win32con.CF_TEXT) # get clipboard data
SetClipboardData(win32con.CF_TEXT, “Hello”) # set clipboard data
CloseClipboard()

0 Likes

#5

Why try to extract the file path from a file object in clipboard? Why not just hold shift in explorer, right click the file, and select “Copy as path”?

If this is unacceptable, I would try and read through the pywin32 documentation. I frankly don’t have the time to be a consultant for pywin32, I simply provide the package to help people out. I’m sorry I cannot be of more help right now.

0 Likes

#6

subscribing~

0 Likes

#7

Thinking about dropping support for this. I only have one plugin that uses it, and I am in the process of moving to using ctypes instead. There looks to be maybe around 400 installs on Package Control, not sure how many actively even use this.

As I can do most things with ctypes directly, which saves me this extra dependency, I have no personal use for it anymore; therefore, I have no desire to continue supporting it. My support for packages is usually tied to my personal usage. If anyone wants to take over, let me know. I think once I finish getting my plugin weened off this, I will give it a month to see if anyone wants to take it over, if not, I may transfer it over to SublimeText organization or just kill it off completely.

To be honest, if someone where taking this over, it would make more sense to implement it as a package control dependency than its current usage model; maybe I will kill it off and let someone re-implement it as a dependency if there is a strong enough desire for it.

0 Likes

#8

I can easily translate the package into a dependency since I spent quite a bit of time with Pc’s internals in that regard and know how it works. I don’t think I would be able to provide support however (never used itit so far) , so moving it to the sublime text org is probably better.

0 Likes

#9

That would be great. I figure the people who can’t figure out how to get things working with ctypes and want to use PyWin32 can just contribute to the Sublime Text org if they run into troubles with it. That way the people who are actually using it can make things better. As I have spent more time with ctypes, it’s really not that hard to use instead of PyWin32, though you are working with less of an abstraction layer making you do a little more reading. So yeah, PyWin32 can be more accessible at times, but not having the extra dependency is nice as well.

On a side note, I have been looking into dependencies, for Package Control. How do you setup Package Control to test out dependencies locally? I wouldn’t mind setting up PyWin32 myself, but I’m just not sure yet how you validate it locally.

0 Likes

#10

[quote=“facelessuser”]
On a side note, I have been looking into dependencies, for Package Control. How do you setup Package Control to test out dependencies locally? I wouldn’t mind setting up PyWin32 myself, but I’m just not sure yet how you validate it locally.[/quote]

I don’t think it’s m.tiond anywhere, but you can add channel or repository JSON files to PC’s config that are on your hard drive. I used that to test much of the dependency installation stuff and quicks that existed in 3.0.0 and have been fixed in 3.0.1. Unfortunately, I couldn’t seem to get PC to install packages from the hard drive, so I had to upload the test package to a web server.

Following is an excerpt of my local .json file that I added to PC’s “repositories” setting:

[code]{
“schema_version”: “3.0.0”,

"packages": 
    {
        "name": "RequestsTest",
        "author": "me",
        "releases": 
            {
                "sublime_text": "*",
                "url": "<url to the .sublime-package file I uploaded on some server>",
                "version": "1.0.0",
                "date": "2015-01-10 22:38:02"
            }
        ]
    }
],

"dependencies": 
    {
        "name": "requests",
        "load_order": "50",
        "description": "Python requests module",
        "author": "FichteFoll",
        "issues": "https://github.com/packagecontrol/requests/issues",
        "releases": 
            {
                "base": "https://github.com/packagecontrol/requests",
                "tags": true,
                "sublime_text": "*"
            }
        ]
    }
]

}
[/code]

Note: “m.tioned” was required to trick the utterly stupid and annoying “kit*n spam filter”. I get so mad whenever I see it.

And yes, I did delete remote tags a couple times.

0 Likes

#11

Hmm. Not as straight forward as I hoped.

Alright, well, I at least have a plan now. My plugins are all no longer using PyWin32 (unreleased), so when I get some time to tackle this, I will start looking into dependencies. I will just upload it to Sublime Text org and add to PC’s dependencies when I am done.

0 Likes

#12

I am going to start transferring this over to SublimeText org. Not sure when I will get time to convert this to a dependency, but when I get time, I will probably do that.

0 Likes