将项目移到特定文件夹

时间:2019-02-20 07:51:19

标签: applescript

我想要一个文件夹操作,将添加到“桌面”文件夹的所有项目移动到特定的子文件夹。子文件夹每周一次由自动机生成。在本周中,该子文件夹称为“桌面周02-18-2019”

我想使用文件夹操作,以便将任何文件添加到桌面后将其移动到该周的子文件夹。

据我了解,我需要找到该子文件夹并将其设置为变量,并将触发该文件夹操作的原始文件移至该特定子文件夹中。

大多数联机解决方案仅处理已知要移入名称的文件夹。

预先感谢您的帮助

2 个答案:

答案 0 :(得分:0)

这对于使用最新版本的macOS Mojave的我来说有效

将下面的AppleScript代码保存为.scpt文件到文件夹…。 /用户/您的简称/库/工作流/应用程序/文件夹操作。

property moveToFolder : (path to documents folder as text)
property folderNameContains : "Desktop Week"

on adding folder items to theFolder after receiving theNewItems
    if weekday of (current date) is Tuesday then
        set theWeekday to 1
    else if weekday of (current date) is Wednesday then
        set theWeekday to 2
    else if weekday of (current date) is Thursday then
        set theWeekday to 3
    else if weekday of (current date) is Friday then
        set theWeekday to 4
    else if weekday of (current date) is Saturday then
        set theWeekday to 5
    else if weekday of (current date) is Sunday then
        set theWeekday to 6
    else if weekday of (current date) is Monday then
        set theWeekday to 0
    end if

    set moveToFolderCreationDate to short date string of ((current date) - (theWeekday * days))

    tell application "Finder"
        set dateStringForMakeFolder to my shortDateTID(moveToFolderCreationDate, {"/"})
        set a to length of (item 1 of words of dateStringForMakeFolder)
        if a is 1 then set dateStringForMakeFolder to (0 as text) & dateStringForMakeFolder
        try
            make new folder at moveToFolder ¬
                with properties {name:((folderNameContains & " " & dateStringForMakeFolder) as string)}
        end try
        set theFolder to folders of alias moveToFolder whose name contains dateStringForMakeFolder
        move theNewItems to (theFolder as alias) with replacing
    end tell
end adding folder items to

to shortDateTID(someText, delimiterListItems)
    set originalText to someText
    set AppleScript's text item delimiters to delimiterListItems
    set tempText to text items of originalText
    set text item delimiters to "-"
    set cleanedText to tempText as text
end shortDateTID

enter image description here

答案 1 :(得分:0)

这是一个示例脚本,它将把添加到此脚本的任何文件夹中的所有新项目移动。当然,您将必须用每周存储的任何文件夹替换/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

棘手的是,目标文件夹中已经存在一个文件。默认情况下,如果名称冲突,move会将该项保留在桌面上。唯一的选择是移动替换,这将删除先前的文件。

但是在桌面上重命名文件将触发新的文件夹操作,从而在桌面上保留所有后续文件。技巧似乎是在不移动文件的情况下重命名文件 ,允许新触发的文件夹操作对其进行处理,然后将该脚本移至其余项目。

请注意,Automator和AppleScript均可用于文件夹操作。这意味着您实际上不需要每周生成一次文件夹;如果该文件夹的唯一目的是存储“文件夹操作”中的项目,则可以即时生成该文件。

您可以拥有生成它的Automator脚本,也可以是Folder Action;或者您也可以像该脚本那样使用移动项目的AppleScript来生成它。

如果您希望脚本在未生成文件夹的情况下失败,请使用简单的make new folder at subFolderParent with properties {name:subFolderName}替换return,以在没有适当名称的文件夹时退出脚本。