-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: support for fileURL pasteboard type #9
Comments
Depending on what Python type the output should be, it shouldn't be too hard. |
Hmm. I would imagine the interface would look something like: # Set a file to the clipboard
pb.set_contents('file:///Users/path/to/file.ext', pasteboard.FileURL)
# Get the contents
pb.get_contents(pasteboard.FileURL)
# 'file:///Users/path/to/file.ext'
I'm not sure I totally understand the question. I think it's sufficient to just test/return the clipboard contents, which should just be the file path in the case of a fileURL type. Should work pretty much the same as a unicode string. I don't think there's anything special or extravagant that needs to be accounted for. Although, I'm not 100% sure what typical programs look for when pasting files, if you mean test it in that way. When I copy from finder, as shown in the image above, Apple decides to place many representations at the same time, including I'm guessing that the presence of representation of |
Perfect, sorry, that what I meant by "How do I test/trigger a paste with a file URL?". I should've said "what steps I'd have to do so the pasteboard is populated with an entry of |
The latest release has support for FileURLs (it's up on PyPI). |
Thanks much for this @tobywf Can confirm the I was wondering: is it possible to set this format on the clipboard? |
I'm sure it is possible, but it might need some design/thought/testing. I'm not well-versed in macOS/AppKit API limitations, and the API documentation is shockingly barren, e.g. writeObjects. I guess the first question would be "Why?". From a bit of testing when I was implementing get, it does seem to be sufficient to set Which brings us to the technical side. Do the FileURLs have to exist, and do bad things happen if they don't? Presumably not, because you could delete a file after setting the value. I do know (by accident) you can only set absolute FileURLs, which makes sense if you think about it, but couldn't find it mentioned anywhere. I'm honestly not sure what/how much validation is done by either It might be a good idea for I might have a play around with this when I have time, but help answering these questions would be appreciated. The other alternative is for you to use PyObjC if you have a specific use-case and want more direct control. |
Yeah, tell me about it 😞 -- I've been struggling for a couple weeks to find good examples of interacting with the pasteboard, and the closest I've found is some antiquated example code for an application called ClipboardViewer which uses deprecated APIs.
I'm looking for this capability for a program I'm writing that interacts with the clipboard across platforms. One feature is the ability to copy/paste files to/from the clipboard from a CLI or Python interface and that action should be compatible with graphical file explorers (or other programs that accept the file Clipboard format), like explorer on Windows or Finder on MacOS. Admittedly, I think the most common case would be reading this after somebody copies it, which is covered in the latest release. But I would like to have bidirectional, which I'm able to do so far in Windows and Linux. The ecosystem for MacOS is sparse and, unfortunately, I've not touched Objective-C until a few weeks ago :-)
Yeah,
I wonder if that's really necessary though. My thought is that you could implement it in Python. As I understand it, For example, in from ._native import * Instead, you could have this code in, say, a For example, maybe something like this: from pasteboard import Pasteboard as _Pasteboard # or from _pasteboard internally to this project ;)
from typing import Union, Sequence, Optional
import pathlib
class Pasteboard:
def __init__(self, *args, **kwargs):
self._pb = _Pasteboard(*args, **kwargs)
...
def get_file_urls(self, diff: bool=False):
files = self._pb.get_file_urls(diff)
if files:
return tuple(pathlib.Path(f) for f in files)
return tuple() # a gentle suggestion :-)
def set_file_urls(self, paths: Sequence[Union[str, pathlib.Path]]) -> bool:
urls = tuple(pathlib.Path(p).as_uri() for p in paths)
... # do more validations?
self._pb.set_file_urls(urls) Subclassing could also work cleanly, I think. |
I dislike the split Python/native approach, having to wrap everything is meh. I guess inheriting from the (C) object might be possible?! Maybe that kind of protection isn't needed and I was overthinking it. Consumers could try and write what they want; it'll just fail. I hit a bigger problem though. Ostensibly you're supposed to use |
Hi there, thanks so much for making this package!
I have a request and I hope it's not too much trouble to implement. I was wondering if the fileURL pasteboardtype could be supported.
This way, it can be easy for my program to work with files that are copied from finder.
Inspecting the clipboard after copying a file from finder shows that it's exposed, among other things, as a file url type. It would be nice to be able to retrieve these contents using
pasteboard
The text was updated successfully, but these errors were encountered: