Skip to content

Instantly share code, notes, and snippets.

@montethinks
Created October 5, 2018 18:34
Show Gist options
  • Save montethinks/2bf9d12ed831b1aefb0ecf704f4038bd to your computer and use it in GitHub Desktop.
Save montethinks/2bf9d12ed831b1aefb0ecf704f4038bd to your computer and use it in GitHub Desktop.
Quick and dirty removing screenshots from mac OS desktop
require "fileutils"
class CleanDesktop
EXTENSIONS = %w(.png .jpg .jpeg)
attr_reader :directory_name
def initialize(directory_name)
@directory_name = directory_name
end
def run
create_bucket(directory_name)
move_files!
print_result
end
def print_result
puts "#{checked_files ? 'Something went wrong' : 'Transfer Complete' }"
end
def create_bucket(path)
if Dir.entries(Dir.home).include? "#{path}"
puts "'#{path}' directory already exists"
puts "Continuing file transfer"
puts ""
else
puts "Creating directory #{path}."
FileUtils.mkdir(path)
end
end
def desktop_files
return @files if defined? @files
desktop = File.join(Dir.home, "Desktop")
@files = Dir.entries(desktop).select { |file| EXTENSIONS.include?(File.extname(file)) }
end
def source_files
desktop = File.join(Dir.home, "Desktop")
desktop_files.map { |p| File.join(desktop, p) }
end
def destination
File.join(Dir.home, directory_name)
end
def destination_files
source = File.join(Dir.home, directory_name)
Dir.entries(source)
end
def checked_files
checked = destination_files.each_with_object([]) do |el, results|
results << desktop_files.member?(el)
end
checked.all?
end
def move_files!
FileUtils.mv source_files, destination if confirmed?
end
def confirmed?
puts "File list"
puts desktop_files
STDOUT.puts "Ready for transfer. #{desktop_files.size} files will be moved. Do you want to proceed? (y/n)"
response = STDIN.gets.strip
return true if response == 'y'
abort "You chose not to transfer. Quitting the transfer now."
end
end
CleanDesktop.new("screen_shots").run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment