Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts

Thursday, December 3, 2015

Automating Password Management

I have a password that I don't particularly need to be super secret, but I don't really want to memorize another password either.

I  put together a script that will automatically copy the password to the clipboard (essentially a Control + C command of text). I threw this into a project folder and I can run the script quickly by double clicking it.

For the sake of slight security, I named the file something boring and unrelated.

Code

from Tkinter import Tk
r = Tk()
r.withdraw()
r.clipboard_clear()
r.clipboard_append('password')
r.destroy()


Setup

  • Install Python if it's not already installed
  • Copy the code above
  • Paste into Notepad or another text editor
  • Replace the highlighted password with your own password
  • Save as type: All Files (*.*)
  • File Name: "SomeFileName.py"
    • Wrapping the filename in quotes ensures the file type will be set to .py - a python script file
  • Double click on the file to run it. A Python window will briefly open and close automatically
  • Navigate to a text area and hit Ctrl+V or use some other paste command

Sunday, June 7, 2015

Make New Project Directory

I like to organize photos, videos, GIS projects, and developments projects in separate "project directories." Each project folder has the following format:
YYYY-MM-DD - Description

File Structure Example:
  • [Drive]:\
    • Development
      • 2014-10-04 - Scuba Calculators
      • 2015-06-01 - Make New Project Directory
    • GIS
      • 2012-05-14 - Whatever Project...
        • Shapefiles
        • Exports
        • From Client
        • MXDs
    • Photos
      • 2013-04-05 - Birthday in Midtown
        • Hilights
          • Edited
        • Original
      • ...
    • Videos

This is an easy way to chronologically catalog projects within their respective folders, but I'm lazy, and I hate having to look down at my task bar clock to find out what the date is, and I can't stand typing in that endless date!

Solutions Posted on GitHub

I posted all the scripts I came up with for this on my GitHub page under the Make New Project Directory repo.

Batch File

I originally tried coming up with a Batch file (.bat) to add the folder using the format I wanted. It's easy to make a predefined folder structure using CMD commands, but pulling that date style turned out to be a little tricky, so I looked elsewhere.

Python Script

I came up with a great Python script that will automatically add a folder that's named with the current date followed by XXXXDescriptionXXXX (so I could easily find and rename it further). Just double click the python file and the new project folder is added. Additionally, I repeated the code to add subdirectories for common sub-folders: Exports and Shapefiles for GIS projects; Originals, Highlights, and Edited for photos & video; etc.


Pros:

  • This is great! It works automatically
  • Simple code. It executes in 6 lines.  View the code here

Cons:

  • I quickly realized how awfully tedious it is to have to slow double click to rename, something every time I create a new project. I need some kind of interface to enter a project name, and I haven't had great luck using TkInter yet.
Note: I've heard good things about PyQt for an interface
  • I don't necessarily want those sub-directories for every project

VBScript

My best solution so far was to write a VBScript (.vbs format) to prompt me for a project name AND ask if I want to add those common project sub-folders.






Pros:

  • Most efficient method so far
  • Quick, easy, no renaming
Cons:


  • Slightly longer code (24 lines without comments & line continuation), but it does include some more logic handling. View the code here.
  • This is still generalized, and the "common subdirectories" option is hard coded (i.e. I don't need a Shapefiles directory in a video editing project).
Overall, This VBScript method opens the door to writing a slightly larger executable in Visual Studio with a more dynamic interface to add any kind of project folder - but that moves away from the original requirement of a highly rapid way to add a project folder automatically. I think this nails it.




    Tuesday, March 10, 2015

    Make New Directory Script

    I like to keep my projects organized by date. I  name new project folders with this format inside a "Project" folder on the root of my data drive:

    YYYY-MM-DD - Project Description

    It'll end up like this:

    D:\Projects\2015-03-10 - Make New Directory Script\

    It's an easy way to keep things consistently organized. I'll usually have some ambiguous reference directories in there that I don't want to be mixed in with regular projects (e.g. development, temp data, snippets, links, tools, logos, notes, etc.).

    Shortcut Method

    Another method is to add a Date Created sorting option to your file explorer window, but this may not help keep projects separated from those reference directories:
    • Open Windows Explorer
    • Press Alt + v to open the View menu, and choose the Details view. Sorting options will appear over the main file area of the window
    • Right click on that details bar and choose More...
    • Scroll down and check Date created to add it as a sorting option. Look around the Details menu and select/deselect parameters that are useful or unhelpful

    Make a Formated Folder with Python

    Let's get real. I like my method.

    I'll also be honest: I'm not saving any real time here.  I can easily click the New Folder button and immediately start typing a folder name, but I have to look ALL the way down at the clock to verify the date, then I gotta type all 13 characters of the date prefix. I'm over it.


    I threw these two tricks together in five lines using Python's os and datetime modules - And 40% of that just calls those modules!

     import os  
     import datetime  
     today = datetime.date.today()  
     newpath = today.strftime('D:\Projects\%Y-%m-%d - Description')  
     if not os.path.exists(newpath): os.makedirs(newpath)  
    

    Line 3 prepares the today() date object
    Line 4 formats the path and embeds the 4-digit year (capital %Y), etc.
    Line 5 even checks to  make sure the path doesn't already exist.

    Killer.

    As long as you have Python installed, you can save this code as makeNewDirectory.py and run it. It should take just a fraction of a second to run.

    Batch File

    I was trying to run this as a .bat file, but it was kind of tricky and I got bored. Here's what I came up with.

    mkdir easily makes a new directory, and this comment on StackOverflow pulls apart a %date% stamp with concatenation. I got them to work separately, but not together. Play around with underscores and let me know what you can find.