-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathMonthly-Cleanups.applescript
77 lines (61 loc) · 2.91 KB
/
Monthly-Cleanups.applescript
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
(*
===============================================================================
Monthly Cleanup
===============================================================================
Version: 1.10 Updated: 05/15/19 21:48:10 CST
By: Kevin Funderburg
PURPOSE:
- Clean up old files in ~/Downloads or ~/Desktop by consolodating files
> 2 weeks old into a folder with the current month and year, then consolodate
that folder into a "Cleanups" folder.
- I've used this method for years to keep these folders tidy and I love it.
Example: ~/Downloads/Cleanups/05.19 - Monthly Cleanup/[old files]
~/Desktop/Cleanups/05.19 - Monthly Cleanup/[old files]
TO EXECUTE:
Can be called by a shell script with the folder that needs to be cleaned like
so:
osascript 'Monthly Cleanups.scpt' "Desktop"
VERSION HISTORY:
1.0 - Initial version.
===============================================================================
*)
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
on run argv
if item 1 of argv = "Desktop" then
set rootFolder to (path to desktop folder)
else if item 1 of argv = "Downloads" then
set rootFolder to (path to downloads folder)
end if
set t to POSIX path of rootFolder
set oldFiles to {}
set mydate to current date
set twoweeksago to mydate - (14 * days)
set thisMonth to (month of mydate) as integer
if thisMonth < 10 then set thisMonth to "0" & thisMonth
set thisYear to characters 3 thru -1 of ((year of mydate) as string) as string
set shortdate to thisMonth & "." & thisYear as text
try
set MonthlyCleanupFolder to POSIX path of rootFolder & "Cleanups/" & year of mydate & "/" & (shortdate & " - Monthly Cleanup")
try
do shell script "mkdir -p " & quoted form of MonthlyCleanupFolder
end try
tell application "Finder"
-- Get every file that is not the cleanups folder
if item 1 of argv = "Downloads" then
set oldFiles to every item of rootFolder whose (name does not contain "Cleanup") and creation date < twoweeksago
else if item 1 of argv = "Desktop" then
-- I have a "Screen Shots" folder on my Desktop that i dont want to be touched
set oldFiles to every item of rootFolder whose (name does not contain "Cleanup" and name does not contain "Screen") and creation date < twoweeksago
end if
if (count of oldFiles) > 0 then
move oldFiles to POSIX file MonthlyCleanupFolder as alias
display notification ((count of oldFiles) & " files archived to clean up folder") as text with title "Archive Old " & item 1 of argv & " Files" subtitle "Success" sound name "Blow"
else
display notification "No files cleaned up" with title "Archive Old " & item 1 of argv sound name "Frog"
end if
end tell
on error errMsg number errorNumber
display notification errMsg with title "Monthly Cleanup" subtitle "Error number: " & errorNumber sound name "Basso"
end try
end run