【问题标题】:Move items to specific folder将项目移动到特定文件夹
【发布时间】:2019-03-04 09:11:55
【问题描述】:

我想要一个文件夹操作,将添加到桌面文件夹的任何项目移动到特定的子文件夹。子文件夹每周一由自动化器每周生成一次。本周,该子文件夹称为“Desktop Week 02-18-2019”

我想使用文件夹操作,因此当任何文件添加到桌面时,它都会移动到该周的子文件夹。

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

大多数在线解决方案仅处理要移入名称的文件夹已知的位置。

提前感谢您的帮助

【问题讨论】:

    标签: applescript


    【解决方案1】:

    这是一个示例脚本,它将移动添加到任何附加此脚本的任何文件夹中的任何新项目。当然,您必须将 /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 脚本也成为文件夹操作;或者你可以让移动项目的 AppleScript 也生成它,就像这个脚本一样。

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

    【讨论】:

    • repeat with desktopItem in newItems 应该不是必需的,因为 Findermove 命令可以处理文件对象列表。因此,您只需move newItems to...
    • @CJK,你是对的。只要可以依赖文件名是唯一的,move newItems to folder subFolderPath 就可以工作。然而,在测试该解决方案时,我发现move 如果已经存在同名文件,则会失败,并且重命名和移动很棘手,因为这会触发文件夹操作的新实例。诀窍似乎是重命名而不是移动,所以我调整了脚本。
    【解决方案2】:

    这适用于我使用最新版本的 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
    

    【讨论】:

    • 感谢您的帮助。我将此 scpt 放在文件夹操作中,然后将项目保存到桌面,但似乎没有发生任何事情。我应该以某种方式在自动机中手动附加此脚本吗?或者它是否意味着进入文件夹操作并从偏移量工作?
    • 我已更新脚本并保存并附加到每个屏幕视频的桌面。然而,当项目被放入桌面时仍然没有任何变化:/我是对的,我根本不需要更改脚本中的任何行吗?
    • 一些可能有用也可能没用的注释: ① 你最后的if...then...else 条件似乎是多余的,因为ifelse 块执行相同的代码; ② moveToFolderCreationDatefolderCreationDate 被不必要地声明为属性,但作为变量就足够了(并且更合适); ③如果set theFolder to folders of alias...在返回结果中包含多于一项,set theFolder to theFolder as alias会抛出错误; ④ 可能值得解释在被监视文件夹中创建文件夹的文件夹操作的潜在影响。
    • 感谢@CJK 尚未添加这些编辑,因为等待 wch1zpink 只是为了看看我是否遗漏了什么
    • 这里没有永无止境的循环,因为当脚本创建一个文件夹时,它会再次执行脚本以将该文件夹移动到自身中;这将产生一个错误,并终止脚本的第二次运行,因此据用户所知,似乎没有发生任何不愉快的事情。严格来说,这并没有什么特别的问题,但它不一定是执行的理想方式,了解幕后发生的事情总是好的,即使结果看起来是良性的。
    猜你喜欢
    • 1970-01-01
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 2017-04-29
    • 2016-12-06
    • 1970-01-01
    相关资源
    最近更新 更多