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.


    Simple file rename script

    Pythonista
    1
    1
    2742
    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.
    • dgelessus
      dgelessus last edited by

      Quick and easy script to change the name of files and folders without Pythonista's automatic extension guessing thing. Allows "changing" a file's type from/to Python script, pyui file or plain text, renaming of unopenable files, and moving files and folders to another location.

      This script is meant to be short, easy to use and newbie-friendly. It does nothing that can't also be done with Shellista or StaSh, which are a better choice if you are a power user and have experience with bash and command lines.

      (GitHub)

      import console
      import editor
      import os
      import sys
      
      DOCUMENTS = os.path.realpath(os.path.expanduser("~/Documents"))
      if DOCUMENTS.startswith("/private"):
          DOCUMENTS = DOCUMENTS[len("/private"):]
      
      def main(args):
          try:
              # Source and destination are passed via runtime args
              src, dest = args
          except (TypeError, ValueError):
              # Get source and destination from user
              curfile = os.path.relpath(editor.get_path() or "", DOCUMENTS)
              
              shortsrc = console.input_alert(
                  "Source Name", # title
                  "Path is relative to Script Library root", # message
                  curfile, # input
              )
              src = os.path.join(DOCUMENTS, shortsrc)
              
              if not os.path.exists(src):
                  console.hud_alert("Source file does not exist", "error")
                  sys.exit(1)
              
              dest = os.path.join(DOCUMENTS, console.input_alert(
                  "Destination Name", # title
                  "Path is relative to Script Library root", # message
                  shortsrc, # input
              ))
          else:
              # Process source and destination from runtime args
              src, dest = os.path.join(DOCUMENTS, src), os.path.join(DOCUMENTS, dest)
              
              if not os.path.exists(src):
                  console.hud_alert("Source file does not exist", "error")
                  sys.exit(1)
          
          if os.path.exists(dest):
              console.hud_alert("Destination file already exists", "error")
              sys.exit(1)
          
          os.rename(src, dest)
          
          sys.exit(0)
      
      if __name__ == "__main__":
          main(sys.argv[1:])
      
      1 Reply Last reply Reply Quote 0
      • First post
        Last post
      Powered by NodeBB Forums | Contributors