I have a line of code that assembles a date and assigns it to a variable for use later in the script. The problem is that I’m getting carriage returns in the output that shouldn’t be there. I’ve tried various versions of the line such as adding “as text” at the end and enclosing the code after the variable in various types of brackets, but no dice. It produces the same result in the length script or as a one-liner in a separate applescript. Any ideas on a fix would be most welcome.
Here’s the line…
set myDate to weekday of (current date) & ", " & month of (current date) & " " & day of (current date) & ", " & year of (current date)
I want it to look like this, to be used in plain text: Tuesday, July 6, 2010
and here’s what the output looks like:
Here’s where the returns are (in TextWrnagler, they look like a horizontal hockey stick with a downward hook on the right end). Here they are designated as (CR) to make them visible:
set myDate to (weekday of (current date) & ", " & month of (current date) & " " & day of (current date) & ", " & year of (current date)) as text
I think your problem is that AppleScripts text item delimiters are set to return. I did circumvent the problem by coercing it to text in the first place I think this should have worked equally well.
set myDate to "" & weekday of (current date) & ", " & month of (current date) & " " & day of (current date) & ", " & year of (current date)
The problem was that the expression were returned as a list, since weekday of (current date) returns a constant - and a text string I believe can’t be coerced to a constant, then the whole expression will be coerced to a list, with
the following problem of your text item delimiters, which inserted returns between the items in the list.
Right, constants can’t be concatenated, so every expression is appended to the list
The easiest solution is to coerce the first expression to text, the other classes (constant, integer) will be coerced implicitly.
Calling current date 4 times is a bit expensive and not necessary
tell (current date) to set myDate to (its weekday as text) & ", " & its month & space & its day & ", " & its year
You can also use /bin/date to create custom formatted date strings
set myDate to do shell script "/bin/date +'%A, %B %d, %Y'"
Thank you all. As usual, the various approaches teach me how many ways there can be to skin a problem in AppleScript when something that seems to be an obvious (though clunky) method doesn’t work.
Tom X and StefanK, your approaches are both elegant and intriguing. It never would have occurred to me, for instance, to start off with a " " as part of the code line to trigger the coercion of things that followed, and working out a way to call (current date) once in a nicely worded statement using ‘its’ is just as interesting.
It returns a date formatted according to the system settings.
It’s not necessarily the wanted format.
On my French System for instance, it returns “jeudi 8 juillet 2010”
But I may wish to get “08/04/2010” or “07/08/2010” or “2010/07/08” .
The Original Poster used ", " as separator between its date components.
All these features may be sufficient to reject date string.
For my own uses I often call:
set the_date to (do shell script “date +_%Y-%m-%d”)
I don’t know if there is a way to insert other separators between the date components.
Yvan KOENIG (VALLAURIS, France) jeudi 8 juillet 2010 17:13:25
You could always use change text item delimiters .
” obtaining a date string and gets the wanted parts here..
set delims to text item delimiters
” you should of course use AppleScript's text item delimiters if this is within an app's tell block.
set text item dellimers to "/"
set dateString to every text item of someDateString
set text item delimiters to ":"
set dateString to "" & dateString
set text item delimiters to delims
” something like that.