I have a script that runs from a launch agent, so when a file arrives in the folder. Now as the files are arriving at this folder from another computer I don’t want it to move the files to soon as they might still be in the process of downloading.
So I have this script that works by checking the file size. But incase anything arrives in the time it begins to move the files I would like it to unload the launchAgent. continue the applescript then load it again.
This is where the script then fails to run and therefore doesn’t get reloaded. Is there a work around?
set waitTime to 30 -- a maximum time to wait for the transfer to complete
set this_folder to ((path to desktop folder) as text) & "Hotfolder" -- the watched folder
set destination to ((path to pictures folder) as text) & "HotDestination"
do shell script "launchctl unload -S Aqua /Users/Matthew/Library/LaunchAgents/theplist.plist"
if (waitForFilesToCopy into this_folder for waitTime) then -- transfer appears to be complete
tell application "Finder" to move (files of folder this_folder whose name does not start with "MJC_" and name does not start with "SH_" and name does not start with "ARK_" and name does not start with "5" and name does not start with "0" and name does not start with "Focus" and name does not start with "Untitled" and name does not start with "PR__5" and name does not start with "SE__5" and name does not start with "NN__5") to folder destination
else -- timed out
log "copy not completed" -- or whatever
end if
do shell script "launchctl load -S Aqua /Users/Matthew/Library/LaunchAgents/theplist.plist"
to waitForFilesToCopy into theFolder for timeToWait -- waits up to the timeToWait for files to be copied/downloaded to theFolder
set {theFolder, possible, interval} to {theFolder as text, false, 2} -- change the check interval as desired
tell application "System Events" to set currentSize to size of disk item theFolder -- get initial size
repeat (timeToWait div interval) times -- check every interval seconds
delay interval
tell application "System Events" to set newSize to size of disk item theFolder -- recheck size
if (newSize is equal to currentSize) then
if possible then -- no change since last time
return true -- success
else -- one more time...
set possible to true
end if
else -- update size & possible switch
set {currentSize, possible} to {newSize, false}
end if
end repeat
return false -- fail (timeout)
end waitForFilesToCopy