Saturday, September 23, 2017

#1 2016-10-14 03:18:22 pm

tneison
Member
Registered: 2015-04-03
Posts: 66

Six digit numeric date string creator

Previously I posted an error checking mechanism¹ because NSCalendar was occasionally returning garbage. Well, unfortunately it didn't fix the error.

So I've written a simpler and (hopefully) more reliable way to create a 6 digit numeric date string.

Applescript:

use framework "Foundation"
use scripting additions

sixDigitDateCode()

on sixDigitDateCode()
   set dateFormatter to current application's NSDateFormatter's alloc()'s init()
   set todaysDate to current application's NSDate's |date|()
   set codes to {"MM", "d", "Y"}
   set dateStr to ""
   repeat with code in codes
       (dateFormatter's setLocalizedDateFormatFromTemplate:code)
       set dateComponent to (dateFormatter's stringFromDate:todaysDate) as text
       if (count of characters of dateComponent) = 4 then
           set dateComponent to characters 3 thru 4 of dateComponent
       else if (count of characters of dateComponent) = 1 then
           set dateComponent to "0" & dateComponent
       end if
       set dateStr to dateStr & dateComponent
   end repeat
   return dateStr
end sixDigitDateCode

¹"Find item occurring most frequently in a list": http://macscripter.net/viewtopic.php?id=45186

Offline

 

#2 2016-10-14 03:31:48 pm

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11481
Website

Re: Six digit numeric date string creator

Easier

Applescript:

on sixDigitDateCode()
   set dateFormatter to current application's NSDateFormatter's alloc()'s init()
   set todaysDate to current application's NSDate's |date|()
   set codes to {"MM", "dd", "yy"}
   set dateStr to ""
   repeat with code in codes
       (dateFormatter's setLocalizedDateFormatFromTemplate:code)
       set dateComponent to (dateFormatter's stringFromDate:todaysDate) as text
       set dateStr to dateStr & dateComponent
   end repeat
   return dateStr
end sixDigitDateCode

Still easier

Applescript:

on sixDigitDateCode()
   set dateFormatter to current application's NSDateFormatter's alloc()'s init()
   set todaysDate to current application's NSDate's |date|()
   set dateFormatter's dateFormat to "MMddyy"
   return (dateFormatter's stringFromDate:todaysDate) as text
end sixDigitDateCode

lowercase y is crucial because uppercase Y represents year in "Week of Year" based calendars.

Last edited by StefanK (2016-10-14 03:34:03 pm)


regards

Stefan

Offline

 

#3 2016-10-14 04:24:34 pm

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4383

Re: Six digit numeric date string creator

And of course:

Applescript:

sixDigitDateCode()

on sixDigitDateCode()
   using terms from scripting additions
       tell (current date) to return text 2 thru -1 of ((1000000 + ((its month) * 10000) + (its day) * 100 + (its year) mod 100) as text)
   end using terms from
end sixDigitDateCode


NG

Offline

 

#4 2016-10-14 05:28:54 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5131

Re: Six digit numeric date string creator

tneison wrote:

because NSCalendar was occasionally returning garbage.


Can you explain how/where this was happening? It sounds an odd thing.

a 6 digit numeric date string.


I guess you need such a thing, but anyone with memories of the Y2K debacle will suggest such things are far better avoided.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#5 2016-10-14 06:48:16 pm

tneison
Member
Registered: 2015-04-03
Posts: 66

Re: Six digit numeric date string creator

Thanks for making this significantly simpler.

Can you explain how/where this was happening? It sounds an odd thing.


I was using NSCalendar to build a path to a folder containing the 6 digit numeric date string in it's name (i.e. "/volumes/path/to/server/folder/101416_assets"). Occasionally (once every 200th time used) it would create the path with an extra path component at the end (i.e. "/volumes/path/to/server/folder/1014/16_assets"). The error would always occur before the 2 digit year.

Y2K debacle


Not too worried about this. The folders eventually get put in subfolders with a four-digit year in the name.

Offline

 

#6 2016-10-14 06:56:23 pm

tneison
Member
Registered: 2015-04-03
Posts: 66

Re: Six digit numeric date string creator

Applescript:

set dateFormatter's dateFormat to "MMddyy"

btw I didn't know you could set class properties in this way in ASOC. Is this new?

Offline

 

#7 2016-10-14 07:00:08 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5131

Re: Six digit numeric date string creator

tneison wrote:

I was using NSCalendar to build a path to a folder containing the 6 digit numeric date string in it's name (i.e. "/volumes/path/to/server/folder/101416_assets"). Occasionally (once every 200th time used) it would create the path with an extra path component at the end (i.e. "/volumes/path/to/server/folder/1014/16_assets"). The error would always occur before the 2 digit year.


If you can be bothered, I'd love to see the code.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#8 2016-10-14 07:05:48 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5131

Re: Six digit numeric date string creator

tneison wrote:

I didn't know you could set class properties in this way in ASOC. Is this new?


It's not new -- but IMO it's not a particularly good idea. For one thing, it doesn't always work; for another, there can be subtle class issues, especially if you also use it to retrieve properties. It essentially uses key-value coding, which bypasses the scripting bridge's normal way of converting to and from AppleScript and Objective-C based on signatures and property definitions.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#9 2016-10-14 09:13:44 pm

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2709
Website

Re: Six digit numeric date string creator

Most easiest (when AppleScript Toolbox is installed)?

Applescript:

AST format date "MMddYY"

Offline

 

#10 2016-10-15 06:08:22 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 4383

Re: Six digit numeric date string creator

tneison wrote:

Applescript:

set dateFormatter's dateFormat to "MMddyy"

btw I didn't know you could set class properties in this way in ASOC. Is this new?


Shane Stanley wrote:

It's not new -- but IMO it's not a particularly good idea.


So would this be better, or equally undesirable?

Applescript:

dateFormatter's setDateFormat:("MMddyy")


NG

Offline

 

#11 2016-10-15 06:15:28 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11481
Website

Re: Six digit numeric date string creator

Shane Stanley wrote:

It's not new -- but IMO it's not a particularly good idea.


I apologize for too much translating one-to-one from ObjC wink


regards

Stefan

Offline

 

#12 2016-10-15 06:29:18 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5131

Re: Six digit numeric date string creator

Nigel Garvey wrote:

So would this be better, or equally undesirable?

Applescript:

dateFormatter's setDateFormat:("MMddyy")


That's what I'd use (maybe without the parentheses, but that's probably just a typing thing).


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#13 2016-10-15 05:23:18 pm

tneison
Member
Registered: 2015-04-03
Posts: 66

Re: Six digit numeric date string creator

This is the problematic NSCalendar version. Note: I haven't yet used the NSDateFormatter version enough to determine if I get the same error.

Applescript:

use framework "Foundation"
use scripting additions
set serverPath to "/Volumes/Path/To/Server/Folder/" & sixDigitDateStr() & "_assets"
on sixDigitDateStr()
   set todaysDate to current application's NSDate's |date|()
   set cal to current application's NSCalendar's currentCalendar()
   set calComponents to cal's components:254 fromDate:todaysDate
   set |month| to adjustComp(calComponents's |month|() as text)
   set |day| to adjustComp(calComponents's |day|() as text)
   set |year| to adjustComp(calComponents's |year|() as text)
   return |month| & |day| & |year|
end sixDigitDateStr
on adjustComp(dateComp)
   if length of dateComp = 1 then
       set dateComp to "0" & dateComp
   else if length of dateComp = 4 then
       set dateComp to characters 3 thru 4 of dateComp as text
   end if
   return dateComp
end adjustComp

Last edited by tneison (2016-10-15 05:23:59 pm)

Offline

 

#14 2016-10-15 08:47:41 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5131

Re: Six digit numeric date string creator

Are you sure the intermittent error you were getting wasn't a slash between the two year digits?

You have this line:

set dateComp to characters 3 thru 4 of dateComp as text


That makes a two-character list, and then coerces it to a string. What you end up with depends on the value of text item delimiters at the time; if you've set them to a slash somewhere else in your code, without resetting them to the default, you'd get a slash inserted between the digits.

The better way to extract a substring is:

Applescript:

set dateComp to text 3 thru 4 of dateComp

which is also more efficient, because there's no list created.


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/

Offline

 

#15 2016-10-16 12:03:31 am

tneison
Member
Registered: 2015-04-03
Posts: 66

Re: Six digit numeric date string creator

What you end up with depends on the value of text item delimiters at the time


That's almost certainly what was happening. Thanks.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)