-
Notifications
You must be signed in to change notification settings - Fork 15.3k
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
Method to get/set file pointers in clipboard #9035
Comments
EDIT: Not ideal, but on mac I think I've used something like: var filePath = clipboard.read('public.file-url').replace('file://', ''); to retrieve the path of the copied file. On windows: var rawFilePath = clipboard.read('FileNameW');
var filePath = rawFilePath.replace(new RegExp(String.fromCharCode(0), 'g'), ''); |
@bompi88 this works well! But I guess nobody know this, and can only get only one file's path if multi files are copied. There should be an API like An additional API like |
@evecalm From inspecting the clipboard with Apple's clipboard viewer sample app, it looks like you should be able to get the path of multiple files via This doesn't help with setting the clipboard though, seeing as Electron only allows you to copy plain text to the clipboard (not e.g. It'd be great if there were some way to set non-plaintext clipboard data. |
Actually, after further investigation, it looks like copying files to the clipboard programmatically is also totally possible (on macOS at least) using the experimental clipboard.writeBuffer(
'NSFilenamesPboardType',
Buffer.from(`
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>/path/to/file1.ext</string>
<string>/path/to/file2.ext</string>
</array>
</plist>
`)
) |
@timkendrick Awesome! But I guess we need a much more developer-friendly api 😟 |
@evecalm you can use the plist npm package to simplify working with plist-formatted strings: const plist = require('plist');
function hasClipboardFiles() {
return electron.remote.clipboard.has('NSFilenamesPboardType');
}
function getClipboardFiles() {
if (!hasClipboardFiles()) { return []; }
return plist.parse(electron.remote.clipboard.read('NSFilenamesPboardType'));
}
function setClipboardFiles(filePaths) {
electron.remote.clipboard.writeBuffer('NSFilenamesPboardType', Buffer.from(plist.build(filePaths)));
} |
@bompi88 I tried to write on Windows to |
It looks like when writing to |
how to get file paths in windows? |
You may try this on Windows: clipboard.readBuffer('FileNameW').toString('ucs2').replace(RegExp(String.fromCharCode(0), 'g'), '') |
HIHI,if i copy multiple files at the same time, how can i get all the file list on windows? |
@lizhengnacl haven't found a way :( |
+1 |
1 similar comment
+1 |
great, thanks, works like a charm on MacOS |
I found a format name 'FileGroupDescriptorW' for this purpose that lists the names of the files. But I did not have enough time to format the output of You can try to improve the regular expression in @bompi88 's answer.
If you can parse the output correctly please share it with us. Have a good day! |
I've been playing around with this code, and digging into Windows internals and this is what I found out. I found the list of supported formats in windows here -> I did find the If you dig deeper, their FieldDescriptor only has a the fileName and not the full path. You can verify this functionality by using the code above. It prints out only the file names and not their full path. (Along with other garbage characters) You can also verify this by going to powershell and typing There is one more format you can try as well. It seems that if you use this, it returns the full path but the encoding is all wonky as before. Try I mean you can keep going down the rabbit hole and see more info on clipboard transfering here. (On Windows) If you dig deeper into the clipboard from the MSDN docs. It says that if you use CF_HDROP that you can get the list of files and their paths somehow. Unfortunately, I don't think there is a way that node can get it other than creating a windows only native module in C++ or Go and calling it from node. We might be able to if we figure out how to decode the data from the buffer correctly, but for now I think the best way would be a |
How about this issue's progress?
|
Electron Clipboard, read the path of multiple copied files, still not solved? |
my solution (macOS && windows) : https://github.com/alex8088/clipboard-files support read or write file path for clipboard |
Anybody know an equivalent to |
how to get file paths in linux? |
@liyuzhao - if you figure it out, please do share. I spent some time trying, but never did succeed. It doesn't help that inspecting the buffer values in the clipboard is so damn difficult on Linux. On Windows/macOS there are great tools to explore all the formats contained in the clipboard, but not so on Linux. |
I wrote a library for this issue with some additional clipboard image processing features. https://www.npmjs.com/package/electron-clipboard-ex |
Hello everyone, after I upgrade to electron v13.1.7, |
@FatShen3 for an issue like that you definitely need to specify to OS you're seeing the issue on. |
Windows 10 Pro 20H2 19042.1288. The current electron version i use is
|
Huh, that's strange. What were you copying to populate |
In the code blow, I can wrtie file to system clipboard in const filePaths = ['C:/1.txt', 'C:/2.txt']
const str = '\u0014\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0001\u0000' + filePaths.join('\u0000') + '\u0000\u0000'
clipboard.writeBuffer('CD_HDROP', Buffer.from(str, 'ucs-2')) In the code blow, I can get multiple filePaths from system clipboard in const winFileStr = clipboard.readBuffer('CD_HDROP').toString('ucs2')
filePaths = winFileStr
.replace(/\u0014\u0000{7}\u0001\u0000/, '')
.split('\u0000') // split each filePath
.filter(e => e) // still get some \u0000, ignore them
// winFileStr is like ['C:/1.txt', 'C:/2.txt'] |
Hm... I'm able to make that work in Windows 7 using that code. Does this Electron-Fiddle (of your own code) also fail for you on Windows 10 @FatShen3? https://gist.github.com/65d60ea177626fba9d7145b782a6a3d4 I assume it will fail for you, even though it works for me, and if so I think we'll need to find where this issue was introduced and flag down a maintainer. |
Yes, it work in this way just in electron itself. But what i want is write file paths to system clipboard, if success, i can press |
This lib helps me out perfectly. Thanks a lot! |
i try |
|
Thank you ! This helps me a lot . |
thanks |
this code donot work for me why ? and paste item on windows context menu donot enable clipboard.writeBuffer('public.file-url', Buffer.from( |
on windows: it works for me |
Thank you very much. Your library has been a great help to me |
There is currently no mechanism to get/set file pointers from/to clipboard.
File pointers: The stuff in clipboard when you copy (
ctrl+c
) selected files in your OS file explorer.After ~15 min research, there was #2244 issue which went a different direction and than got closed. Also there are some projects on hold waiting for this kind of functionality, such as bortexz/quercus#1.
The text was updated successfully, but these errors were encountered: