我需要创建一个 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
我需要创建一个 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
一般来说:
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
如果目标文件夹已存在,您可能还需要添加错误检查。
AppleScript 的诀窍在于移动文件是使用别名完成的。
do shell script
更现实的是,如果您使用 Automator 或类似的东西,制作一个可以从 AppleScript 运行的 shell 脚本可能会更容易。
#!/bin/sh
fileToBeMoved="$HOME/Desktop/Test Folder 1/test.doc"
newFolderName="Test Folder 2"
mkdir "$newFolderName"
cp -a "$fileToBeMoved" "$newFolderName"
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
这适用于复制到已安装的网络卷:
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
如果您正在寻找同步,您可以在 AppleScript 中运行以下 shell 脚本:
rsync -a /Users/username/folderToBeCopiedFrom /Volumes/folderToBeCopiedTo/
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
我使用了 Chealions shell 脚本解决方案。不要忘记使您的脚本文件可执行:
sudo chmod u+x scriptFilename
还要删除=
变量赋值中符号之间的空格。不适用于空格,例如:newFolderName="Test Folder 2"
简单的方法如下所述
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