First day of the week

More out of curiosity than any pressing need…

In System Prefs - Language & Region - Advanced I have the first day of the week set to Monday.

However, when I run

Monday * 1

in Script Editor the result is 2.

And if I run

Sunday * 1

the result is 1.

It appears that AppleScript (or Script Editor) is ignoring the system preference.

Is there a better way of specifying that Monday is the day 1 of the week than

Monday * 1 - 1



What you are doing has nothing to do with a date type variable. And Monday of the week is exactly the date, not some integer. Here’s how I learned to get Monday of the current week from our site’s moderator @Nigel Garvey:

tell (current date) to set theMonday to it - ((its weekday) - 2) * days


AppleScript’s weekday and month enums are simple tokens with fixed integer and (English) text equivalents. They’re not influenced by users’ start-of-week or language settings.

date objects, on the other hand, are more complex beasts (a subclass of record) with associated “machinery” which can translate between date objects and text, taking the user’s preferences into account. However, the week start day isn’t one of the preferences considered (by vanilla AppleScript, anyway). Scripters have to code round this themselves if it’s relevant to a script.

I guess that to get a strange Monday (which is actually Sunday), for example, you just need to set your custom Monday (the myStrangeMonday variable) by changing the offset -2 to -1. I am sure that the required offset (as AppleScript weekday constant Sunday) can be read from the corresponding .plist settings.

set firstWeekDay to Sunday as integer
--> 1
tell (current date) to set myStrangeMonday to it - ((its weekday) - firstWeekDay) * days
--> date "Sunday, November 28, 2021 at 1:58:53 PM"

This offset (1 in this case) as you see, is the result of Sunday as integer

This is quite relevant in practice. I recently had the problem to find out if a focus setting is valid for the current date/time or not. Apple itself stores the info bitwise and also uses the 1 for Monday.
That’s how I made it work:

use scripting additions
use framework "Foundation"

property ca : a reference to current application

-- test date (Monday)
set theDay to 29
set theMonth to 11
set theYear to 2021

set checkValue to 7 -- value from the settings (Mo, Tu, We)

set currentDate to current date
tell currentDate to set {day, year, its month, day} to {1, theYear, theMonth, theDay} 
set theWeekDay to ca's NSCalendar's currentCalendar's ordinalityOfUnit:(ca's NSWeekdayCalendarUnit) inUnit:(ca's NSWeekCalendarUnit) forDate:(currentDate) --> 1

return BWAND(2 ^ (theWeekDay - 1), checkValue) ≠ 0

on BWAND(__int1, __int2)
	set theResult to 0
	repeat with bitOffset from 30 to 0 by -1
		if __int1 div (2 ^ bitOffset) = 1 and __int2 div (2 ^ bitOffset) = 1 then
			set theResult to theResult + 2 ^ bitOffset
		end if
		set __int1 to __int1 mod (2 ^ bitOffset)
		set __int2 to __int2 mod (2 ^ bitOffset)
	end repeat
	return theResult as integer

set weekStartDay to Monday

set today to (current date)
set weekdayNumber to ((today's weekday) - weekStartDay + 7) mod 7 + 1

@Nigel : I knew it should be easier. :wink:


Exists simple way to automatically read the First Weekday setting of the user from Global Settings plist file, as I sad above:

	do shell script "defaults read .GlobalPreferences AppleFirstWeekday"
	set userWeekStart to (character 19 of result as integer) -->  for not Monday
on error
	set userWeekStart to 2 --> that is, Monday
end try

set today to (current date)
set weekdayNumber to ((today's weekday) - userWeekStart + 7) mod 7 + 1

NOTE: the error occur, because for default Monday setting the Global Settings plist file doesn’t contain AppleFirstWeekday key at all.

Thanks, KniazidisR. That’s nice.

But on my Mojave machine, it’s Monday that’s the default, not Sunday. I wonder if there are other factors involved, such as OS version or region? :confused:

Thanks all, good to know.

No, that’s my typo. For me (on Catalina) as well as for you, Monday is the default. I have updated the script to fix the typo.

Thanks, KniazidisR.

Here for the hell of it is the same thing, but using NSUserDefaults in ASObjC. I like it because it doesn’t involve picking an indexed character out of a returned text and is thus (theoretically) more reliable.

use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
use scripting additions

set AFWDict to (current application's class "NSUserDefaults"'s alloc()'s initWithSuiteName:(".GlobalPreferences"))'s dictionaryForKey:("AppleFirstWeekday")
if (AFWDict is missing value) then
	set weekStartDay to 2 -- Entry missing. Default = Monday.
	-- Assuming the Gregorian calendar.
	set weekStartDay to (AFWDict as record)'s gregorian
	-- Not so assuming.
	-- set weekStartDay to AFWDict's allValues()'s firstObject() as integer
	-- Or:
	-- set weekStartDay to (AFWDict as record as list)'s beginning
end if

set today to (current date)
set weekdayNumber to ((today's weekday) - weekStartDay + 7) mod 7 + 1


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions

set weekStartDay to current application's NSCalendar's currentCalendar()'s firstWeekday()
set today to (current date)
set weekdayNumber to ((today's weekday) - weekStartDay + 7) mod 7 + 1