Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Applescript: date formatting

1,298 views
Skip to first unread message

JF Mezei

unread,
Mar 27, 2016, 3:31:10 AM3/27/16
to

the following applescript:
write "date: " & aPhoto's date & return to outfile

produces:
date: Tuesday, 10July, 2012 at 13:22:10

Which is a useless date for more processing.

I found the following:
> https://developer.apple.com/library/mac/documentation/AppleScript/Conceptual/AppleScriptLangGuide/reference/ASLR_classes.html

I can get a numeric day of month and year. But month appears to be text
only, and the time is in seconds since midnight.

(times string is useless as it appears to use AM/PM instead of proper 24
hour values)


So, is it a case of having to write some complex subroutine to generate
a proper ISO date format with lots of string concatenation and
truncation (I suspect I would have to add 100 to all values and then
take the last 2 digits to get a leading 0 for values less than 10).

Or is there something really simple which I have not found ?

JF Mezei

unread,
Mar 27, 2016, 3:52:47 AM3/27/16
to
I've found the following on the web (not tested)

set theDate to (current date)
set theHour to fillInZero(hours of (theDate) as integer)
set theMinutes to fillInZero(minutes of (theDate) as integer)
tell theDate to get (it's month as integer)
set theMonth to fillInZero(result)
set theDay to fillInZero(day of theDate)


So, how would I know that I can specify
"minutes of (theDate) as integer"

when this was not listed in the apple web page I specified in previous
post, so where would it be documented ?


And why can I specify as a variable (argument to routine or value to be
assigned to variable)

minutes of (theDate) as integer

but I can't do that with month of (theDate( as integer ?

What exactly happens when I "tell theDate to get (it's month as integer) ?

Does the date class have methods to convert stuff, and if so, where are
those documented ?

When I use the simple "hours of (theDate( as integer", does this
involve any methods or just basic applescript language processing while
the conversion of the month involves a method avaualble for a date object?


John Stewart

unread,
Mar 27, 2016, 7:55:48 AM3/27/16
to

On 2016-03-27 07:52:43 +0000, JF Mezei said:


I've found the following on the web (not tested)


set theDate to (current date)

        set theHour to fillInZero(hours of (theDate) as integer)

        set theMinutes to fillInZero(minutes of (theDate) as integer)

        tell theDate to get (it's month as integer)

        set theMonth to fillInZero(result)

        set theDay to fillInZero(day of theDate)



So, how would I know that I can specify

"minutes of (theDate) as integer"


when this was not listed in the apple web page I specified in previous

post, so where would it be documented ?


In the Applescript Language Guide <http://apple.co/1E6eql1>


And why can I specify as a variable (argument to routine or value to be

assigned to variable)


minutes of (theDate) as integer


but I can't do that with  month of (theDate( as integer ?


You can

set theDate to (current date)

month of theDate as integer -- returns 3 for March


What exactly happens when I "tell theDate to get (it's month as integer) ?


Does the date class have methods to convert stuff, and if so, where are

those documented ?


In the Applescript Language Guide <http://apple.co/1E6eql1> such as they are.


When I use the simple  "hours of (theDate( as integer", does this

involve any methods or just basic applescript language processing while

the conversion of the month involves a method avaualble for a date object?


For myself I've found it much easier to use 'strftime' to retrieve 'dates' in a specific format i.e.


do shell script "date '+%m/%d/%Y'" will return "03/27/2016", see: unix man pages for "date" and "strftime"


the Satimage.osax contains a command that can duplicate the shell's function. It is "strftime" and uses the same formatting as the shell, see: unix man page "strftime".


strftime (current date) into "%m/%d/%Y" -- returns 03/27/2016

Siri Cruise

unread,
Mar 27, 2016, 8:01:14 AM3/27/16
to
In article <56f78c3b$0$29512$c3e8da3$c8b7...@news.astraweb.com>,
JF Mezei <jfmezei...@vaxination.ca> wrote:

> Or is there something really simple which I have not found ?

Up through System 9 I continued to throw myself against the concrete wall which
is known as AppleScript because I had no alternative. Now I do.

set somefile to POSIX file "/System/Library/Sounds/Basso.aiff"
set someinfofor to info for somefile
set unusefuldatetime to creation date of someinfofor
set usefuldatetime to do shell script "echo 'puts [clock format [clock scan {" &
unusefuldatetime & "}] -format {%Y %m %d %H %M}]' | tclsh"
-- Consult man strftime for the format string.

tell current application
info for file ":System:Library:Sounds:Basso.aiff"
--> {name:"Basso.aiff", creation date:date "Wednesday, May 25, 2011
10:21:40 AM", modification date:date "Thursday, February 9, 2012 6:29:56 PM",
size:14562, folder:false, alias:false, package folder:false, visible:true,
extension hidden:false, name extension:"aiff", displayed name:"Basso.aiff",
default application:alias ":Applications:iTunes.app:", kind:"AIFF-C audio", file
type:"file creator:"type identifier:"public.aifc-audio", locked:false, busy
status:false, short version:"", long version:""}
do shell script "echo 'puts [clock format [clock scan {Wednesday, May 25,
2011 10:21:40 AM}] -format {%Y %m %d %H %M}]' | tclsh"
--> "2011 05 25 10 21"
end tell
Result:
"2011 05 25 10 21"

--
:-<> Siri Seal of Disavowal #000-001. Disavowed. Denied. Deleted.
'I desire mercy, not sacrifice.'
If you assume the final scene is a dying delusion as Tom Cruise drowns below
the Louvre, then Edge of Tomorrow has a happy ending. Kill Tom repeat..

Jolly Roger

unread,
Mar 27, 2016, 12:34:54 PM3/27/16
to
On 2016-03-27, JF Mezei <jfmezei...@vaxination.ca> wrote:
>
> the following applescript:
> write "date: " & aPhoto's date & return to outfile
>
> produces:
> date: Tuesday, 10July, 2012 at 13:22:10
>
> Which is a useless date for more processing.
>
> So, is it a case of having to write some complex subroutine to generate
> a proper ISO date format with lots of string concatenation and
> truncation (I suspect I would have to add 100 to all values and then
> take the last 2 digits to get a leading 0 for values less than 10).
>
> Or is there something really simple which I have not found ?

[cross posted to alt.comp.lang.applescript]

-- begin script
return FormatDateTime(current date)

--------------------------------------------------------------------------------------------

on FormatDateTime(theDate)
set theDate to theDate as date
set D to characters -2 through -1 of ("0" & theDate's day) as
text
copy theDate to tempDate
set the month of tempDate to January
set M to characters -2 thru -1 of ("0" & 1 + (theDate - tempDate
+ 1314864) div 2629728) as text
set Y to characters -1 thru -4 of ((year of theDate) as text) as
text
set hh to "0" & theDate's hours as text
set hh to characters ((count of hh) - 1) through (count of hh)
of hh
set mm to "0" & theDate's minutes as text
set mm to characters ((count of mm) - 1) through (count of mm)
of mm
set ss to "0" & theDate's seconds as text
set ss to characters ((count of ss) - 1) through (count of ss)
of ss
return (Y & "-" & M & "-" & D & " @ " & hh & ":" & mm & ":" & ss
as text)
end FormatDateTime
-- end script

--
E-mail sent to this address may be devoured by my ravenous SPAM filter.
I often ignore posts from Google. Use a real news client instead.

JR
0 new messages