“如何结合”;选择文件夹命令";加上;添加文件夹项目";带Applescript的函数

“如何结合”;选择文件夹命令";加上;添加文件夹项目";带Applescript的函数,applescript,directory,Applescript,Directory,我正在使用文件夹操作创建一个“热”文件夹,它会立即将放置在此文件夹中的文件复制到特定的备份文件夹 这是我用作文件夹操作的脚本,它工作正常: on adding folder items to this_folder after receiving these_items tell application "Finder" to duplicate these_items to folder ("Macintosh HD:Users:photograper:Desktop:Targe

我正在使用文件夹操作创建一个“热”文件夹,它会立即将放置在此文件夹中的文件复制到特定的备份文件夹

这是我用作文件夹操作的脚本,它工作正常:

on adding folder items to this_folder after receiving these_items
   tell application "Finder" to duplicate these_items to folder 
   ("Macintosh HD:Users:photograper:Desktop:Target:")
end adding folder items to
但是我想像普通的applescript一样运行这个文件夹操作,并用“选择文件夹”命令触发“添加文件夹项”功能。因此,我可以手动指向特定的源文件夹和目标文件夹,并让脚本运行

我试过这个,但没有结果:

set this_folder to choose folder with prompt "Select source folder:" 
with multiple selections allowed
set target_folder to choose folder with prompt "Select backup folder:"


on adding folder items to this_folder after receiving these_items
   tell application "Finder" to duplicate these_items to target_folder
end adding folder items to

如何将脚本的两个部分组合在一起以便进行交互,有什么建议吗?

只需删除处理程序并将变量“this\u items”更改为“this\u folder”,如下所示:

set this_folder to choose folder with prompt "Select source folder:" with multiple selections allowed
set target_folder to choose folder with prompt "Select backup folder:"

tell application "Finder" to duplicate this_folder to target_folder
然后将脚本作为普通脚本运行,它会将文件夹完全复制到您的目标

另一方面,如果您只想复制项目,则可以执行以下操作:

set this_folder to choose folder with prompt "Select source folder:" with multiple selections allowed
set target_folder to choose folder with prompt "Select backup folder:"

tell application "Finder"
    set these_items to list folder of this_folder without invisibles
    repeat with i from 1 to count of these_items
        set ItemX to item i of these_items
        set ItemX to (this_folder & ItemX as string) as alias
        duplicate ItemX to target_folder
    end repeat
end tell

无论哪种方式,最终都会得到一个副本。

在下面的示例中,目标文件夹被设置为桌面上的文件夹“Target”。你可以根据需要调整它

警告:此示例不管理目标文件夹不存在时的情况

on adding folder items to this_folder after receiving these_items

    set DestFolder to ((path to desktop folder) as string) & "Target:"
    tell application "Finder" to duplicate these_items to folder DestFolder

end adding folder items to

每次将文件放在指向该脚本的文件夹链接中时,文件都会复制到目标。

如果使用文件夹操作,将文件放在文件夹中可能会更好地运行“添加文件夹时…”和“结束添加文件夹时…”之间的代码部分。因此,如果要选择目标文件夹,它必须介于这两条语句之间。请注意,添加的文件已包含在这些项目中,因此无需询问。感谢您的回复@pbell。我不想使用文件夹操作。当一个文件被放到文件夹中时,我不希望它立即复制到目标文件夹,这就是我不希望的。我尝试使用“选择文件夹”命令来设置dest。文件夹中的2条语句,但每次删除文件时,它都会触发“选择窗口”。有没有办法一次性设置目标文件夹?在源文件夹中放置的每个文件都使用哪一个?@pbell,这是我尝试的:在收到这些\u项目后将文件夹项目添加到此\u文件夹时,设置目标\u文件夹以选择文件夹,并提示“选择备份文件夹:”告诉应用程序“查找器”将这些\u项目复制到目标\u文件夹结束将文件夹项目添加到目标文件夹