脚本将文件移动到 Applescript 中新创建的文件夹时出现问题
Trouble with script moving files to a newly created folder in Applescript
我在完成我认为非常简单的任务时遇到了问题,但似乎无法让我的脚本正常工作。我通过论坛找到了很多关于使用的个别例程的帮助,但似乎仍然失败。
简而言之,我想做的是监视文件夹中是否有新文件被添加。一旦添加了一批文件(每隔一天左右),它将在另一个目录中创建一个文件夹,新文件夹名称为当前日期,将这些文件移动到新目录,然后执行一个简单的 bash 使用新目录名称作为参数的脚本。
我的脚本编译正常,但是添加文件后它只会创建新文件夹,而不会创建其他任何东西。我感谢任何帮助。
property the_sep : "-"
on adding folder items to my_folder after receiving the_files
tell application "Finder"
(* First create a new folder with name of folder = current date *)
set the_path to (folder "qa" of folder "Documents" of folder "ehmlab" of folder "Users" of disk "Macintosh HD")
set the_name to (item 1 of my myDate())
set the_name to (the_name & the_sep & item 2 of my myDate())
set the_name to (the_name & the_sep & item 3 of my myDate())
make folder at the_path with properties {name:the_name}
set newDir to the_path & the_name
end tell
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with i from 1 to number of items in the_files
tell application "Finder"
try
set this_file to (item i of the_files)
move file this_file to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & newDir
end adding folder items to
on myDate()
set myYear to "" & year of (current date)
set myMth to text -2 thru -1 of ("0" & (month of (current date)) * 1)
set myDay to text -2 thru -1 of ("0" & day of (current date))
return {myYear, myMth, myDay}
end myDate
失败的原因是不同的路径样式。
AppleScript 使用 HFS 路径(以冒号分隔)。
UNIX 使用 POSIX 路径(斜杠分隔)。
解决方案是将 HFS 路径字符串强制为 POSIX 路径
do shell script "qc.sh " & quoted form of POSIX path of newDir
这是一个较短的脚本版本,使用 shell 也用于时间戳和创建目录。
on adding folder items to my_folder after receiving the_files
(* Next, move the newly added files to the source into the newly created date folder,
"path to documents" is a relative path to the documents folder of the current user *)
set baseFolder to (path to documents folder as text) & "qa:"
set timeStamp to do shell script "date +%Y-%m-%d"
set newDir to baseFolder & timeStamp
do shell script "mkdir -p " & quoted form of POSIX path of newDir
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with aFile in the_files
tell application "Finder"
try
move aFile to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & quoted form of POSIX path of newDir
end adding folder items to
我在完成我认为非常简单的任务时遇到了问题,但似乎无法让我的脚本正常工作。我通过论坛找到了很多关于使用的个别例程的帮助,但似乎仍然失败。
简而言之,我想做的是监视文件夹中是否有新文件被添加。一旦添加了一批文件(每隔一天左右),它将在另一个目录中创建一个文件夹,新文件夹名称为当前日期,将这些文件移动到新目录,然后执行一个简单的 bash 使用新目录名称作为参数的脚本。
我的脚本编译正常,但是添加文件后它只会创建新文件夹,而不会创建其他任何东西。我感谢任何帮助。
property the_sep : "-"
on adding folder items to my_folder after receiving the_files
tell application "Finder"
(* First create a new folder with name of folder = current date *)
set the_path to (folder "qa" of folder "Documents" of folder "ehmlab" of folder "Users" of disk "Macintosh HD")
set the_name to (item 1 of my myDate())
set the_name to (the_name & the_sep & item 2 of my myDate())
set the_name to (the_name & the_sep & item 3 of my myDate())
make folder at the_path with properties {name:the_name}
set newDir to the_path & the_name
end tell
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with i from 1 to number of items in the_files
tell application "Finder"
try
set this_file to (item i of the_files)
move file this_file to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & newDir
end adding folder items to
on myDate()
set myYear to "" & year of (current date)
set myMth to text -2 thru -1 of ("0" & (month of (current date)) * 1)
set myDay to text -2 thru -1 of ("0" & day of (current date))
return {myYear, myMth, myDay}
end myDate
失败的原因是不同的路径样式。
AppleScript 使用 HFS 路径(以冒号分隔)。
UNIX 使用 POSIX 路径(斜杠分隔)。
解决方案是将 HFS 路径字符串强制为 POSIX 路径
do shell script "qc.sh " & quoted form of POSIX path of newDir
这是一个较短的脚本版本,使用 shell 也用于时间戳和创建目录。
on adding folder items to my_folder after receiving the_files
(* Next, move the newly added files to the source into the newly created date folder,
"path to documents" is a relative path to the documents folder of the current user *)
set baseFolder to (path to documents folder as text) & "qa:"
set timeStamp to do shell script "date +%Y-%m-%d"
set newDir to baseFolder & timeStamp
do shell script "mkdir -p " & quoted form of POSIX path of newDir
(* Next, move the newly added files to the source into the newly created date folder *)
repeat with aFile in the_files
tell application "Finder"
try
move aFile to folder newDir
end try
end tell
end repeat
do shell script "qc.sh " & quoted form of POSIX path of newDir
end adding folder items to