[snip code]
It’s a rather pretentious, gratuitously obfuscated and completely unwarranted way to write:
on find_and_replace(the_string, search_string, replace_string)
set old_atid to text item delimiters
set text item delimiters to search_string
set temp_list to the_string's text items
set text item delimiters to replace_string
set the_string to temp_string as Unicode text
set text item delimiters to old_atid
return the_string
end find_and_replace
K.I.S.S.
…
Nope. The ‘a reference to’ operator is used to create a reference object that identifies a property or element of an object. Reference objects can be a bit confusing to wrap your head around at first, and are easiest to understand by example. Let’s take a list containing several elements:
set lst to {1, 2, 3, 4, 5}
Most times when you write a reference identifying an element in the list, you want that reference to be resolved immediately, returning the value stored at that location:
set val to item 3 of lst -- variable 'val' contains the value '3'
Sometimes though you don’t want to get the value stored at a particular location straightaway; instead you just want to keep a note of where that location is so you can fetch a value from it later on. So you create a reference object to that location instead:
set valRef to a reference to item 3 of lst -- variable 'val' contains the reference object 'item 3 of <the list object>'
Later on, to resolve the reference, you ask the reference object for the value of its ‘contents’ property:
set val to valRef's contents --> the value [i]currently stored[/i] at item 3 of the list
What’s the significance of all this? Well, compare the two following examples:
set lst to {1, 2, 3, 4, 5}
set val to item 3 of lst
set item 3 of lst to 0
val --> 3
set lst to {1, 2, 3, 4, 5}
set valRef to a reference to item 3 of lst
set item 3 of lst to 0
set val to valRef's contents
val --> 0
The real advantage is that you can manipulate reference objects just like any other objects: store them in variables, pass them to handlers, and so on. e.g. You’ll often use them in ‘repeat’ blocks to manipulate a list’s content in-place, e.g. to change a list of Finder references into a list of aliases without having to build a second list:
repeat with itemRef in finderFileRefsList
set itemRef's contents to itemRef's contents as alias
end repeat
For most tasks you’ll never need to create your own reference objects though as this is really only useful in advanced programming tasks (or for showing off one’s l33t h@X0r credentials, I suppose).
…
Tell blocks are generally used to specify a common target for a whole bunch of commands to be sent to. (Most often that target is an application object, though any object is acceptable.) You can also refer to the target object’s properties by using the ‘its’ keyword:
set rec to {a:1, b:2, c:3}
tell rec
get its b --> 2
end tell
(Note that the ‘its’ keyword can be omitted in some cases, though is required in others.)
Tell blocks are really just a stylistic device as there’s other ways to achieve exactly the same ends. While tell blocks can be good for reducing the size and complexity of code in some cases, gratuitous usage tends to increase code complexity instead, making code harder to read and understand, increasing the chances of it containing bugs and making it harder to modify in future.
When it makes code easier to read than writing it some other way. As a rough rule of thumb, if you’re sending several commands to the same object(s) at the same time then your code will be cleaner and more compact if you use a tell block; otherwise don’t bother. Developing good coding style is mostly just a matter of practice; for example, the following examples all do the same thing, so deciding which form to use is really just a matter of picking the one that’s the most readable and least likely to make you tie yourself in knots:
tell application "Finder"
set fileNames to name of every file of target of front Finder window
set fileSizes to size of every file of target of front Finder window
set fileLabels to label of every file of target of front Finder window
end tell
tell application "Finder"
set filesRef to a reference to every file of target of front Finder window
set fileNames to name of filesRef
set fileSizes to size of filesRef
set fileLabels to label of filesRef
end tell
tell application "Finder"
tell every file of target of front Finder window
set fileNames to its name
set fileSizes to its size
set fileLabels to its label
end tell
end tell
HTH
has