我正在编写一个脚本,允许学生将他们的文件上传到计算机实验室(同一网络)教师计算机上的共享文件夹。我有一个工作脚本,在执行时,会将学生机器上UPLOAD文件夹中的所有文件复制到教师机器上的SUBMISSIONS文件夹中。但是,如果教师计算机上已存在某个文件,则该脚本会挂起。
我需要能够测试教师机器上是否存在单个文件,并且(a)弹出一条消息,说明“此文件已存在,重命名并再次上传”或(b)附加内容用于区分它的文件名...随机数或“副本1”等。
理想情况下,我希望它作为文件夹操作运行。将文件添加到“UPLOAD”文件夹后,它将自动发送给教师。但我不希望文件复制到同名文件......或者脚本挂起。
欢迎任何想法或替代方法。
这是我的代码:
set home_path to path to home folder as string
set work_folder to alias (home_path & "Desktop:" & "Upload")
try
mount volume "afp://[LOGIN INFO].local/Submissions"
set this_folder to result as alias
tell application "Finder"
tell application "Finder"
duplicate every file of work_folder to this_folder
end tell
eject this_folder
end tell
end try
答案 0 :(得分:0)
我认为如果你的try-block有一个错误的阻止通知你任何错误会有所帮助。
try
# try stuff here
j # this will compile but will throw a runtime error and you can see the error
on error error_message number error_number
display alert "Error: " & error_message & return & return & (error_number as text)
end try
答案 1 :(得分:0)
此脚本会将每个文件复制到已装入的卷。如果目标中存在具有相同名称的文件,则会在复制文件名的末尾添加一个数字并尝试该操作。
示例:如果文件夹中已存在 test.doc ,则脚本将尝试使用名称 test_1.doc 复制它,依此类推..
永远不会重命名原始文件,也不会覆盖旧文件。 该脚本已完全注释,以解释它正在做什么。
** Update2 **
复制到目标代码现在位于它自己的处理程序中。 原始文件由查找器标签索引6(绿色)标记,表示已成功复制。
这也会通过使用索引颜色作为检查来阻止同一原始文件被复制两次。如果它被标记为索引标签7,则将被忽略。
如果您想使用脚本将成功复制的文件移到另一个文件夹中,则可以。但是我没有在这个剧本中这样做过。
set home_path to path to home folder as string
set work_folder to alias (home_path & "Desktop:" & "Upload")
set counter to ""
global counter
--try
set this_folder to mount volume "afp://myMac/UserName/"
this_folder as text
tell application "Finder" to set theFiles to every file of work_folder as alias list #GET ALL FILES OF LOCAL FOLDER AS ALIASES
tell application "Finder" to set theRemoteFiles to (every file of ((this_folder & "Submissions:" as string) as alias)) #GET ALL FILES OF REMOTE FOLDER -- ONLY NEEDED FOR COUNT CHECK LATER
repeat with i from 1 to number of items in theFiles #PROCESS EACH LOCAL FILE
set this_item to item i of theFiles #GET A LOCAL FILE
tell application "Finder" to set LabelIndex to label index of this_item
if LabelIndex is not 6 then
tell application "Finder" to set this_name to displayed name of this_item #GET ITS DISPLAYED NAME
tell application "Finder" to set this_extension to name extension of this_item #GET ITS EXTENSION NAME i.E "txt"
set realName to (do shell script "echo " & quoted form of this_name & "| awk -F '" & quoted form of this_extension & "' '{print $1}'") #GET ITS NAME WITHOUT EXTENSION NAME
set counter to 1 # SET A NUMBER TO ADD TO THE FILE NAME IF THE FILE NAME EXIST ALREADY IN THE REMOTE FOLDER
my checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder) # CALL TO HANDLER THAT WILL DO THE CHECKING AND COPYING
end if
end repeat
tell application "Finder" to eject this_folder
# THE CALL TO THE HANDLER INCLUDES VARIABLES THAT ARE NOT GLOBAL OR PROPERTIES BUT NEED TO BE PASSED ON TO IT I.E(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder)
on checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder)
# (1) IF THE NUMBER OF theRemoteFiles IS GREATER THAN 0 THEN FILES EXIST IN THE REMOTE FOLDER AND MAY CONTAIN FILES WITH THE SAME NAMES AS THE LOCAL ONES. PROCESS..
# (2) IF THE NUMBER OF theRemoteFiles IS NOT GREATER THAN 0.THEN FILES DO NOT EXIST IN THE REMOTE FOLDER AND THE LOCAL ONES CAN JUST BE COPIED OVER.
if (count of theRemoteFiles) is greater than 0 then # (1)
try
my copyOver(this_item, this_folder, this_name)
on error errMssg #WE USE not overwritten ERROR TO TRIGGER THE RENAME THE DESTINATION FILE NAME TO INCLUDE A NEW NUMBER.
--tell application "Finder" to set label index of this_item to 6
if errMssg contains "not overwritten" then
set this_name to (realName & counter & "." & this_extension)
set counter to counter + 1 #WE SETUP THE FILE NAME NUMBER FOR THE POSSIBLE NEXT RUN
# RUN THE HANDLER AGAIN WITH THE CHANED DETAILS
my checkName(this_name, realName, this_item, this_extension, theRemoteFiles, this_folder)
end if
end try
else # (2)
my copyOver(this_item, this_folder, this_name)
end if
end checkName
on copyOver(this_item, this_folder, this_name)
# THE -n OPTION IN THE SHELL COMMAND TELLS CP NOT TO OVERWRITE EXISTING FILES. AN ERROR OCCURE IF THE FILE EXISTS.
# THE -p OPTION IN THE SHELL COMMAND TELLS CP TO PRESERVE THE FOLLOWING ATTRIBUTES OF EACH SOURCE FILE IN THE COPY:
# modification time, access time, file flags, file mode,
#user ID, and group ID, as allowed by permissions. Access Control
#Lists (ACLs) and Extended Attributes (EAs), including resource
#forks, will also be preserved.
# THE -v OPTION IN THE SHELL COMMAND TELLS CP TO USE VERBOS MODE. WHICH GIVES US A BETTER CLUE OF THE ERROR
set theResult to (do shell script "cp -npv " & quoted form of (POSIX path of this_item) & space & quoted form of (POSIX path of (this_folder & "Submissions:" as string) & this_name))
if theResult contains "->" then
tell application "Finder" to set label index of this_item to 6
else
tell application "Finder" to set label index of this_item to 7
end if
end copyOver
答案 2 :(得分:0)
行。我再次尝试编写一些代码。这是我的版本。
它与原始海报发布的代码有所不同。
它将文件和文件夹复制到服务器上的新文件夹中,并带有时间戳,以解决服务器上是否已存在某些文件的问题。
我更改了重复语句的措辞,重复复制每个“文件”以复制每个“项目”,以便文件夹也重复。
我在try-statement中输入了一个on-error块来显示任何错误。
我激活Finder,以便您可以看到进度窗口。
如果没有错误,我会在最后弹出一个对话框。
我遇到了一个我必须解决的问题:
我认为以下代码应该可以正常工作。这几乎是100%的AppleScript。它只使用一次对shell的调用,即获取当前日期并将其格式化为服务器上创建的新文件夹的时间戳。
# set the path to the "work_folder" where the files are to be uploaded
set home_path to path to home folder as string
set work_folder to alias (home_path & "Desktop:" & "Upload")
# duplicate the files and folders in work_folder to the server
try
# TODO set the name of your server here
set the_volume to mount volume "afp://32-bit.local/Submissions"
set destination_path to the_volume as text
set folder_name to getTimeStamp()
tell application "Finder"
activate
set new_folder to make new folder at alias destination_path with properties {name:folder_name}
duplicate every item of work_folder to new_folder
eject the_volume
display alert "Successfully uploaded the files and folders"
end tell
on error error_message number error_number
if error_number is not equal to -128 then
display alert "Error: " & error_message & return & return & (error_number as text)
end if
end try
# This function returns the current date and time as a time-stamp of the form yyyy-mm-dd-hh-ss
# This function uses a shell script because it's just a lot easier to do than in AppleScript
on getTimeStamp()
set time_stamp to do shell script "date '+%Y-%m-%d-%H-%M-%S'"
return time_stamp
end getTimeStamp
答案 3 :(得分:0)
这是调试的另一个想法。您可以调用“显示对话框”以便能够知道脚本失败的位置:
display dialog "Entering script"
set home_path to path to home folder as string
display dialog "home_path: " & home_path
set work_folder to alias (home_path & "Desktop:" & "Upload")
display dialog "work_folder: " & work_folder as string
try
mount volume "afp://32-bit.local/Submissions"
set this_folder to result as alias
display dialog "this_folder: " & this_folder as string
tell application "Finder"
display dialog "Inside of the first tell application Finder"
tell application "Finder"
display dialog "About to call duplicate"
duplicate every file of work_folder to this_folder
display dialog "Just returned from calling duplicate"
end tell
display dialog "About to call eject"
eject this_folder
display dialog "Just returned from call to eject"
end tell
on error error_message number error_number
display alert "Error:" & error_message & return & return & (error_number as text)
end try
display dialog "Exiting script"
另一种调试技术是将输出记录到文本文件中。
另一种调试技术是购买AppleScript调试器:
http://www.latenightsw.com/sd4/index.html
我相信这个调试器售价200.00美元对我来说太贵了,但是我用过其他调试器和调试器都是很好的工具,可以让你在运行时“窥视”脚本,看看变量的值和跟踪正在执行哪些代码行。