On May 28, 2010, at 12:17 PM, Mark J. Reed wrote: Regular scripting requires that the app be scriptable. GUI scripting doesn't - it fakes mouse clicks and keypresses so the app thinks a human is driving it.
Something to keep in mind ... GUI scripts are fragile.
I'm currently using a script with just two lines of GUI code within a loop. Like this ...
repeat with msg in msgList -- lots of script here tell application "System Events" tell process "Mail" repeat if exists window 1 then exit repeat delay 1 end repeat keystroke "p" using {command down} delay 1 repeat if exists sheet 1 of window 1 then exit repeat delay 1 end repeat click button "Print" of sheet 1 of window 1 delay 1 end tell -- lots more script here end repeat
If I forget that it's running and click the desktop at just the right moment, it hangs. Why? Because GUI commands aren't directed at a particular application (no 'tell application' with GUI), but to the screen. If Mail is not frontmost, the script waits for something that can't happen. I suppose I could fix this with a more complicated script, but is it worth it?
The real problem is that both the user and the script are trying operate the same computer at the same time. If two humans were trying to do this, there would be negotiations leading to one waiting until the other had finished.
A script needs to be able to take possession of an application or computer and lock-out other users (or scripts) until it is finished. Too bad this feature does not exist.
|