Hey, Kevin.
You’re certainly not the only one who has had problems with this. I’ve pointed out to dozens of people the importance of keeping code clean and consistent… and tell blocks are one place where you can not afford to try to cut corners with your code. The difficult part is, that you can not always know up front whether a bit of code will be adversely effected by placing it inside of a tell block. There are a lot of ways that a given bit of code can be interpreted, and there are certainly many times when a bit of code can be placed in a tell block and it will perform just fine. BUT… is that to say that we SHOULD put it in there? In my opinion, no. For years now, I’ve been watching others bang their heads over the simplest of problems, because they have not taken the time to really look at the code they’ve written to see if it makes sense. Remember, we’re writing a language, not just a bunch of chicken scratches that somehow magically work. Writing good code… especially in applescript… requires only that you write out the story that you want your application to follow. What I’ve found in my time spent scripting, is that it often saves me time and frustration to simply clarify what it is I want to do, and then carefully go through each necessary step to accomplish the goal. Sounds simple, but we often get carried away in “streamlining”, condensing code, and trying to get the job done in as little time as possible. When we get off on these idealistic tangents, though, people forget that the code should just be written in such a way that it gets the job done while being clear, smart, and modular. While it sounds easy and convenient to say that up here on my soap box, it’s really the truth I think, and I see people getting way ahead of themselves a lot of the time trying to ‘change the world’ in a day rather than just slowing down and doing things correctly.
I have pointed out countless times to people that they have code in places that it shouldn’t be. Often it is within tell blocks, and although it sometimes works I always try to point out the importance of re-writing the code so that it is not within the block, just on principal. While it may seem trivial to someone who’s just starting out with AS and who has never had a problem with a tell block conflict, it is an important concept and should be learned for many reasons. While style is important, I think it is of even greater importance to write good code on principal. By writing clean, literal code all the time you are speaking the machine’s language more precisely, leaving yourself less open to the chance of your intentions being misunderstood. Getting the job done is no substitute for getting the job done right.
For example, a common pitfall is using a command or calling a subroutine from inside of an application tell block…
tell application "Finder"
set theNewWindow to (make new Finder window)
someSubroutine()
close theNewWindow
end tell
Because the finder has no idea what a ‘someSubroutine’ is, it errors. If “someSubroutine()” were replaced with something a bit more benign like “beep”, it would function fine and you’d never know the difference. Of course, you could do “my someSubroutine()” and all should be well, but that’s beside the whole point I’m trying to make. Many of you might argue that using “my…” would be appropriate, and I will concede to that in this simple case. But, it’s also important to think about why using it might NOT be the best option, considering the broader concept of writing clearer code.
Why not just do this?..
tell application "Finder" to set theNewWindow to (make new Finder window)
someSubroutine()
tell application "Finder" to close theNewWindow
The script size change is negligible, the script is easier to read, and at no time are you in question of whether any conflict might occur because of a tell-block conflict. While there are certainly occassions where this may not be appropriate or reasonable, it illustrates the importance of being clear in your intentions. Not only will you be able to read through this tomorrow or 2 years from now and see quite obviously what it’s intentions are, if you have a problem with it the likelihood of finding the problem quickly increases because you’ve already eliminated the more obvious obstacles by simply writing good code from the beginning.
As you’ve said, Kevin, in ASStudio we also often run into tell-block conflicts with object references. I’ve seen many occassions where people get errors referencing objects because of what you describe. Most of the time, it is newer users who’ve not had the pleasure of staying up until 2 in the morning looking for something so simple as a tell-block conflict that they never thought of or considered. While it makes perfect sense to an experienced scripter, someone who’s never learned the lesson wouldn’t have a clue that they even needed to look for such a gremlin.
Ultimately it comes down to learning how to write your code so that it follows as logically as possible with how the compiler will try to interpret what you as a programmer are telling it. By placing any call inside of a tell block that doesn’t need to be there, you’re breaking the logical flow of the code and leaving yourself open to unforeseen problems both now and into the future. As you write more and more scripts, you train yourself to write in a certain style. Training yourself to write in a way that makes no sense (other than by sheer luck of not encountering a truly logical boundary) leaves you vulnerable to hours of brain-racking, head-banging, hair-pulling frustration. I’ve been preaching the importance of writing clean code since I started coming here, and I still think it’s the cause of 80% of the problems users have with their code. While it takes a long time to learn all of the intricacies of any language, it is important nonetheless to start practicing good code-writing skills the day you start writing code.
Applescript is so pleasant. It’s pretty. You write it and it flows like poetry… it often even looks like poetry, because of it’s english-like terminology and user-friendly focus. Scripters should remember this, viewing their creations not just as work… but as works of art. People often post these insanely messy jumbles of code that look scary and unappealing. I rarely bother to read code samples that are obviously not thought-out at all. Nothing personal, I just don’t have the time to point out to people that they’re not focused, are rushing into something that’s obviously beyond their skill set, or that they’re just lazy. If you’re code is clear and says only what it means, it is always beautiful and a pleasure to read. And, on top of the benefits of having good form, you’ll end up with a product that also has better function.
Just my two cents for the day, take care all…
j