Command for Text Editing Areas Always Use Light Mode

Get help using and writing Nisus Writer Pro macros.
Post Reply
tedg
Posts: 29
Joined: 2012-09-22 01:52:56
Location: Brisbane, Australia
Contact:

Command for Text Editing Areas Always Use Light Mode

Post by tedg »

I would like a macro that toggles the "Text Editing Areas Always Use Light Mode" preference checkbox. I run a global dark mode and want to switch back and forth in the page view.
User since 1990
Most current NWp and MacOS
MacBookPro 16-inch 2021
User avatar
martin
Official Nisus Person
Posts: 5118
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Command for Text Editing Areas Always Use Light Mode

Post by martin »

There's no way the Nisus Writer macro language can help you with such a thing, sorry. But maybe AppleScript can interact with the dialog? You'd have the AppleScript open the Appearance preferences pane and "click" the checkbox– if that's possible. I'm not sure since I don't use AppleScript. Maybe someone else knows?
User avatar
martin
Official Nisus Person
Posts: 5118
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Command for Text Editing Areas Always Use Light Mode

Post by martin »

One other idea: assign a keyboard shortcut to the menu Nisus Writer Pro > Preferences > Appearance. If you have full keyboard control turned on in your macOS system preferences then toggling this option isn't too lengthy. The steps would be:

1. Activate your keyboard shortcut for the Appearance preferences.
2. Press the Tab key once or twice to move focus to the checkbox.
3. Press the Space key to toggle the checkbox.
4. Use Command + W to close the preferences window.

It's not a single action, but it's also not too onerous.
adryan
Posts: 490
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Command for Text Editing Areas Always Use Light Mode

Post by adryan »

G'day, Martin et al

I looked at this after the initial posting and tried doing it with an AppleScript that invoked a Do Menu Macro command to arrive at the relevant Preferences pane and then used System Events to tick the appropriate checkbox. (So pretty much along the lines Martin suggested.)

It worked BUT one really needs to reset the Appearance pane each time the script runs and I don't know how to do this. If it's not reset, the tab moves on to the next place instead of acting as a toggle.

Close, but not quite there, which is why I deferred posting.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Monterey 12.6
Nisus Writer user since 1996
User avatar
martin
Official Nisus Person
Posts: 5118
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Command for Text Editing Areas Always Use Light Mode

Post by martin »

adryan wrote: 2022-04-25 15:50:36 It worked BUT one really needs to reset the Appearance pane each time the script runs and I don't know how to do this. If it's not reset, the tab moves on to the next place instead of acting as a toggle.
That sounds like good progress to me! I found this post that discusses how AppleScript can inspect the focussed control of the frontmost app. The code:

Code: Select all

activate application "Nisus Writer Pro"
tell application "System Events"
	set theApplication to first application process whose frontmost is true
	set theControl to value of attribute "AXFocusedUIElement" of theApplication
	return properties of theControl
end tell
When I run that AppleScript while the Light Mode checkbox has focus the output is like this:

Code: Select all

{minimum value:missing value, orientation:missing value, position:{1061, 750}, class:checkbox, accessibility description:missing value, role description:"checkbox", focused:true, title:"Text editing areas always use Light Mode.", size:{278, 18}, help:missing value, entire contents:{}, enabled:true, maximum value:missing value, role:"AXCheckBox", value:0, subrole:missing value, selected:missing value, name:"Text editing areas always use Light Mode.", description:"checkbox"}
So it seems like AppleScript can detect whether the desired checkbox still has focus by looking at the title property. I don't know enough AppleScript to put it all together, but maybe you can...
adryan
Posts: 490
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Command for Text Editing Areas Always Use Light Mode

Post by adryan »

G'day, Martin et al

Thanks for looking into this, Martin. I played with your code fragment but, although I could alter it to toggle the checkbox once the focus was on it, I couldn't see how to enforce focus on it in the first place without things getting messy.

However, I have discovered that it's possible to force a reset of the focus of a Preferences pane to the topmost level by changing to a different pane and closing the Preferences window entirely. If you don't close the window, the focus remains unchanged when you revisit the original pane of interest.

Here's an AppleScript script that seems to do what we want. I incorporated a delay in order to ensure we allow time for the main task to be accomplished before the window closes. It could probably be shorter, but the actual value would depend on the computer in use.

Code: Select all

tell application "Nisus Writer Pro"
	activate
	Do Menu Macro with macro "Preferences:Appearance"
	tell application "System Events"
		keystroke tab
		keystroke space
	end tell
	delay 1
	close window 1
	Do Menu Macro with macro "Preferences:General"
	close window 1
end tell
Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Monterey 12.6
Nisus Writer user since 1996
adryan
Posts: 490
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Command for Text Editing Areas Always Use Light Mode

Post by adryan »

G'day, all

For those who aren't familiar with AppleScript, I thought a few words about handling the code in my previous posting might be in order. There's nothing scary about it.

Launch the Script Editor application (in either the Utilities folder or the AppleScripts folder inside your Applications folder, depending on your operating system) and paste the code into a new document. Save it on the Desktop with File Format "Script" (so it has a ".scpt" file extension) and a nice name (eg, "Toggle Dark Mode for Text.scpt").

Before you leave Script Editor, go to Preferences… > General > Script Menu.

Tick the two checkboxes there and choose the "at bottom" radio button. You can then quit Script Editor.

One possibility now is to move your script to the following location:–

~Users > your_username > Library > Scripts > Applications > Nisus Writer Pro

(Note that the operating system contains three Libraries. The one you want is in the above path.)

You should see an icon resembling a scrolled manuscript in the main menu bar at the top of the screen. Whenever Nisus Writer Pro is the active application, any scripts you have in that "Nisus Writer Pro" folder in the Library will be listed at the bottom of the drop-down menu that appears when you click on the scrolled manuscript icon. Choose a script (eg, "Toggle Dark Mode for Text") and the script will execute the commands incorporated in it.

Another possibility is to place your script in Nisus Writer Pro's Macros folder which you can open by invoking the "Show Macros Folder in Finder" command in the Macro menu. The script will then function like any other Macro. If you like, you can go the extra mile and add it to your NWP Toolbar.

Or you could place copies of the script in both your Library and your NWP Macros folder.

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Monterey 12.6
Nisus Writer user since 1996
tedg
Posts: 29
Joined: 2012-09-22 01:52:56
Location: Brisbane, Australia
Contact:

Re: Command for Text Editing Areas Always Use Light Mode

Post by tedg »

Thanks for this. I think if there is no clean way to do it without the arcade flashing, I may as well turn to Keyboard Maestro.

Many thanks, Ted
User since 1990
Most current NWp and MacOS
MacBookPro 16-inch 2021
Vanceone
Posts: 197
Joined: 2013-05-03 07:06:31

Re: Command for Text Editing Areas Always Use Light Mode

Post by Vanceone »

I think that there is a solution to this.

Applescript has a way of sending arbitrary Cocoa commands using CocoaScripting. The basic process would be to grab the current Nisus Preference object and set the appropriate preferences on it, which can be done.

The downside, of course, is knowing what Nisus has actually called their Preference's class and the methods on that class internally. Presumably Nisus will keep that a secret. But it would be possible.
adryan
Posts: 490
Joined: 2014-02-08 12:57:03
Location: Australia

Re: Command for Text Editing Areas Always Use Light Mode

Post by adryan »

G’day, Vanceone et al

Yes, direct AppleScript access to the required Preference command is really what is required.

Now that you've raised this topic again, I've had another, out-of-left-field thought. (And we all know that left field is situated somewhere in the vast Kingdom of Kludge.)

Ted doesn't like the "arcade flashing", by which (I presume) he means the transient display of the Preferences window. This is due to the way Nisus implements its Do Menu Macro command because it "activates" the menu path which in the present case causes the Preferences window to open and come to the front.

It occurs to me that, if the Nisus document window were a floating window, it would remain frontmost. Then, as long as the Preferences window remained within the bounds of the document window (which is easy enough to arrange), it should remain hidden from the user while its settings were being manipulated by the script.

So it might be possible to incorporate Terminal commands at the beginning and the end of the script to turn flotation of the document window on and off respectively.

The funny thing is, I usually abhor floating windows, but I may actually have found a use for one!

Cheers,
Adrian
MacBook Pro (M1 Pro, 2021)
macOS Monterey 12.6
Nisus Writer user since 1996
User avatar
martin
Official Nisus Person
Posts: 5118
Joined: 2002-07-11 17:14:10
Location: San Diego, CA
Contact:

Re: Command for Text Editing Areas Always Use Light Mode

Post by martin »

Vanceone wrote: 2022-06-28 16:11:20 Applescript has a way of sending arbitrary Cocoa commands using CocoaScripting. The basic process would be to grab the current Nisus Preference object and set the appropriate preferences on it, which can be done.

The downside, of course, is knowing what Nisus has actually called their Preference's class and the methods on that class internally. Presumably Nisus will keep that a secret. But it would be possible.
I'm not very well informed on AppleScript, but I don't think it can manipulate arbitrary objects in apps. Maybe AppleScript can manipulate data held by the standard NSUserPreferences object, but this won't help with "Always Use Light Mode". Nisus Writer keeps the data for that setting elsewhere, in a custom object, because Nisus Writer allows storing these preferences anywhere on disk, i.e. the user's chosen synchronized settings location.

Maybe we can grant the Nisus macro language access to these potentially cloud sync'd preferences, like we do with the "Read User Default" macro command. The problem with any hypothetical "Write" commands is that they have the potential to introduce dirty data to the system, e.g. a macro trying to stuff a string value somewhere that expects a number. But maybe it's enough to introduce such commands and give macro authors enough rope to hang themselves ;)
Post Reply