Applescript 将文件复制到新文件夹

Posted

技术标签:

【中文标题】Applescript 将文件复制到新文件夹【英文标题】:Applescript Copy file to a new folder 【发布时间】:2010-10-28 11:45:52 【问题描述】:

我需要创建一个 AppleSript,它将指定文件从一个文件夹复制到新创建的文件夹。

这些文件需要在 AppleScript 编辑器中指定,例如:

start

fileToBeMoved = "Desktop/Test Folder 1/test.doc"
newfoldername = "Test Folder 2"

make newfolder and name it 'newfoldername'
copy 'fileToBeMoved' to 'newfolder'

end

【问题讨论】:

【参考方案1】:

AppleScript 的诀窍在于移动文件是使用别名完成的。

更现实地说,如果您使用 Automator 或类似的东西,制作一个 shell 脚本可能更容易,而不是可以使用 do shell script 从 AppleScript 运行。

#!/bin/sh

fileToBeMoved="$HOME/Desktop/Test Folder 1/test.doc"
newFolderName="Test Folder 2"
mkdir "$newFolderName"
cp -a "$fileToBeMoved" "$newFolderName"

【讨论】:

最好使用这种方法而不是applescript 方法,因为有时您可能会遇到权限问题,然后使用这种方法会有所帮助。 这样做的缺点是:如果复制时间较长,用户将看不到进度条。出于同样的原因,用户也不能取消。使用 shell 命令支持这些操作将比简单地让 Finder 来支持这些操作要困难得多。但是,Applescripting Finder 的缺点:如果用户选择使用替换的“Finder”应用程序,Applescript 操作可能会失败(对此不确定,可能取决于替换的编写情况)。 使用Finder的另一个好处:如果目标项目存在,将要求用户替换或不替换。【参考方案2】:

一般:

tell application "Finder"
  make new folder at alias "Macintosh HD:Users:user:Desktop:" with properties name:"Test Folder 2"
  copy file "Macintosh HD:Users:user:Desktop:Test Folder 1:test.doc" to folder "Macintosh HD:Users:user:Desktop:Test Folder 2"
end tell

您可以添加代表 POSIX 文件和路径的变量名称。

显然,冒号 (:) 是文件夹和文件名的保留字符。

set desktopFolder to "Macintosh HD/Users/user/Desktop/"
set desktopFdrPosix to quoted form of POSIX path of desktopFolder
set newFolderName to "Test Folder 2"
set destinationFdrPosix to quoted form of desktopFdrPosix & POSIX file newFolderName
set sourceFilename to "Test Folder 1/test.doc"
set sourceFnPosix to quoted form of desktopFdrPosix & POSIX file sourceFilename

tell application "Finder"
  make new folder at alias desktopFdrPosix with properties name:newFolderName
  copy file sourceFnPosix to folder destinationFdrPosix
end tell    

如果目标文件夹已经存在,您可能还想添加错误检查。

【讨论】:

我需要更改哪些内容才能复制到已挂载的 SMB 文件共享上的文件夹?我在/Volumes/Video 的共享上安装了文件夹“电影”。我尝试为VideoVideo/Movies/Volumes/Video/Volumes/Video/Movies 创建一个quoted form of POSIX path,同时为copy fileduplicate file 创建一个to folderto disk。他们都没有工作。全部报错:execution error: Finder got an error: Can't set folder(或disk"'/Volumes/Video/Movies'" to file "'/Users/me/path/to/file.m4v'". (-10006)【参考方案3】:
set home_path to path to home folder as string
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:")
tell application "Finder"
    duplicate every file of work_folder to folder "Archive" of home
end tell

【讨论】:

【参考方案4】:

这适用于复制到已安装的网络卷:

    mount volume "afp://compname.local/mountpoint"
    tell application "Finder"
      duplicate file "MyTextFile.txt" of folder "Documents" of home to disk "mountpoint"
      eject "mountpoint"
    end tell

【讨论】:

【参考方案5】:

如果您正在寻找同步,您可以在 AppleScript 中运行以下 shell 脚本:

rsync -a /Users/username/folderToBeCopiedFrom /Volumes/folderToBeCopiedTo/

【讨论】:

【参考方案6】:
tell application "Finder"
    make new folder at desktop with properties name:"folder"
    duplicate POSIX file "/usr/share/doc/bash/bash.html" to result
end tell

POSIX file ((system attribute "HOME") & "/Documents" as text)
tell application "Finder" to result

tell application "Finder" to duplicate (get selection) to desktop replacing yes

-- these are documented in the dictionary of System Events
path to home folder
POSIX path of (path to documents folder)
path to library folder from user domain
path to desktop folder as text

-- getting files as alias list is faster
tell application "Finder"
    files of entire contents of (path to preferences folder) as alias list
end tell

【讨论】:

【参考方案7】:

我使用 Chealions shell 脚本解决方案。不要忘记使您的脚本文件可执行: sudo chmod u+x scriptFilename

同时删除变量赋值中= 符号之间的空格。不适用于空格,例如:newFolderName="Test Folder 2"

【讨论】:

【参考方案8】:

简单的方法如下所述

set home_path to path to home folder as string
set work_folder to alias (home_path & "AutomationAppleScript:ScreenShots:")
tell application "Finder"
    duplicate every file of work_folder to folder "Archive" of home
end tell

【讨论】:

以上是关于Applescript 将文件复制到新文件夹的主要内容,如果未能解决你的问题,请参考以下文章

将文件复制到新位置

将用户选择的多个文件(通过文件对话框)复制到新创建的文件夹

Flutter - 无法将文件复制到新路径

将自己文件夹中的松散文件批量复制到新的重命名文件夹

将主动录制的 Wav 文件的内容复制并更新到新文件

如何 git 将文件夹和文件移动到新文件夹并保留历史记录? [复制]