omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Increment and Save

    Pythonista
    3
    7
    5483
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • wm
      wm last edited by

      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.

      https://gist.github.com/weakmassive/2400bcca3ae2c0a6a43c

      1 Reply Last reply Reply Quote 0
      • ccc
        ccc last edited by

        Cool. Check out os.path.splitext() because it might help to simplify the logic.

        1 Reply Last reply Reply Quote 0
        • wm
          wm last edited by

          Thank you! I've revised the script based on your comments over on Gist.

          1 Reply Last reply Reply Quote 0
          • briarfox
            briarfox last edited by

            @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.

            1 Reply Last reply Reply Quote 0
            • wm
              wm last edited by

              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)[0]
              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])[1]
              		noext = files[x][:-len(ext)]
              		
              		try:
              			num = re.split('([0-9]+)$',noext)[1]
              			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)
              
              1 Reply Last reply Reply Quote 0
              • ccc
                ccc last edited by

                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)
                
                1 Reply Last reply Reply Quote 0
                • wm
                  wm last edited by

                  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.

                  1 Reply Last reply Reply Quote 0
                  • First post
                    Last post
                  Powered by NodeBB Forums | Contributors