Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

hopi

macrumors newbie
Original poster
Apr 26, 2010
7
0
Hi,
I am looking for an applescript which provides the actual week number from the a date. I found several VB and Javascript examples in the web but could find any applescript.

There might be a solution to translate following JavaScript to Applescript but I am not that experienced enough to do it. I tried but failed ;-(
Anyone who knows an Applescript or could change the following Javascript to Applescript?
Thanks for any help.

// This script is released to the public domain and may be used, modified and
// distributed without restrictions. Attribution not necessary but appreciated.
// Source: https://weeknumber.net/how-to/javascript
// Returns the ISO week of the date.

Date.prototype.getWeek = function() {
var date = new Date(this.getTime());
date.setHours(0, 0, 0, 0);
// Thursday in current week decides the year.
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
// January 4 is always in week 1.
var week1 = new Date(date.getFullYear(), 0, 4);
// Adjust to Thursday in week 1 and count number of weeks from date to week1.
return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
- 3 + (week1.getDay() + 6) % 7) / 7);
}

// Returns the four-digit year corresponding to the ISO week of the date.

Date.prototype.getWeekYear = function() {
var date = new Date(this.getTime());
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
return date.getFullYear();
}
 
The AppleScript date class has limited accessible components.
I've listed some of them here.

AppleScript:
set theDate to current date
set theDay to day of theDate
set theWeekday to weekday of theDate
set theMonth to month of theDate
set theYear to year of theDate
set theTime to time of theDate
set theDateString to date string of theDate
set theTimeString to time string of theDate

return theWeekday


But as you can see, the AppleScript date class components does not have a "week" component.
So your better off using the NSDate and NSCalendar Class's from the Foundation Framework like this.

AppleScript:
use scripting additions
use framework "Foundation"

set theNSDate to current application's NSDate's |date|()
set theNSCalendar to current application's NSCalendar's currentCalendar()
set theWeekInteger to (theNSCalendar's component:(current application's NSCalendarUnitWeekOfYear) fromDate:theNSDate) as integer

return theWeekInteger

https://developer.apple.com/documentation/foundation/nsdate?language=objc
https://developer.apple.com/documentation/foundation/nscalendar?language=objc


Unfortunately working between AppleScript's date class and the NSDate class is not very intuitive.
But you can convert a NSDate class to an AppleScript date like this.

AppleScript:
set theNSDate to current application's NSDate's |date|()
set theASDate to theNSDate as date

return theASDate


And convert an AppleScript Date to an NSDate like this.

AppleScript:
set theASDate to current date
set theNSDate to current application's NSDate's dateWithTimeInterval:0 sinceDate:theASDate

return theNSDate


What are you trying to do, and where are you trying to do it.
 
Last edited:
Many thanks Mark FX for your answer. I will experiment with the NSDate class.

What I am trying to do is to organize certain emails with their attachments. I use defined rules in AppleScript with Mailtags.
Every time an email arrives I check the content of the email title for key words and save the attachment to folders which preferable are structured by week of the year (CW).
Examples:
Mail 1 with title "[AAA] MoM of topic A" and attachment file_1 shall be saved in directory /AAA/CWxx/file_1
Mail 2 with title "[BBB] MoM of topic B" and attachment file_2, file_3 shall be saved in directory /BBB/CWxx/file_2, /BBB/CWxx/file_3

I get about 60 emails per day with attachments and I wanted to use Applescript to create the specific CW folder when it does not exist or just move the files to an existing CW folder. I wanted to use the date of the email reception to define the CW.
 
If your trying to automate the "Mail" app, then AppleScript is certainly useful for that, although I haven't done it myself.
But if your more familiar with Javascript, then as of Mac OS 10.10, then you can also use JavaScript in the Script Editor app as well.

If your comfortable working with the AppleScript date class, then in summary of what I posted above, you could create an AppleScript function for returning the dates week number, like this below.

AppleScript:
use scripting additions
use framework "Foundation"

on getWeekFromDate(theDate)
    try
        set theASDate to theDate as date
        set theNSDate to current application's NSDate's dateWithTimeInterval:0 sinceDate:theASDate
        set theNSCalendar to current application's NSCalendar's currentCalendar()
        set theWeekInteger to (theNSCalendar's component:(current application's NSCalendarUnitWeekOfYear) fromDate:theNSDate) as integer
        return theWeekInteger
    on error
        return -1 as integer
    end try
end getWeekFromDate


And you would call the function from your wider AppleScript like this.

AppleScript:
set theDate to current date
set theDateWeek to getWeekFromDate(theDate) as integer


I hope that's more helpful.
 
  • Like
Reactions: hopi
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.