Date Format Scripting Addition Syntax

I’m working on changing the song name of an itunes song to include a calculated date in format yyyymmdd. I’ve found the scripting addition Date Format at http://www.rtlabs.com/downloads/. I’ve downloaded the OS X version and placed it in my ScriptingAdditions folder. When I use the included sample script below I get the error message below.

Sample script:

 set theDate to format date (current date) with format "%m, %d %Y"

Error:
A class name can’t go after this identifier. (Hilight on “format date”)

I’ve installed another scripting addition just to make sure I’m not setting up things wrong and all works fine.

Either the syntax must be changed for Panther’s Applescript or I must be doing something stupid since the example in the documentation won’t even work for me.

Any help would be appreciated.

RandyChev

Model: Powerbook G3/400 Pismo
AppleScript: 1.9.3
Browser: Safari 125.12
Operating System: Mac OS X (10.3.8)

If that line’s in an iTunes ‘tell’ block, it may be that there’s a terminology clash with something in iTunes. Or you need to parenthesize the whole of the ‘format date’ command:

set theDate to (format date (current date) with format "%m, %d %Y")

But if you just want yyyymmdd format, and you’re using Panther or later, it’s easy to format the date using vanilla AppleScript:

set {year:y, month:m, day:d} to (current date)
set theDate to (y * 10000 + m * 100 + d) as string

I think this scripting addition is broken. You can use instead:

do shell script "date '+%m/%d/%Y'"

(type “man date” in a Terminal window for more options)

Thanks for all the help. I love learning different ways to do things.

The shell script wouldn’t work from what I could see since it uses the current date only. I need to use a calculated date.

Reformatting the Date Format script didn’t help. I believe that scripting addition is indeed broken in Panther. They should document that to save stubborn guys like me a whole lot of time.

The “vanilla AppleScript” worked great. Just what the doctor ordered.

Thanks again!!!

RandyChev

I couldn’t get it to work either even after adding parentheses. I’m using Tiger 10.4.6. Maybe it’s version compatibility issue?

Thanks! This is a much more elegant solution than my old script. All I want is to reformat dates to yyyy.mm.dd. Problem number one is when I bump into 1 digit date/month. Can’t use unix command either since what I want to reformat is not current dates. I’m still hoping it’ll get fixed or something someday.

This was my old solution, btw:

on date_format(old_date)
	set y to year of date old_date
	set m to month of date old_date as integer
	if m is less than 10 then
		set m to "0" & m as text
	end if
	set d to day of date old_date as integer
	if d is less than 10 then
		set d to "0" & d as text
	end if
	set new_date to y & "." & m & "." & d as text
	
	return new_date
end date_format

An this is the new elegant one (thank you!):

on date_format(old_date)
	set {year:y, month:m, day:d} to date old_date
	set ymd to (y * 10000 + m * 100 + d) as string
	set new_date to (text items 1 thru 4 of ymd as string) & "." & (text items 5 thru 6 of ymd as string) & "." & (text items 7 thru 8 of ymd as string)
	return new_date
end date_format

Hi cannedbrain. Your handler’s looking good. :slight_smile:

You could probably refine it a little further, with something like:

on date_format(old_date)
	set {year:y, month:m, day:d} to date old_date
	tell (y * 10000 + m * 100 + d) as string to text 1 thru 4 & "." & text 5 thru 6 & "." & text 7 thru 8
end date_format

Thanks kai! I never knew you can coerce using a tell block. :thumbs up:

Actually, the thing that’s being ‘told’ here is the result of the coercion. Do the maths, coerce the result to string, tell the result of that to concatenate “.” to (its) text 1 thru 4, etc…

Yeah - I don’t know if it might help to clarify some of the stages involved in this process, but let’s try and break down what actually happens here.

As we go through it, watch out for a nifty little automatic coercion in the routine that multiplies the month constant April by 100. (And since April is the fourth month in the year, the result is 400.):

April * 100
--> 400

So here’s a more detailed version of the handler - from which you should be able to recognise the various steps that Nigel mentioned:

date_format("12 April 2006") (* for example *)

on date_format(old_date)
	
	(* get the date  *)
	
	date old_date
	--> date "12 April 2006" [ coerce date string to date ]
	--> date "Wednesday, April 12, 2006 00:00:00"
	
	(* get the date elements [year, month & day] from the date *)
	
	set {year:y, month:m, day:d} to result
	--> (* y = 2006, m = April, d = 12 *)
	
	(* do the maths *)
	
	y * 10000
	--> 2006 * 10000
	--> 20060000
	
	result + m * 100 (* [m * 100] = [April * 100] = [4 * 100] = 400 [ automatic coercion ] *)
	--> 20060000 + 400 
	--> 20060400
	
	result + d
	--> 20060400 + 12
	--> 20060412
	
	(* coerce the result to string *)
	
	result as string
	--> 20060412 as string
	--> "20060412"
	
	(* tell the result to format the output *)
	
	tell result (* tell "20060412" *)
		
		text 1 thru 4 (* text 1 thru 4 of "20060412" = "2006" *)
		--> "2006"
		
		result & "."
		--> "2006" & "."
		--> "2006."
		
		result & text 5 thru 6 (* text 5 thru 6 of "20060412" = "04" *)
		--> "2006." & "04"
		--> "2006.04"
		
		result & "."
		--> "2006.04" & "."
		--> "2006.04."
		
		result & text 7 thru 8 (* text 7 thru 8 of "20060412" = "12" *)
		--> "2006.04." & "12"
		--> "2006.04.12" [ final result returned by handler ]
		
	end tell
end date_format

Of course, if that version’s a bit too long for you, there’s always:

tell "12 April 2006" to tell my date it to tell (year * 10000 + (its month) * 100 + day) as string to text 1 thru 4 & "." & text 5 thru 6 & "." & text 7 thru 8

:wink:

I think this BBS needs a smiley for “howls of pain”!

:wink:

Just couldn’t resist going from one extreme to the other, Mr G. (Figured it might elicit a wince from you.) :stuck_out_tongue:

Wow! I’ll definitely go with this one. :smiley:

May I ask, how do “my” and it" (in “tell my date it to tell” work in this script? I’m guessing they user-defined objects (?) I’ve seen how “my” is used when calling handlers but never seen it used this way.

See if I can get this right before Kai tells us…

“my date it” – the “my” refers to the script so that “date” is understood to be a key word in AppleScript rather than belonging to “12 April 2006” which is just text. The “it” that follows is “12 April 2006” - the thing told.

I think Kai used:

tell "12 April 2006" to tell my date it to...

Instead of (simpler):

tell date "12 April 2006" to...

So everybody could compile the code (though not necessary run it without errors, as coercing a string to a date depends on local settings) → in my machine, for example, it would run OK changing “April” into “Abril”.

Ah I see. So “date it” coerces the previous value. Thanks!

A new version of Date Format is now available at http://www.rtlabs.com/downloads/ This provides very flexible options for programming date strings for any arbitrary dates. It uses the format codes used by the shell command “date”. It is said to work on OS versions 10.3.9, and I have found it works well on 10.4.10. It is version 1.1.

– ±--------±--------±--------±--------±--------±--------±--------±--------+
(*
Filename : very_simple_date.scpt
Author : Bill Hernandez
Location : Plano, Texas
Updated - Thursday, July 16, 2009 ( 8:51 PM )

Note : 

This is an example on how to get the date and time with one line

Hope this is helpful...

*)
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on run
set the_date to my get_date()
display dialog “Today is …” default answer the_date
—> 2009.07.16
end run
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on get_date()
return (do shell script “date +\[%Y.%m.%d\]\(%I:%M%p\)”)
end get_date
– ±--------±--------±--------±--------±--------±--------±--------±--------+

– ±--------±--------±--------±--------±--------±--------±--------±--------+
(*
Filename : simple_date.scpt
Author : Bill Hernandez
Location : Plano, Texas
Updated - Thursday, July 16, 2009 ( 8:51 PM )

Note : 

I decided to write a more useful demo using the date, time, error handler, etc.

This is an example on how to get the date and time with one line
and a simple way to use the date while writing to the error log. 

All my errors use something like : 

set this_error to "[4753] Error: " & error_number & ". " & error_message & return

The numbers start with an arbitrary 4 digit number and represent the exact location 
in the code where the error occurred. When you read the log, it pinpoints exactly 
where the error took place in the code. 

Every time an "on error" is used I increment the number in its brackets. In fact 
I have a script that reads the page and auto sequences them, once I provide 
the dialog a start number. That way I don't get error location number duplication.
In a large script it really comes in handy.

This error is not very helpful, since the error could be anywhere : 
"Error: -2753. The variable some_undefined_variable is not defined."

Once we add an error position [4xxx], we don't have to worry about line number, etc.
This is more useful : shows the error date, error location, error number, and error message

[2009.07.16](09:33PM) [4753] Error: -2753. The variable some_undefined_variable is not defined.
[2009.07.16](09:33PM) [4754] Error: -1708. «script» doesn't understand the some_undefined_function message.

Hope this is helpful...

*)
– ±--------±--------±--------±--------±--------±--------±--------±--------+
property divider : “--------------------------------------------------------------”
global path2me, parent_dirpath, this_date, path_delim, project_name, project_path, project_logname, log_dirpath, log_filepath, default_timeout
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on run
if (init_globals()) then
display dialog “Today is …” default answer this_date
—> 2009.07.16

	try
		set x to some_undefined_variable -- generate an error
	
	on error error_message number error_number
		set this_error to "[4753] Error: " & error_number & ". " & error_message & return
		my send2log(this_error)
	end try
	
	try
		set x to my some_undefined_function() -- generate another error
	
	on error error_message number error_number
		set this_error to "[4754] Error: " & error_number & ". " & error_message & return
		my send2log(this_error)
	end try
	
end if
my show_me("All Done...", "")
my open_log()

end run
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on get_date()
return (do shell script “date +\[%Y.%m.%d\]\(%I:%M%p\)”)
end get_date
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on send2log(message)
try
do shell script (("echo " & quote & this_date & quote & space & quoted form of message) & " >> " & log_filepath)
on error error_message number error_number
set this_error to quote & this_date & quote & space & "[4752] Error: " & error_number & ". " & error_message & return
tell application (path to frontmost application as text)
display dialog this_error
end tell
end try
end send2log
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on init_globals()
try
set this_date to my get_date()
set default_timeout to 3 – no of seconds for display dialog to give up
set project_name to “simple_date.scpt”
set project_logname to “simple_date.log”

	set path_delim to "/"
	set prefs_dirpath to (do shell script "echo ~/as_storage/as_prefs")
	set log_dirpath to (do shell script "echo ~/as_storage/as_logs")
	set log_filepath to log_dirpath & path_delim & project_logname
	
	set path2me to POSIX path of (path to me as string)
	set parent_dirpath to do shell script ("dirname " & quoted form of path2me)
	
	-- project_path could be different from path2me but for this demo, I will set them both the same
	set project_path to path2me
	my check_log()
	return true
on error
	return false
end try

end init_globals
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on check_log()
try
– check to see if the log file exists, if it doesn’t an error will be generated
– and we will create the log directories, and the log file
set f to alias (POSIX file (log_filepath))
on error error_message number error_number
set s to “”
set s to s & divider & return
set s to s & "project name : " & project_name & return
set s to s & "project date : " & this_date & return
set s to s & "project path : " & project_path & return
set s to s & "project log : " & log_filepath & return & divider
do shell script ("mkdir -p " & quoted form of prefs_dirpath)
do shell script ("mkdir -p " & quoted form of log_dirpath)
do shell script (("echo " & quoted form of s) & " > " & log_filepath)

	set this_error to "[4751] Error: " & error_number & ". " & error_message & return & return & "Creating a new log..." & return & return
	tell application (path to frontmost application as text)
		display dialog this_error
	end tell
	
end try

end check_log
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on open_log()
do shell script "open " & log_filepath
tell application “Console”
activate
end tell
end open_log
– ±--------±--------±--------±--------±--------±--------±--------±--------+
on show_me(msg, time_out)
if (time_out is “”) then
set time_out to default_timeout
end if
display dialog (msg) buttons {“OK”} default button {“OK”} giving up after time_out
end show_me
– ±--------±--------±--------±--------±--------±--------±--------±--------+