Applescript复制文件到新文件夹

11
我需要创建一个AppleScript,它将指定的文件从一个文件夹复制到一个新创建的文件夹中。
这些文件需要在AppleScript编辑器中指定,类似于:
start

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

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

end
8个回答

13

一般来说:

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    

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


2
我需要更改什么才能将文件复制到已挂载的SMB文件共享上的文件夹中?我在挂载点/Volumes/Video下有一个名为“Movies”的文件夹。我尝试为VideoVideo/Movies/Volumes/Video/Volumes/Video/Movies创建了POSIX路径的引用形式,对于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) - nekno

5

使用AppleScript的技巧在于,移动文件是使用别名来完成的。

更实际的做法可能是使用shell脚本代替,如果您正在使用Automator或类似工具,则可以从AppleScript中使用do shell script运行该脚本。

#!/bin/sh

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

使用这种方法比使用AppleScript更好,因为有时您可能会遇到权限问题,而使用这种方式可以帮助解决。 - aayush sharma
这样做的缺点是:如果复制时间很长,用户将看不到进度条。由于同样的原因,用户也无法取消。用Shell命令支持这些操作比让Finder自己完成要困难得多。但使用Applescript脚本操作Finder也有其缺点:如果用户选择使用替代“Finder”应用程序,则Applescript操作可能会失败(不确定,可能取决于替代程序编写的好坏)。 - Thomas Tempelmann
1
使用Finder的另一个优点:如果目标项目存在,用户将被询问是否要替换。 - Thomas Tempelmann

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

1

这适用于将内容复制到已挂载的网络卷:

    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

0
如果您正在寻找同步功能,可以在您的AppleScript中运行以下shell脚本:
rsync -a /Users/username/folderToBeCopiedFrom /Volumes/folderToBeCopiedTo/

0

做这件事的简单方法如下所述

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

0

我使用了Chealion的shell脚本解决方案。别忘了使用以下命令将你的脚本文件设置为可执行文件:

sudo chmod u+x scriptFilename

同时,要删除变量赋值中等号=符号两侧的空格。例如,以下代码是无法正常工作的:newFolderName="Test Folder 2"


0
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接