Sublime Forum

Sublime.get_clipboard() not working properly

#1

I’m trying to create a sublime text 3 plugin that copies code from files like .html, .css, .js & .php and then pastes it in a txt document.

My problem is that sometimes when I copy code from a document with a lot of code and paste it into a text document, some of the code disappears.
When I just copy text from a txt file with 300 lines it works fine but when I do the same with .php or .js it doesn’t work, I only get 50-100 of the 300 lines.

import sublime
import sublime_plugin

def save_code(file):
    sublime.active_window().active_view().set_status("","Your file is
    saved inside _templates " + file + "!")
    text_file = open("path_to_file/_templates"+file, "w")
    my_copy = str(sublime.get_clipboard())
    text_file.write("%s" % my_copy)
    text_file.close()

class MycopypasteCommand(sublime_plugin.TextCommand):
    def run(self, edit):
    self.view.window().show_input_panel("Add
    filename:","",save_code,None,None)
0 Likes

#2

Are you trying to copy the whole contents of the file? Maybe try something like this instead.

 view = self.window.active_view()
 my_copy = view.substr(sublime.Region(0, view.size()))
1 Like

#4

Sometimes, yes but not always.
If my_copy’s length is higher than 1013, my_copy will return empty and I don’t understand why. This is when I use get_clipboard.

0 Likes

#5

Are there perhaps null bytes in the clipboard data?

0 Likes

#7

Just realized the problem, when I try to copy text that contains “Åäö”, the code will not work.

so now I use text_file.write("%s" % my_copy.encode(‘utf-8’)) it’s kinda works except that I get “\n” every where there is a space or tab…

0 Likes

#8

Solution:
text_file = open(“path_to_file/_templates”+file, “w”, encoding=“utf-8”)

0 Likes