I think this might be just a quickie for most of you guys, I just want to know WHY? i would
and (HOW? if you have any simple examples)i would use “tell me to blah blah blah…”:
tell me
--do something
end tell
Just seem to have come across it a few times recently, not in any scripts i’ve needed to use just
seen it while surfing around.
When you are in a tell block for an application, then, for example, a handler in your main script will not be recognized as a function within the application and you have to refer to it as “my handler(blah)” so the compiler will look back to your script. Tell me has the same functionality.
That’s useful for a quick call to the highest level, within (nested) tell blocks, for example:
tell application "System Events"
tell process A
--.....
tell window b
--.....
tell me to do shell script "sleep 10"
set rand to my doSomeStuff()
end tell
end tell
end tell
on doSomeStuff()
return random(11)
end doSomeStuff
Here it is not window b of app A that’s sleeping for 10 seconds but the script itself.
Also, the doSomeStuff routine on the higest level (here: the script) can be called from within nested tell statements
Here is a lengthy but good example:
I have a subroutine at the bottom called LogToConsole, which takes a message and writes it to the Console.
The first two calls to this subroutine (at the top, labeled with the “–<<<<======…” markers) are outside of any tell block, so Applescript directs them to the script itself.
The second two calls to the subroutine are within a tell block to Excel, and are prefaced by the “tell me to…” syntax so that the message will not go to Excel but instead will go to “me”, namely the script itself.
logToConsole given theMessage:"TIAARealEstatePricetoExcel Executed." --<<<<=============================
if weekday of (current date) is in {Sunday, Monday} then return
logToConsole given theMessage:"Weekday Identified." --<<<<===================================
set {tiaaFund, tiaaPrice, tiaaDate} to getTIAARealEstateAcctInfo()
set tiaaDateString to date tiaaDate
set TIAAWorkbookName to "TIAA Real Estate.xls"
tell application "Finder" to set TIAAPath to "Grumpy:Users:johnnylundy:Documents:Finance:Retirement:" & TIAAWorkbookName
tell application "Microsoft Excel"
activate
if not (exists workbook TIAAWorkbookName) then open workbook workbook file name TIAAPath
activate object workbook TIAAWorkbookName
select (find range "Date_Price_List" what "")
set lastDatePosted to value of range (convert formula formula to convert "R[-1]C" from reference style R1C1 to reference style A1)
if lastDatePosted is equal to tiaaDateString then
tell me to logToConsole given theMessage:"Fund info for date retrieved from TIAA (" & tiaaDate & ") is already in spreadsheet - new row not added." --<<<<<<===========================================
else
set value of selection to tiaaDate
set value of (find range "Date_Price_List" what "" after selection search order by rows) to tiaaPrice
select range (convert formula formula to convert "R[-1]C[2]:RC[7]" from reference style R1C1 to reference style A1)
fill down selection
tell me to logToConsole given theMessage:" Fund info retrieved from TIAA has new date (" & tiaaDate & ") - data added to spreadsheet." --<<<<<<<========================================================
save active workbook
end if
end tell
to getTIAARealEstateAcctInfo()
set ATID to (a reference to AppleScript's text item delimiters)
set tiaaURL to "https://www3.tiaa-cref.org/ddata/DownloadData?f1=1009&days=1"
set tiaaResult to do shell script "curl -k -w 10 " & quoted form of tiaaURL
set savedDelims to contents of ATID
set contents of ATID to {","}
set {theFund, thePrice, rawDate} to text items of tiaaResult
set contents of ATID to return
set theDate to first text item of rawDate
set contents of ATID to savedDelims
return {theFund, thePrice, theDate}
end getTIAARealEstateAcctInfo
to logToConsole given theMessage:theMessage
set theString to "TIAARealEstatePriceToExcel: " & theMessage
do shell script "echo $(date) " & ":" & quoted form of theString & " >> /dev/console"
end logToConsole
Another reason is when you want to use a conflicting keyword. I think it was Jon’s Commands’ sound volume had the same keyword as QuickTime Player’s sound volume. Tell me allows the scripting additions command to compile.
Good catch. TextEdit overlaps in several places (text item delimiters, eg), offset won’t work inside some tell Object blocks without a “my” or “of me” or “tell me”.
I have used the “my” before my handler and even the handler then “of me”
but only did that cause someone told me or i read somewhere that you had to do that
to run the subroutine from inside a tell block.
I have yet to come across a script i have written where i need to use the “Tell me” line
but with everyone’s help here i know where to begin if the time arises.
I’m coming in late to this but I thought I would add a couple of examples I have used:
Sometimes a display dialog will pop up behind another window you have open depending on what tell block the display dialog is in. I use this sometimes to make sure it will display up front (someone out there will probably let me know there is a better way to do this but here it is anyway):
Tell me
display dialog "Whatever"
end tell
If you are error trapping you can tell the script to quit:
Tell me
quit
end tell
Model: G5 Tower (not Intel) - Script Editor ver 2.1.1
AppleScript: Tiger
Browser: Safari 419.3
Operating System: Mac OS X (10.4)
The ‘tell me’ (or ‘my’, or ‘of me’) construct usually arises within an existing tell statement, when another target needs to be addressed (perhaps only briefly). In general, though, nested tell statements are best kept to a minimum ” or avoided altogether. (Yeah, I know ” I can talk… ;))
In practice, typically to avoid restructuring a script, it can occasionally be more expedient to slip in an additional tell statement within an existing one.
Matt-Boy’s suggestion is a good one, although the dialog example presupposes that the application running the script (a.k.a. “me”) is frontmost at the time. Sometimes, depending on what the script or the user may be doing, this might not be the case.
For example:
tell application "TextEdit"
activate
tell me to display dialog "Excuse me..."
end tell
Sidebar: Script construction
However, I digress. If the aim is to grab the user’s attention as soon as possible, without invoking those amusing little bouncy Dock icons, a slightly safer method might be:
tell application "TextEdit"
activate
tell application (path to frontmost application as Unicode text) to display dialog "Ah - there you are."
end tell
Another situation in which the “me” construct might come in handy is when using indirect references in a nested tell block.
In most cases, we’d name any target application directly ” like this:
tell application "Finder"
activate
display dialog "Finder"
tell application "TextEdit"
activate
display dialog "TextEdit"
end tell
end tell
That works as intended; each application is activated in turn and, while frontmost, displays a dialog.
There are circumstances, though, in which targets are defined at runtime ” depending on what conditions exist at the time. And these indirect references won’t necessarily behave in quite the same way as the more direct ones. Note, for example, how Finder remains frontmost when this version is run:
set app1 to "Finder"
set app2 to "TextEdit"
tell application app1
activate
display dialog app1
tell application app2
activate
display dialog app2
end tell
end tell
That can be worked around by re-establishing the script (“me”) as the source of the commands:
set app1 to "Finder"
set app2 to "TextEdit"
tell application app1
activate
display dialog app1
tell me to tell application app2
activate
display dialog app2
end tell
end tell
All the same, perhaps I should just reiterate that earlier caveat: unless you fully understand what you’re doing, try to avoid (or at least, to minimise) nested tell statements…
set app1 to "Finder"
set app2 to "TextEdit"
tell application app1
activate
display dialog app1
end tell
tell application app2
activate
display dialog app2
end tell
A common requirement for “my” or “of me” is when you are telling an object to do something to avoid having to repeat the object attribution every time it’s used. In this example “tell item k of Cmac” doesn’t know how to get an offset but StandardAdditions does:
set LC to "abcdef"
set UC to "ABCDEF"
-- a MAC address as returned from the shell is all lower case
set MAC to "00:14:51:66:ab:fe"
set Cmac to characters of MAC
-- Need it capitalized for some other application that insists on capitalized hex
repeat with k from 1 to count Cmac
tell item k of Cmac to if it is in LC then ¬
set item k of Cmac to item (my (offset of it in LC)) of UC
end repeat
set MAC to Cmac as text --> "00:14:51:66:AB:FE"