About a week ago, I posted for some help with a “check folder size” portion of one of my scripts. Here’s what I ultimately came up with:
tell application "Finder"
if folderStatus is equal to true then
set folderSize to size of folder FolderToBeBackedUp
repeat until folderSize is not missing value
delay 0.5
end repeat
set legibleSize to ((folderSize / (1024 ^ 3) + 0.5) as integer)
set folderPath to POSIX path of (FolderToBeBackedUp as alias)
if legibleSize > 8 then
set errormsg to {"The backup did not run because the size of the folder selected for backup, \"" & FolderToBeBackedUp & "\", is " & legibleSize & " GB and too large for a typical writable DVD." & return} as text
my WriteLog(errormsg)
set DoBackup to false
set BackupSuccess to false
end if
end if
end tell
The problem is that this function cause Applescript to hang, and I’m not sure why. Sometimes if I kill the script run and run it a second time right afterwards, it succeeds. But I don’t see any bug that would cause the hang. Comments would be appreciated! I do know that it’s not the call to WriteLog that’s causing the hang.
the problem is, you check once the size of the folder, but not in the repeat loop, so it hangs.
Try this
tell application "Finder"
if folderStatus then -- this is the same as "if folderStatus is equal to true"
repeat until (size of folder FolderToBeBackedUp) is not missing value
delay 0.5
end repeat
set legibleSize to (((size of folder FolderToBeBackedUp) / (1024 ^ 3) + 0.5) as integer)
set folderPath to POSIX path of (FolderToBeBackedUp as alias)
if legibleSize > 8 then
set errormsg to {"The backup did not run because the size of the folder selected for backup, \"" & FolderToBeBackedUp & "\", is " & legibleSize & " GB and too large for a typical writable DVD." & return} as text
my WriteLog(errormsg)
set DoBackup to false
set BackupSuccess to false
end if
end if
end tell
Thank you, Stefan! Your way works a treat. I’m still not entirely clear on why my method doesn’t work, as I would think that the line “set folderSize to size of folder FolderToBeBackedUp” could be issued once and then a subsequent command could wait for the set command to be “completed”, but I guess it doesn’t work that way.
the variable folderSize is set to the value of the size at the moment,
but not as a reference, which changes dynamically. This works also
...
set folderSize to size of folder FolderToBeBackedUp
repeat until folderSize is not missing value
set folderSize to size of folder FolderToBeBackedUp
delay 0.5
end repeat
...