jamiew (owner)

Fork Of

Revisions

gist: 138997 Download_button fork
public
Public Clone URL: git://gist.github.com/138997.git
Ruby
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env ruby
# sup luke; jdubs here
# - put the docs and helper scripts inline for syntax highlighting joy
# - added another crontab example -- I like to file once/day.
# plays nice with treating the Desktop like a daily scratchpad
# - added a 'Yesterday' link in addition to Today
# - minor tweaks around town
 
## config
HOME_DIRECTORY = "~" # this will
 
DESKTOP_DIRECTORY = "#{HOME_DIRECTORY}/Desktop"
STORAGE_DIRECTORY = "#{HOME_DIRECTORY}/Documents"
LOG_DIRECTORY = "#{HOME_DIRECTORY}/.bin"
SHELL = "/bin/bash"
 
## docs
=begin
 
drefile: an automatic, chronological file organizer
---------------------------------------------------
Every few minutes drefile copies all files on your Desktop to a folder
such as ~/Documents/2009-06-June/26-Friday, prepending a timestamp
(like 20090626.1310) to the filename.
 
Use
---
1. Put files on your Desktop, and in a few minutes they will be 'drefiled'
2. Use the 'Today' alias on your Desktop to go to today's folder
3. Type 'dday' to go to today's directory
4. Type 'dmonth' to go to this month's directory
5. Type 'drefile file' to manually timestamp + move a file
 
Install
-------
1. `sudo port install osxutils`
2. `sudo gem install open4`
3. copy the 3 files below to ~/.bin (or whatever, in your path)
4. `chmod +x` the 3 files
5. `crontab -e` this (as your user):
 
# drefile desktop every 5 minutes (on the 5)
0,5,10,15,20,25,30,35,40,45,50,55 * * * * ~/.bin/drefile >> /Users/luke/.bin/drefile_cron.log
 
# drefile desktop once/day (at 5am)
* 5 * * * ~/.bin/drefile >> /Users/luke/.bin/drefile_cron.log
 
 
dday.rb
-------
#!/usr/bin/env ruby
 
require "fileutils"
 
month_and_day = Time.now.strftime("%Y-%m-%B/%d-%A")
dir = "/Users/luke/Documents/#{month_and_day}"
FileUtils.mkdir_p dir
puts "cd #{dir}"
 
dmonth.rb
---------
#!/usr/bin/env ruby
 
require "fileutils"
 
month = Time.now.strftime("%Y-%m-%B")
dir = "/Users/luke/Documents/#{month}"
FileUtils.mkdir_p dir
puts "cd #{dir}"
 
drefile.rb
-------
=end
 
  
require 'rubygems'
require 'open4'
require 'logger'
require "fileutils"
require 'time'
 
if ARGV.length == 0
  # make the drefile dir
  month_and_day = Time.now.strftime("%Y-%m-%B/%d-%A")
  drefile_directory = "#{STORAGE_DIRECTORY}/#{month_and_day}"
  FileUtils.mkdir_p(drefile_directory)
  #timestamp_prefix = Time.now.strftime("%Y%m%d.%H%M")+' '
  timestamp_prefix = '' #i don't like the prefixes, just the directories
 
  # remove the today alias
  FileUtils.rm("#{DESKTOP_DIRECTORY}/Today") if File.exists?("#{DESKTOP_DIRECTORY}/Today")
 
  # move files with a datetime prefix
  Dir.entries(DESKTOP_DIRECTORY).reject{|f| f =~ /^\./}.each do |file_name|
    FileUtils.mv("#{DESKTOP_DIRECTORY}/#{file_name}", "#{drefile_directory}/#{timestamp_prefix}#{file_name}")
  end
 
  # remake the today alias
  `/opt/local/bin/mkalias -r "#{drefile_directory}" "#{HOME_DIRECTORY}/Desktop/Today"`
else
  # make the drefile dir
  timestamp = Time.parse(Open4.open4("mdls -name kMDItemFSCreationDate -raw #{ARGV.first}")[2].read)
  drefile_directory = "#{STORAGE_DIRECTORY}/#{timestamp.strftime("%Y-%m-%B/%d-%A")}"
  FileUtils.mkdir_p(drefile_directory)
 
  new_file = "#{drefile_directory}/#{timestamp.strftime("%Y%m%d.%H%M")} #{ARGV.first}"
  FileUtils.mv(ARGV.first, new_file)
  puts new_file
end
 
Text only