Sublime Forum

Temporary file generator

#1

To be honest and straight, i’m looking for someone who can help me to finish my plugin.

So what i planed to do, is:

  1. create a new file with a random name
  2. send some text into the new file
  3. if i close the file (on_close(view) ?) delete the file from the drive

UPDATE:

I managed to do 1) and 3). Anyone can help me out with 2) ?

import sublime, sublime_plugin, random, os, re

class TempfileCommand(sublime_plugin.WindowCommand):
	def run(self):
		rand = random.randrange(1,1000000)
		path = "N:/ahk work/" + str(rand) + ".ahk"
		v = self.window.open_file(path)



class TempfileEvent(sublime_plugin.EventListener):
	def on_close(self, view):
		fullpath = view.file_name()
		match = re.match('.+\\d+.ahk$', fullpath)
		if match:
			os.remove(fullpath)
0 Likes

#2

Maybe you’ll want to use the tempfile module from python’s standart library:

docs.python.org/library/tempfile.html

0 Likes

#3

It sounds like you want a temporary view where you can stick some text and not be asked about saving it on closing. Is that right?
If so then maybe you’d be better off with a scratch view, like the find results.

0 Likes