Это пример сценария, который будет перемещать любые новые элементы, добавленные в любую папку, к которой прикреплен этот сценарий.Вам, конечно, придется заменить /Users/USERNAME/Documents/Weeklies
на любую папку, в которой вы храните свои еженедельники. Это может быть рабочий стол, поскольку скрипт будет влиять только на новые элементы.
-- move any new items into Weekly subfolder
on adding folder items to theFolder after receiving newItems
-- determine subfolder name
set mondayMonday to the weekday of the (current date) as integer
if mondayMonday is 1 then
copy the (current date) - 6 * days to mondayDate
else if mondayMonday is greater than 2 then
copy the (current date) - (mondayMonday - 2) * days to mondayDate
end if
set subFolderName to fillZeroes(month of mondayDate as integer) & "-" & fillZeroes(day of mondayDate) & "-" & year of mondayDate
set subFolderParent to POSIX file ("/Users/jerry/Documents/Weeklies/")
set subFolderPath to (subFolderParent as string) & subFolderName
tell application "Finder"
--create folder if it doesn't already exist
if not (exists subFolderPath) then
make new folder at subFolderParent with properties {name:subFolderName}
end if
--copy folder alias subFolderPath to subFolder
repeat with desktopItem in newItems
if exists file named (name of desktopItem) in folder subFolderPath then
set fileName to name of desktopItem as text
set fileCounter to 1
set fileNameWithCounter to fileName
repeat while exists file named fileNameWithCounter in folder subFolderPath
--put the counter first, so as not to invalidate any file extension
set fileCounter to fileCounter + 1
set fileNameWithCounter to (fileCounter as text) & " " & fileName
end repeat
--DO NOT MOVE. Renaming will trigger a new folder action
--moving will cancel the folder action, leaving remaining files on the desktop
set name of desktopItem to fileNameWithCounter
else
move desktopItem to folder subFolderPath
end if
end repeat
end tell
end adding folder items to
--turn any month or day number that is less than ten into two digits with a leading zero
on fillZeroes(theInteger)
set integerString to theInteger as string
if the length of integerString is 1 then set integerString to "0" & integerString
return integerString
end fillZeroes
Хитрость заключается в том, когдафайл уже существует в папке назначения с таким именем;по умолчанию перемещение оставляет элемент на рабочем столе, если существует конфликтующее имя.И единственный другой вариант - перейти с заменой, которая сотрет предыдущий файл.
Но переименование файла на рабочем столе вызовет действие новой папки, оставив все последующие файлы на рабочем столе.Хитрость заключается в том, чтобы переименовать файл , не перемещая его , что позволяет вновь инициированному действию папки обрабатывать его и позволить этому сценарию перейти к остальным элементам.
Обратите внимание, что оба Automatorи AppleScript можно использовать для действий с папками.Это означает, что вам не нужно создавать папку еженедельно;вместо этого он может быть сгенерирован точно в срок, если единственной целью этой папки является сохранение элементов из действия папки.
У вас может быть сценарий Automator, который его генерирует, также будет действие папки;или у вас может быть AppleScript, который перемещает элементы, также генерирует его, как делает этот скрипт.
Если вы хотите, чтобы скрипт не работал, если папка не была сгенерирована, вы бы заменили make new folder at subFolderParent with properties {name:subFolderName}
на просто return
для выхода из скрипта, если нет папки с соответствующим именем.