Using Tell, but not on an application

StefanK gave me a great shortcut to a script and I didn’t know it existed.


	tell (current date) to set dayStamp to day

Now I’ve read a few AppleScript books and not one of them ever showed this technique, and the libraries never gave me any clue this would work.

I know about Tell to an application, and even Tell to a Window or Button (in System Events when doing UI scripting). But I’d have never of guessed to Tell a system-generated object like “current date.”

How do you know what objects can be “told” directly like this? How do you get any clue as to how the syntax would work?

THANKS!

Hi Calvin,

When you:

tell (current date)

You’re not telling the command (i.e. ‘current date’ is not the target), but the value returned which is an object (an AppleScript date):


set d to current date
class of d

With an AppleScript date, you can use anything that pertains to the date class.


tell (current date)
	{time, time string, date string, day, month, year, it as string} -- etc.
end tell

gl,

Edit: kel beat me to it. :slight_smile:

You can use tell with any object. Note, though, that commands like current date, info for, etc., are being evaluated, and you are “telling” the result; That is, your not telling current date to do something, your telling a date object to do something.

When inside a tell value, you can refer to the object as it.

If I want to find out the available properties/methods of an object I:
a) go and look it up in a dictionary
b) look in the ADC -Reference Library

Is there another way?
like: get properties of (current date)

Hi Luke,

This is from the AppleScriptLanguageGuide.pdf:


A fixed number of specific types of values are recognized by AppleScript. You
cannot define additional types of values, nor can you change the way values
are represented. The different types of AppleScript values, called value classes,
are described in Chapter 3, “Values.”

No value has a property ‘proeprties’ where you can get its proerties. Some application objects have a property ‘properties’ where you can get the properties of the object. For instance, Finder has a prperty ‘properties’:


tell application "Finder"
	properties
end tell

If you want the properties of values see AppleScriptLanguageGuide.pdf. Here’s the old list of properties from the date class:

Class The class identifier for the object. This property is read-only, and
its value is always date.
Weekday One of the constants Monday, Tuesday, Wednesday,
Thursday, Friday, Saturday, Sunday or Mon, Tue, Wed, Thu,
Fri, Sat, Sun.
Month One of the constants January, February, March, April, May,
June, July, August, September, October, November,
December or Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep,
Oct, Nov, Dec.
Year An integer specifying the year; for example, 1993.
Time An integer that specifies the number of seconds since midnight
of the date value; for example, 2700 is equivalent to 12:45 AM.
Date A string that consists of the date portion of the date value; for
example, “June 3, 1993”.

For newer properties, you need to read the revision notes.

gl,

Inside a ‘tell’ block, unattributed properties are normally associated with the object being ‘told’. Sometimes, though, a terminology clash or some other consideration may make the compiler think that a particular ‘property’ makes perfect sense in some other context. In the example above, for instance, the ‘month’ is simply returned as ‘month’. (I’m not sure what the clash is here.) Similarly, the recently added date properties ‘hours’ and ‘minutes’ return 3600 and 60 respectively, because these are the values of AppleScript constants with the same names. In these cases, you need to be explicit about referring the property names to the object being ‘told’:

tell (current date)
	{time, time string, date string, day, its month, year, it as string, its hours, its minutes} -- etc.
end tell

You can be explicit in any case, if you like.

As with all ‘tell’ blocks, you can only use commands inside them that the ‘told’ objects can understand (or, sometimes, that can understand the ‘told’ objects):

tell (current date)
	(its month as string) contains "r"
	--> 'true' or 'false'. ('as string' and 'contains' are both vanilla AppleScript.)
	
	display dialog (its month as string)
	--> Error. ('display dialog' is a StandardAdditions command, which dates can't access.)
	
	set monthStr to (its month as string)
	tell me to (display dialog monthStr)
	--> The application running the script ('me') displays the dialog.
end tell

It’s better practice not to nest ‘tell’ blocks to unrelated objects if that can be avoided, so my last example above would be better written as:

tell (current date)
	set monthStr to (its month as string)
end tell
display dialog monthStr

. or, of course:

display dialog ((month of (current date)) as string)

Interestingly you can create an internal “library” in a script via:


script math

	property e : 2.718281828459
	
	on logb(base, numero) -- logb(a,c) computes the log of c in the base a.
		return loge(numero) / (loge(base))
	end logb
	
	
	
	on loge(x) -- computes the natural log of x.
		set sinal to 1
		if x = 1 then
			return 0
		else if x < 1 then
			set x to 1 / x
			set sinal to -1
		end if
		set {loga, j, zz} to {1, 1, 1}
		try
			set {astid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, "E"}
			set zpow to (text item 2 of (x as text)) as number
			set AppleScript's text item delimiters to astid
		on error
			set AppleScript's text item delimiters to getdec()
			set zpow to (length of (text item 1 of (x as text))) - 1
			set AppleScript's text item delimiters to astid
		end try
		set {x, logex} to {x / (e ^ (2.3 * zpow)), zpow * 2.3} -- this part takes care of the computation of very large numbers.
		--Selects different points for Taylor series, this makes the computation more precise and faster. 
		
		if x > 1.8 and x < 5.2 then
			set {zz, logex} to {e ^ 1, logex + 1}
		else if x > 5.2 then
			set {zz, logex} to {e ^ 2, logex + 2}
		end if
		
		set erro to 1.0E-19
		repeat while loga > erro
			set loga to logex
			set logex to (x - zz) ^ j * ((-1) ^ (j - 1)) / (j * (zz ^ j)) + logex
			set loga to abs(logex - loga)
			set j to j + 1
		end repeat
		return sinal * logex
	end loge

end script

repeat with i from 0 to NumFontSizes

	tell math
		set Threshold to Threshold & (100 * (logb(10, MINCOUNT + (i * sizeDelta) + 2))) as list
	end tell
	
end repeat

You can extend this by loading external script files into a variable and “telling” the loaded script to access its various methods. Like this:

property MacPackLib : "" --MacPackLib container

		set libPath to ((path to extensions) & "MacPackLib" as string)
		try
			set MacPackLib to load script alias libPath
		on error msg number num
			if num = -43 then
				display dialog "WordScore cannot be loaded because MacPackLib is not in the Extensions folder." buttons {"Cancel"} default button 1 with icon stop
			else
				error msg number num
			end if
		end try
		tell MacPackLib
			set tmpVersion to getVersion()
		end tell

Note that you can load external run-only scripts, which prevents unintended editing or viewing of proprietary/sensitive info (logins, database schema, whatever).

Model: iMac Core Duo 17"
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

I would assume it’s because month is also a class. (I wish I knew more, but that’s all I have for today.)

Sometimes, the things Apple does, doesn’t make sense to me. Why would they add a property with same or partial label as an AppleScript constant (hours, minutes, month).

gl,

Why not? There can be a peaceful coexistence.
minutes without any reference is the constant
minutes of [aReference] is the property.

Those duplicates work also easily e.g. return, which can be ASCII charcter 13 or the keyword

Hi Stefan,

See Nigel’s post. “terminology clash” in tell block.

Edited: a good name for the new property might be ‘dhours’. Then you could just write:

tell (current date)
dhours
end tell

Scripters in New Orleans might like this. :slight_smile: get dhours. Human readable!

Edited: In a post from HawaiiScripterGuy:

“I tried: dahours of (current date), but it didn’t work.”

NewOrleansGuy replied:

“It’s not dahours, it’s dhours.”

gl,

This would look odd though (to me, at least). :stuck_out_tongue:

return return

Edit: On a more serious note. You almost never use those two terms in a way that would conflict. If you did, you would still have a terminology clash.

return
"Hello" & result & "World"
--> Doesn't return the text I wanted