Applescript将文件复制到新文件夹

时间:2009-05-23 19:58:00

标签: file copy applescript directory

我需要创建一个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

8 个答案:

答案 0 :(得分:10)

一般而言:

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    

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

答案 1 :(得分:5)

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

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

#!/bin/sh

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

答案 2 :(得分: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

答案 3 :(得分: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

答案 4 :(得分:0)

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

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

答案 5 :(得分: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

答案 6 :(得分:0)

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

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

答案 7 :(得分: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
相关问题