help on applescript creating new folders, applescript bug?

i’m trying to create a new folder using finder with the current date as its name e.g. June 25, 2008, the code i used is as follows:

set DateSTR to date string of (current date)
set Pos to offset of " " in DateSTR
set theDate to characters (Pos + 1) through end of DateSTR as string

try
tell application “Finder” to make new folder at “storage:” with properties {name:theDate}
end try

the question is, why is the name of the folder it creates named j=u=n=e==2=5=,==2=0=0=8 ? is this a bug?
i used a string replace function to remove the “=”. the result is finally right if you run it once, however, if you run it again, the folder name is then replaced with j,u,n,e,2,5,2,0,0,8.

shouldn’t it disregard making a new folder if you run it a second time and a folder with the same name is available?

has anyone encountered this?

The problem you are having is due to not initializing the value of text item delimiters before you implicitly use it.

The code characters x through y of someStr as string is evaluated in two stages. First, characters x through y of someStr evaluates to a list of strings (single character strings). Second, that list of string is coerced to a single string value (. as string) using the current value of text item delimiters.

The first time you tried it, text item delimiters must have been “=”. The next time you tried it, it was “,”. That would explain all the occurances of those characters in your output.

If you want to fix it, add set text item delimiters to {“”} before the line that uses . as string. You should never use a list-to-string coercion (or a string-to-list conversion via text items) unless you have first initialized text item delimiters. If the reference to text item delimiters is in a tell block, you will need to write it as set AppleScript’s text item delimiters to . to avoid terminology and scoping conflicts.

But really, there is a simpler way that does not involve text item delimiters at all. Just use text x through y of someStr.

set DateSTR to date string of (current date)
set Pos to offset of " " in DateSTR
set theDate to text (Pos + 1) through end of DateSTR

Or, if you want to create folders with names that do not depending on the localization settings of the current system (my system produces date strings with the format “[Weekday], [year] [month name] [day]”), construct the string yourself:

tell (current date) to set theDate to "" & its month & " " & its day & ", " & its year -- "June 24, 2008"
(* 
 * OR
 *)
to zeroPad(s)
	text -2 through end of ("00" & s)
end zeroPad
-- .
set theDateObj to current date
set theDate to "" & (year of theDateObj) & "-" & zeroPad(month of theDateObj as number) & "-" & zeroPad(day of theDateObj) -- "2008-06-24"
-- will need my zeroPad(.) if it is inside a tell block.

Model: iBook G4 933
AppleScript: 1.10.7
Browser: Safari Version 3.1.1 (4525.18)
Operating System: Mac OS X (10.4)