Command for Text Editing Areas Always Use Light Mode

Get help using and writing Nisus Writer Pro macros.
Post Reply
tedg
Posts: 28
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: 5078
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: 5078
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: 474
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.0.1
Nisus Writer user since 1996
User avatar
martin
Official Nisus Person
Posts: 5078
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: 474
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.0.1
Nisus Writer user since 1996
adryan
Posts: 474
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.0.1
Nisus Writer user since 1996
tedg
Posts: 28
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
Post Reply