I am unfortunately terribly new at Applescript and don’t know of many of the functions I would use.
How can I get Applescript to check all files in a folder to verify their size isn’t non-zero on a repeating loop?
I am having issues with Applescript not waiting til previous processes finish (copying files to a folder - via a unix cp/mv) and I am assuming this will fix the problem.
It starts the music/photo import before the files have finished sorting and I see folders full of a couple real items and zero sized files and, of course, it errors on zero sized files.
Thank you, as always, in advance for any direction.
I have a script that someone else here at work created, that contains a subroutine to check that files have finished copying to a folder.
I am sure I have also seen something similar (if not exactly the same) on this forum.
on adding folder items to thisFolder after receiving theItems
if folderReady(thisFolder) then -- Delays script start for network to catch up with file copy
--subroutine below
on folderReady(theFolder)
set myFolder to theFolder as alias -- use the path to the folder that's loading
set firstSize to size of (info for myFolder) --get initial size
delay 1 --wait 1 seconds
set newSize to size of (info for myFolder) --get a newer size, bigger
repeat while newSize ≠firstSize --if they don't equal, loop until they do
set firstSize to newSize --new base size
delay 1 --wait 1 seconds
set newSize to size of (info for myFolder) --get a newer size
end repeat --once the sizes match, the transfer is complete
return true
end folderReady
Perhaps you could incorporate this into your existing script?
That is not possible. The finder is not a file manager but is an application that displays files for you. Why it’s not possible is simply: do shell script only continues when the the process has finished or reached timeout. When the process has finished, it means it is stopped and ready till the last bit, then you’re guaranteed the copy has been successful. The problem you have is that the Finder hasn’t been updated yet (the finder is a high level application that display files and is not part of the file managers in your machine).
Thank you Intoto and Bazzie Wazzie.
I incorporated a little bit of each into what I had to do.
I couldn’t ditto files based off name (ditto path/to/*.EXT), so I had to stick with cp and mv, but I was able to use ditto in my pre-processing full file backup.
I had to build a loop that checked file count every 5 seconds (quick verification) and if that passed it did a check to see if any file size changes on an interval of 0.5 seconds (if file count the same but copying a large file it would catch it).
If either of these were triggered as the directory is still changing, it would restart the entire loop.
Here’s the script of what I did:
repeat until goodCount = 1 and goodSize = 1
set newItemCount to -1 as integer
set oldItemCount to 0 as integer
repeat until newItemCount = oldItemCount
set oldItemCount to newItemCount
delay 5
tell application "Finder"
set newItemCount to count of (files in folder this_folder)
end tell
end repeat
--Item count has passed, now check to see if item size is changing as well.
set goodCount to 1
repeat with f in these_items
set oldSize to 0
set newSize to -1
set goodSize to 1
-- When newSize equals oldSize, it means the copy is complete because the size hasn't changed.
repeat while newSize is not equal to oldSize
-- Get the file size.
set oldSize to size of f
delay 0.5
-- Sample the size again after delay for comparison.
set newSize to size of f
--If item size is still changing, then set goodSize to 0, which triggers the overall file check repeat again.
if newSize is not equal to oldSize then
set goodSize to 0
end if
end repeat
end repeat
end repeat