Applescript根据部分文件名复制文件

Applescript根据部分文件名复制文件,applescript,Applescript,原谅我,我有点爱你 我正在努力满足数百名学生的记录要求。所有文件都以名称的前5位数字命名,即学生的ID号。我创建了下面的脚本,它运行了,但没有结果 我欢迎你们提供任何帮助 with timeout of 3600 seconds tell application "Finder" set myFiles to files of folder POSIX file "/Volumes/Storage/Records" as alias list end tell repeat with a

原谅我,我有点爱你

我正在努力满足数百名学生的记录要求。所有文件都以名称的前5位数字命名,即学生的ID号。我创建了下面的脚本,它运行了,但没有结果

我欢迎你们提供任何帮助

with timeout of 3600 seconds
tell application "Finder"
    set myFiles to files of folder POSIX file "/Volumes/Storage/Records" as alias list
end tell
repeat with aFile in myFiles
    tell application "System Events"
        set myvalues to {"11111", "22222", "33333", "44444", "55555", "66666", "77777", "88888", "99999", "00000", "11112", "22223", "33334", "44445", "55556", "66667", "77778", "88889", "99990"}

        if name of aFile contains myvalues then
            copy aFile to folder POSIX file "/Volumes/Storage/Records"
        end if
    end tell
  end repeat 
end timeout

首先,用Finder和系统事件的术语复制文件的命令是
duplicate

第二,你必须检查前5个字符,而不是整个名字

第三,将文件复制到同一个位置不是很有用,但我想这只是一个占位符

property IDNumbers : {"11111", "22222", "33333", "44444", "55555", "66666", "77777", "88888", "99999", "00000", "11112", "22223", "33334", "44445", "55556", "66667", "77778", "88889", "99990"}

with timeout of 3600 seconds
  tell application "Finder"
    set myFiles to files of folder "Storage:Records:"
    repeat with aFile in myFiles

      set IDPrefix to text 1 thru 5 of (get name of aFile)
      if IDPrefix is in IDNumbers then
        duplicate aFile to folder "Storage:Records:Destination:"
      end if
    end repeat
  end tell
end timeout
我的解决方案只使用Finder,因为它不包含不可见的文件,并且不需要强制Finder对象说明符为alias


如果文件的位置在外部卷上,则使用HFS路径
“存储:记录”
比从POSIX路径
POSIX文件强制更容易“/Volumes/Storage/Records”作为别名

我建议您使用do shell script函数,而不是使用Finder应用程序,因为这是一种更灵活的解决方案。您可以很容易地在命令中找到任何错误,因为您可以使用终端对其进行调试

do shell script "<your commands here>"
do shell脚本“”

对于您的用例,您需要在do shell脚本中使用两个命令:find,然后cp,,我建议使用一个简单的
bash
脚本。在桌面上将以下脚本另存为
CopyFiles

#!/bin/bash
# Make a subdirectory to copy the results to
mkdir results 2>/dev/null

# Read all ids from file "ids.txt"
while read id; do
   echo Processing id: $id
   # Remove the word "echo" on following line to actually copy files
   echo cp /Volumes/Storage/Records/${id}* results 
done < ids.txt
然后按Cmd+空格键并键入“Terminal”并点击“Enter”,启动一个终端

转到桌面,使脚本可执行

cd Desktop
chmod +x CopyFiles
然后运行它

./CopyFiles
目前,它什么也不做,只是告诉你它会这样做:

Processing id: 11111
cp /Volumes/Storage/Records/11111* results
Processing id: 22222
cp /Volumes/Storage/Records/22222* results
Processing id: 12345
cp /Volumes/Storage/Records/12345* results
Processing id: 54321
cp /Volumes/Storage/Records/54321* results
如果它看起来像是在做你想做的事情,编辑脚本并删除注释处的单词
echo
,然后再次运行它

Processing id: 11111
cp /Volumes/Storage/Records/11111* results
Processing id: 22222
cp /Volumes/Storage/Records/22222* results
Processing id: 12345
cp /Volumes/Storage/Records/12345* results
Processing id: 54321
cp /Volumes/Storage/Records/54321* results