开发者

Script to manage dated space on Mac OS X (10.6) for downloads

开发者 https://www.devze.com 2023-02-08 17:34 出处:网络
In the past I had created an applescript for datedspace but which is not running anymore. I would like to create an applescript that would look inside a folder ~/Documents/pool/ or ~/Downloads.

In the past I had created an applescript for datedspace but which is not running anymore.

I would like to create an applescript that would look inside a folder ~/Documents/pool/ or ~/Downloads.

Let's say we are on February 7, 2011.

  1. Check if the ~/Documents/2011/02/07/ exists
  2. If not create ~/Documents/2011/02/07/
  3. Move the files and/or folders inside ~/Documents/pool/ to ~/Documents/2011/02/07/

Bonus

  1. Create a text file in ~/Documents/2011/ with the name index.mdown and containing the following format
# Index 2011
## January
[…] 
## February 
* 2011-02-07: Foo.jpeg 
* 2011-02-07: Bar/ 

Lead or incomplete solutions are welcome.

Previous script

-- Script to automaticaly save documents in a date space.
-- Karl Dubost - http://www.la-grange.net/ - <karl@*******> - 2001 ©
-- Feel free to distribute it and modify it
-- Feel free to send me improvements 
-- ********************************************************
-- Version 1.1 2001-03-30
--      Add control on existence of folders year and month
--      Make it more general based on Startup Disk
-- Version 1.0 2001-03-29
--      Creation of the code
--      Thanks to Bill Briggs
--      http://maccentral.macworld.com/columns/briggs.shtml
-- ********************************************************
on adding folder items to this_folder after receiving added_items
    tell application "Finder"
        set yourDisk to name of startup disk as string
    end tell
    set todaysDate to (current date)
    set {d, m, y} to {day, month, year} of todaysDate
    set monthList to {January, February, March, April, May, June, ¬
        July, August, September, October, November, December}

    repeat with i from 1 to 12
        if m = (item i of monthList) then
            set monthString to text -2 thru -1 of ("0" & i)
            exit repeat
        end if
开发者_开发百科    end repeat

    set y to y as string
    set dayString to text -2 thru -1 of ("0" & d)
    set dayString to dayString as string
    set datedFolder to yourDisk & ":Documents:" & y & ":" & monthString & ":" & dayString & ":" as string
    set monthFolder to yourDisk & ":Documents:" & y & ":" & monthString & ":" as string
    set yearFolder to yourDisk & ":Documents:" & y & ":" as string
    set rootFolder to yourDisk & ":Documents:" as string

    tell application "Finder"
        if (folder datedFolder exists) then
            repeat with oneFile in added_items
                move oneFile to folder datedFolder
            end repeat
        else if not (folder yearFolder exists) then
            make new folder at folder rootFolder with properties {name:y}
        else if not (folder monthFolder exists) then
            make new folder at folder yearFolder with properties {name:monthString}
        else
            make new folder at folder monthFolder with properties {name:dayString}
            repeat with oneFile in added_items
                move oneFile to folder datedFolder
            end repeat
        end if
    end tell
end adding folder items to


I hope this will be good enough :-) : With the help of some basic command line applications it creates subfolders in your ~/Documents folder (mkdir -p: it will not overwrite existing directories!), moves files there (mv -n: not overwriting files with the same name), then creates monthly logfiles containing the names of addedItems and finally a index.mdown file with all monthly logfiles which should look like this:

2011/01/03: testfile9
2011/02/01: testfile10
2011/02/07: testfile11

Now go ahead and attach it to your folder, give it a try! (maybe uncomment baseFolder and change it to /tmp/test or something, or to POSIX path of thisFolder, if you want it to clean up the folder it´s attached to.)

property dateFormatForFolder : "+%Y/%m/%d"
property dateFormatForMonthlyLog : "+%Y/%m/"
property dateFormatForYearlyLog : "+%Y/"

on adding folder items to thisFolder after receiving addedItems
set baseFolder to POSIX path of (path to documents folder)
set todaysFolder to do shell script "/bin/date '" & dateFormatForFolder & "'"
set monthsFolder to do shell script "/bin/date '" & dateFormatForMonthlyLog & "'"
set yearsFolder to do shell script "/bin/date '" & dateFormatForYearlyLog & "'"
set fullPath to baseFolder & todaysFolder

createFolder(fullPath)

repeat with i from 1 to number of items in addedItems
set oneItemsPath to (quoted form of POSIX path of item i of addedItems)

try
    moveToFolder(fullPath, oneItemsPath)
end try

set fileName to name of (info for item i of addedItems)
set logThis to todaysFolder & ":   " & fileName
do shell script "echo " & logThis & " >> " & baseFolder & monthsFolder & "log.txt"

end repeat

do shell script "find " & baseFolder & yearsFolder & " -type f -name 'log.txt' -print0 | xargs -0 cat > " & baseFolder & yearsFolder & "index.mdown"

end adding folder items to


on createFolder(fullPath)
do shell script "/bin/mkdir -p " & quoted form of fullPath
end createFolder

on moveToFolder(fullPath, oneItemsPath)
do shell script "/bin/mv -n " & oneItemsPath & " " & fullPath
end moveToFolder
0

精彩评论

暂无评论...
验证码 换一张
取 消