Skip to content

Instantly share code, notes, and snippets.

@ncooke3
Last active September 23, 2019 03:40
Show Gist options
  • Save ncooke3/8edb7bdb7d9867b6f6ad20c212f4a6ef to your computer and use it in GitHub Desktop.
Save ncooke3/8edb7bdb7d9867b6f6ad20c212f4a6ef to your computer and use it in GitHub Desktop.
Simple python script that formats "Hello World" to "hello_world" and copies formatted string to clipboard!
# Hey! This is a script that formats a string to the lowercased, underscore-joined strings frequently used in python.
# On mac, copy this file into your /usr/local/bin directory!
# You can then run it anywhere in terminal with:
# >>> python pythonalize.py "The string you want formatted!"
# The string you pass in will be automatically copied to your Mac's clipboard!
import subprocess
import sys
def convert_to_file_name_syntax(name):
words = name.split(" ")
for index, word in enumerate(words):
words[index] = word.lower()
return "_".join(words)
def write_to_clipboard(output):
process = subprocess.Popen(
'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode('utf-8'))
formatted_file_name = convert_to_file_name_syntax(sys.argv[1])
write_to_clipboard(formatted_file_name)
print(formatted_file_name + " copied to clipboard!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment