Skip to content

Instantly share code, notes, and snippets.

@melaniehoff
Last active June 9, 2019 00:09
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melaniehoff/0752e241adf2f6d7da5edcd89287ecc3 to your computer and use it in GitHub Desktop.
Save melaniehoff/0752e241adf2f6d7da5edcd89287ecc3 to your computer and use it in GitHub Desktop.
A *truly* Random Folder
import glob
import random
import shutil
import os
import pickle
## Make an empty folder called Random and put it on your desktop
## Make an empty file called all_my_files.txt and put it in the same dir as random_folder.py
## Replace my file paths with yours
pathToRandom = '/Users/YOURUSERNAME/Desktop/Random'
pathToAllFiles = '/Users/YOURUSERNAME/WHEREVER/YOU/PUT_THIS_FILE/all_my_files.txt'
rootPath = '/Users/YOURUSERNAME/**/*' # this is the path to all the files in your hd
## To run this code:
## 1. Run Part I, then comment Part I out
## 2. Uncomment Part II, then run again and check your Random folder
#####################################################
####################################################
# PART I -- this could take a long time. for me, 15 minutes
all_files = []
for filename in glob.iglob(rootPath + '.gif', recursive=True):
# print(filename)
all_files.append(filename)
for filename in glob.iglob(rootPath + '.jpg', recursive=True):
# print(filename)
all_files.append(filename)
for filename in glob.iglob(rootPath + '.png', recursive=True):
# print(filename)
all_files.append(filename)
with open(pathToAllFiles, "wb") as fp: #Pickling
pickle.dump(all_files, fp)
# ###################################################
# ##################################################
# ## PART II
# with open(pathToAllFiles, "rb") as fp: # Unpickling
# all_files_pickle = pickle.load(fp)
# # first delete whats in Random/
# folder = pathToRandom
# for the_file in os.listdir(folder):
# file_path = os.path.join(folder, the_file)
# try:
# if os.path.isfile(file_path):
# os.unlink(file_path)
# elif os.path.isdir(file_path): shutil.rmtree(file_path)
# except Exception as e:
# print(e)
# rand_files = random.sample(all_files_pickle, 12)
# # print(rand_files)
# source = rand_files
# destination = pathToRandom
# for files in source:
# shutil.copy(files,destination)
# ## still working on symlinks
# # try:
# # os.symlink(files, destination)
# # break
# # except OSError as e:
# # if e.errno != os.errno.EEXIST:
# # raise
# # # time.sleep might help here
# # pass
# print()
# print("+++++ 12 Random Pics")
# print()
on closing folder window for this_folder
tell application "Terminal"
do shell script "say random"
do shell script "/usr/local/bin/python3 /Users/YOURUSERNAME/WHEREVER/YOU/PUT_IT/random_folder.py"
end tell
end closing folder window for
@melaniehoff
Copy link
Author

Place the scpt file in here:
Go to folder: /Library/Scripts/Folder Action Scripts

right click on your Random folder and select folder action scripts. add random_folder_action.scpt to Random/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment