Increment and Save
This is a script that you can add to the Actions menu to duplicate the current script and add a number to it (or if it's numbered already, add one). I wanted a quick way to keep multiple iterations of a script.
I just started learning Python last week, so I'm sure I could do things differently (let me know!), but it seems to working for my purposes and I didn't see anything similar already on here.
Cool. Check out
os.path.splitext()because it might help to simplify the logic.
Thank you! I've revised the script based on your comments over on Gist.
@wm neat script. I had used a similar method (manually changing names) when I first started using pythonista. You may want to check out the StaSh project, as it has a limited git. git is nice because it allows you to commit changes (create save points) in a project and roll back/see revision history with ease.
Thanks briarfox, I'll check out StaSh.
I noticed my script wouldn't increment correctly if a file ending in a higher number already existed. For example, if I've got the files a1, a2, a3 and use the script on a2, it would give me "a3 1".
I've attempted to fix this and it seems to be working but the code is not at all elegant. Any tips on easier ways to do this? Again, I'm really new to this so sorry for the crazy code! I don't really know what I'm doing. I also noticed it doesn't work in folders, but that's for another day.
Here's the code:
import os, sys, editor, re filepath = editor.get_path() filename = os.path.basename(filepath) ext1 = os.path.splitext(filepath) noext = filename[:-len(ext1)-1] noextnonum = re.split('([0-9]+)$',noext) namelen = len(noextnonum) #filepath stuff path = os.path.dirname(filepath) #editor.reload_files() files = os.listdir(path) text = editor.get_text() if not text: sys.exit('No text in the Editor.') alist =  x = 0 while x<=len(files)-1: if noextnonum == files[x][:namelen]: ext = os.path.splitext(files[x]) noext = files[x][:-len(ext)] try: num = re.split('([0-9]+)$',noext) alist.append(int(num)) filename = noextnonum + str(max(alist)+1) + ext except: filename = noextnonum + str(1) + ext x = x + 1 editor.make_new_file(filename, text)
I would suggest that you use a timestamp in the filename instead of a serial number of you want to avoid all possible name collisions. (See my other solution.). I like the following approach but it does not avoid the "a3 already exists" name collision.
import editor, os, re, sys text = editor.get_text() if not text: # fast fail sys.exit('No text in the Editor.') def next_filename(filename): filename, ext = os.path.splitext(os.path.basename(filename)) num = re.split('[^\d]', filename)[-1] if num: return filename[:-len(num)] + str(int(num)+1) + ext else: return filename + '_1' + ext editor.make_new_file(next_filename(editor.get_path()), text)
And this one adds the solution to the "a3 already exists" problem.
import os, sys, editor, re text = editor.get_text() if not text: # fast fail sys.exit('No text in the Editor.') def next_filename(filename): filename, ext = os.path.splitext(os.path.basename(filename)) num = re.split('[^\d]', filename)[-1] if num: filename = filename[:-len(num)] + str(int(num)+1) + ext else: filename += '_1' + ext if os.path.exists(filename): return next_filename(filename) return filename editor.make_new_file(next_filename(editor.get_path()), text)
Oh thank you - this is perfect! I knew there had to be an easier way than looping every file.
It's just my personal preference to have the numbered files. There's an "increment and save" function in Adobe After Effects that I use for my job and I just wanted to replicate that. And on my phone the I can't really read the long timestamps.