Wednesday, January 19, 2022

#1 2021-12-02 04:26:57 am

hubert0
Member
From:: East Anglia
Registered: 2020-11-20
Posts: 18

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

Applescript:

Monday * 1

in Script Editor the result is 2.

And if I run

Applescript:

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

Applescript:

Monday * 1 - 1

Thanks,

H


MacOS 11.0.1

Offline

 

#2 2021-12-02 04:50:29 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: First day of the week

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:

Applescript:


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

Last edited by KniazidisR (2021-12-02 04:52:32 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#3 2021-12-02 05:26:58 am

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

Re: First day of the week

hubert0 wrote:

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


Hi.

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.


NG

Offline

 

#4 2021-12-02 06:01:48 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: First day of the week

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.

Applescript:


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

Last edited by KniazidisR (2021-12-02 06:13:25 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#5 2021-12-02 06:03:46 am

db123
Member
Registered: 2020-12-07
Posts: 60

Re: First day of the week

More out of curiosity than any pressing need...

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:

Applescript:


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
end BWAND

Last edited by db123 (2021-12-02 06:14:56 am)

Offline

 

#6 2021-12-02 06:31:59 am

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

Re: First day of the week

Applescript:

set weekStartDay to Monday

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


NG

Offline

 

#7 2021-12-02 07:06:47 am

db123
Member
Registered: 2020-12-07
Posts: 60

Re: First day of the week

@Nigel : I knew it should be easier. wink

Offline

 

#8 2021-12-02 07:54:33 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: First day of the week

Nigel Garvey wrote:

Applescript:

set weekStartDay to Monday

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


@Nigel,

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

Applescript:


try
   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.

Last edited by KniazidisR (2021-12-02 10:25:07 am)


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#9 2021-12-02 08:46:20 am

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

Re: First day of the week

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?  hmm


NG

Offline

 

#10 2021-12-02 10:22:36 am

hubert0
Member
From:: East Anglia
Registered: 2020-11-20
Posts: 18

Re: First day of the week

Thanks all, good to know.


MacOS 11.0.1

Offline

 

#11 2021-12-02 10:30:30 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 2208

Re: First day of the week

Nigel Garvey wrote:

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?  hmm


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.


Model: MacBook Pro
OS X: Catalina 10.15.7
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#12 2021-12-02 03:48:07 pm

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

Re: First day of the week

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.

Applescript:

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.
else
   -- 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


NG

Offline

 

#13 2021-12-02 05:15:00 pm

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

Re: First day of the week

Alternatively:

Applescript:

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


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

Offline

 

#14 2021-12-03 02:24:19 am

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

Re: First day of the week

Shane Stanley wrote:

Alternatively:


cool


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)