dgelessus
Feb 01, 2015 - 16:27
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:])