Episode 3 has me inspired to finally learn AppleScript, after years of thinking about it. I already know JS as well as a few other languages, so I could just do it in JS but I want to learn AppleScript. I have been having trouble doing something that I assumed would be extremely trivial, casting a date type to a string. Currently I am able to get the date but when I try to either add it to a string or add to body text I get the error that it “cannot make date into type string.” Whats the easiest way to cast to a string, I haven’t found a solution that is not as simple as I would expect (especially since AppleScript is supposed to be an extremely simple language), mostly people who have written separate classes. Does anyone know an easy way to cast a date to string?
Try adding as text
after a date variable to cast it as a string.
Yes, I have tried this and unfortunately it returns the same error–Cannot convert date type into text.
Hmmmm. Try directly with as string
. Any difference?
Sorry I’m on the go at the moment so working off the top of my head
These examples worked well for me in Script Debugger
set d to current date
-- d is date type
set t to d as text
-- t is text type converted from date d
set t1 to "August 13, 2018"
-- t1 is text type
set d1 to date t1
-- d1 is the date type converted from text
Thanks but I’m pretty sure this does the opposite, converts a String to date type.
unfortunately that doesn’t work either. Thanks though
They have shown two conversions there.
One from date to text and one from text to date to just round it out.
Oh that makes more sense. (I was wondering why there were 4 variables.)
Unfortunately though this still didn’t work.
I have attached my code below incase I am doing anything incorrectly.
`tell application "Pages"
activate
set thisDocument to ¬
make new document with properties {document template:template "Blank"}
tell thisDocument
set {year:y, month:m, day:d} to (current date)
{month, day, year} of (current date) --> {2006, April, 15}
-- replace body text
set thisText to "Name"
datestring of (current date) as string
set wholebody to thisText & datestring
set body text to date string
set d to current date
-- d is date type
set t to d as text
-- t is text type converted from date d
set wholebody to t
tell body text
-- set body tet properties
set size to 12
end tell
end tell
end tell`
Look at using as text.
Also can you see where you have used “date string” rather than the “datestring” that you used previously?
I am not trying to display a dialog box but instead add it to a document. I am not extremely familiar with AppleScript so would I use what is shown in the image but substitute “display dialog”?
And changing date string to datestring did not fix it
display dialog was just a way to show it working displaying a sttring. Think of it like an alert() in JavaScript and how you might use that for deomnstarting or debugging - console aside of course.
Note that if you have all of the code above, the points I highlighted would atill stand and it could be those causing your issues.
I don’t use Pages and so don’t have the AppleScript dictionary for it, but I think something like this might be give you an example where a string and a date are set as the body of a new Pages document.
tell application "Pages"
activate
set thisDoc to make new document with properties {document template: template "Blank"}
tell thisDoc
set body text to (">> " & (current date) as text)
end tell
end tell
If it works (it may not as I can’t test it), you can then build out your bits of formatting, etc. If not we figure out the issue
Thank You. It works!
Also thanks for teaching me about “display dialog,” I had been trying to figure out what that was used for.