I’m getting errors in the script i’m working on that SEEM to be coming from my progress bar related commands being inside a “tell app “System Events”” clause.
I never had problems with other windows like dialogs and list choosers going inside tells. Can progress bars not go in there?
Here’s the problem if that’s the case: I have a repeat loop where the loop’s while clause is getting disk information from System Events. But inside that same loop, I’m updating the progress bar as the loop iterates.
So if progress bar updates can’t be inside tell statements, then how do I do this? Because that would mean the repeat loop would have to be inside the tell for it’s while to work, but it would have to be outside of a tell for it’s progress bar updates to work?
Update:
It appears that yes, progress bar related commands have to be outside of tell clauses.
So to get this to work, I had to set a variable in a tell clause before my repeat loop. Then inside the loop, I have a tell clause for an action, then no tell l cause to set progress bar properties, then another tell loop to update the variable that is controlling the loop. And the loop itself just checks the variable, it doesn’t actually talk to any apps (no tell).
Its a somewhat obscure way to do things, and it makes me wonder, is there any overhead associated with opening and causing so many tell clauses?
If so, is there a way to keep a loop in a tell clause, but to exit out of the clause temporarily just for some specific commands (the progress bar commands)?
This is probably going to be confusing to read so maybe the code itself will help it make more sense.
tell application "System Events"
set FreeMB to (((free space of disk (tdname of TargetDisk)) / 1000000) - FillBuffer)
end tell
repeat while FreeMB is greater than PayloadSize
tell application "System Events"
set PercentComplete to round 100 - ((free space of disk (tdname of TargetDisk)) / (capacity of disk (tdname of TargetDisk)) * 100)
end tell
set progress completed steps to PercentComplete
set progress additional description to "Writing payload_" & i & ".txt" & return & "Free space: " & DisplayFreeSpace(FreeMB)
delay 0.1
do shell script "openssl rand -out '" & PayloadPath & "payload_" & i & ".txt' " & (PayloadSize as string) & "000000"
set progress description to "Creating Payload Files... (ETA: " & CalculateETA(start, PercentComplete) & ")"
set i to i + 1
tell application "System Events"
set FreeMB to (((free space of disk (tdname of TargetDisk)) / 1000000) - FillBuffer)
end tell
end repeat
Since a tell statement targets a specific application, to avoid conflicts, stuff that isn’t meant for an application really shouldn’t be in its tell statement, or if they must be the commands need to use my or of me to target the main script. Some commands from StandardAdditions such as display dialog will work in an application tell statement without issue, but the progress commands are not part of that - AppleScript uses the system ScriptMonitor application for those.
You should be able to use something like set my progress completed steps to whatever inside a System Events tell statement, but your solution is the best practice.