AppleScript复制后正在使用的文件

AppleScript复制后正在使用的文件,applescript,Applescript,我有一个每两小时运行一次的脚本。它将Helix数据库集合及其日志文件复制到本地备份文件夹。我先保存,使它们同步,然后复制它们。它已开始间歇性失败,表示日志文件正在使用中。它在取景器中变暗了 以下是脚本: tell application "Finder" set dupArray to {} --move the collection into the array set CollectionName to name of collectionFile copy

我有一个每两小时运行一次的脚本。它将Helix数据库集合及其日志文件复制到本地备份文件夹。我先保存,使它们同步,然后复制它们。它已开始间歇性失败,表示日志文件正在使用中。它在取景器中变暗了

以下是脚本:

tell application "Finder"
    set dupArray to {}
    --move the collection into the array
    set CollectionName to name of collectionFile
    copy collectionFile to the end of dupArray

    --move the logfile into the array
    set theCollectionLogfile to ((mainLoc as text) & "Log_" & CollectionName & ".hlog")
    if (exists file theCollectionLogfile) then
        copy theCollectionLogfile to the end of dupArray
    end if

    --do the duplicate
    duplicate dupArray to backupFolder with replacing
end tell
我怎样才能在第二次替换时复制它

谢谢

莱尼

注意: 我建议您保存您的工作并积极测试,以便获得这些概念。 请仔细阅读

[编辑:我意识到,即使你可能理解我的意思,我在评论中写副本的时候写了副本]

您可以尝试使用以下内容:

--right before duplicate
set UNIQUE_fileName to "Log_" & CollectionName & ".hlog"
repeat until fileNotInUse
    try
        do shell script "lsof | grep " & UNIQUE_fileName
    on error
        set fileNotInUse to true
        exit repeat
    end try
end repeat
--do duplicate
这应该等到文件写入完毕。您会注意到我将UNIQUE大写,因为如果系统正在使用与该名称匹配的任何其他文件,或者其他任何文件,它将使您处于无限循环中。避免这种情况的一种方法是传递POSIX样式的文件完整路径,而不仅仅是文件名。您还可以通过设置重复的上限来格外小心。以下是一个使用这两种思想的版本:

--right before duplicate

set upperLimit to 200000
set u to 0

set theCollectionLogfilePOSIX to quoted form of (POSIX path of theCollectionLogfile)
repeat until fileNotInUse
    try
        do shell script "lsof | grep " & theCollectionLogfilePOSIX
    on error
        set fileNotInUse to true
        exit repeat
    end try
set u to (u + 1)
if (u > upperLimit) then exit repeat
end repeat
--do duplicate
再次:

我建议您保存您的工作并积极测试,以便获得概念


[编辑:它在一个try块中,因为如果没有任何“打开”,即没有从lsof结果中“grepped”任何内容,那么do shell脚本将出错]

好吧,我最后咬紧牙关,只是在“do shell script”中编写了它。结果发现发现者不想扔掉文件,它仍然被什么东西钩住了。我使用同上进行复制,因为文件有资源分叉,我使用rm删除文件

我想查找者搞错了,锁定了一些东西,因为我复制的文件在我复制时有打开的插槽。我还认为“获取Ifo”对话框应该有一个区域,在该区域中,如果显示正在使用,您可以看到哪个应用正在使用

谢谢你的帮助


莱尼

谢谢你的回复。我昨天出去了。。。不幸的是,一天后,该文件仍在使用中。它不是为了任何事情而发布的,我想你会明白,我的意思是复制我写的副本;本计划在最后一个复制零件之前完成。