You are not logged in.
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
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 02:34:03 pm)
regards
Stefan
Offline
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
Online
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/
latenightsw.com
Offline
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
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
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/
latenightsw.com
Offline
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/
latenightsw.com
Offline
Most easiest (when AppleScript Toolbox is installed)?
Applescript:
AST format date "MMddYY"
Offline
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?
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
Online
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/
latenightsw.com
Offline
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 04:23:59 pm)
Offline
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/
latenightsw.com
Offline
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