Applescripts cannot seem to target the Preferences Pane (Ventura) - applescript

I'm trying to use Automator to record an action on the system preferences, in the accessibility display pane. I want to change the value of the slider for the color filter intensity. (There are many similar questions asked here on SO, but they seem to be from 4-11 yr ago. I think the issue may be changes with Ventura, etc.)
I use automator to Watch Me Do and record the action. I reset the preferences manually. Clicking run does not cause the setting to change in preferences.
So I drag the actions from the Watch Me Do panel to the workflow timeline, which pop-out the action into an AppleScript. Running this is also not successful.
Can I manually set this preference in some way via the cli? When I search Apple developer docs, all I'm finding is Swift code to write an application. ((I realize that the app that will do this change, Automator, Terminal, or otherwise, will need to be granted accessibility permissions under privacy/security system preferences.))
Or how do I get/write an AppleScript to do this?
Also, I have checked the OSAX dictionary for System Preferences, and it does not have more specific controls registered besides at the basic window/pane/etc level

Related

AppleScript Quick Action Services Shortcut issue - MacOS Catalina

Running on: MacOS Catalina 10.15.5
I made a Quick Action in Automator and it works just fine. In fact, I've been using it for a few years. The code in the action itself is not the problem, nor is it allowing accessibility/controls in system preferences. My problem is mind-boggling to say the least and I cannot seem to figure it out and thought I'd ask the hive mind.
After making my quick action with AppleScript and making sure the 'workflow receives: no input in any application' as I normal would do, then going in and assigning the shortcut the key 'F13' in System Preferences --> Keyboard --> Shortcuts --> Services, everything seems just great.
In the application I use this shortcut in, if I hover over the application main dropdown and hover over "Services" the item is there and has a shortcut 'F13' associated with it. If I select the shortcut from the dropdown menu, it works PERFECTLY.
Here is the problem, if I hit 'F13' on my keyboard, the service runs 3 times in a row. It's almost as if the system services thinks I want to run that particular item 3x. I have tried a number of different shortcut keys as well (as simple as: F13 and as complicated as: ⌘⌥⌃⇧N) and the shortcut keys did not yield any positive results either.
I have checked everywhere and cannot seem to find why this is happening. Any insight/help would be much appreciated. This is not an AppleScripting issue. It's something with the OS that is causing a triple selection on the services menu item for some reason. It does not seem to be a Key Repeater issue either.
Thanks in advance!
The problem, as best as I can tell, isn't that the workflow is running three times, but that you're getting a series of three security error messages. It seems that when you try to run a service using a keyboard shortcut, the system invokes an xpc service (com.automator.runner.xpc, which is located inside the automator application package), and that service is triggering security alerts because it doesn't have the accessibility rights that are needed for GUI scripting. There is no obvious way to grant the xpc the proper rights (and I suspect that would be an enormous security hole anyway), so that's not workable.
However, there's a workaround. Take your functioning script, copy it into a new Script Editor window, and save it as an application using the 'File Format' pulldown at the lower left of the save window:
Give this application permission to use accessibility features:
System Preferences → Security & Privacy → Privacy → Accessibility
Now create a service in automator that uses the Launch Application action to launch the script application you just created. Assign your keyboard shortcut to that service, and things should work as you like.
Of course, this launches an application, showing it in the Dock and grabbing the foreground momentarily, which can be visually distracting. If you want it to run silently in the background, do the following:
Right-click on the icon for the script app and choose Show Package Contents.
Drill down two levels to find the info.plist file, and open it in TextEdit, BBEdit or some other plain-text editor
Add the following key/value pair to the file (being carful not to break up other key/value groups):
<key>LSUIElement</key>
<true/>
Save the info.plist file, then run the app again; it should be faceless.

How to add Finder/Services menu items to own macOS contextual menu

When a file is right-clicked in the macOS Finder, a contextual menu like this shows up. It contains items at the bottom from macOS Services. I'm talking about services from third party such as (in this case) iTerm and Find Any File. How would I go about adding such items to a contextual menu in my own macOS app? Is there a documented API to do this?
I know this can be done, because it's implemented in at least one other application, see for example the following contextual menu in Find Any File:
macOS provides App Extensions for products to integrate with the OS. The programming guide lists the different extension points that can be utilised, along with a description of their purpose.
Based on the example you've provided, it may be the Finder Sync extension that you're looking for, as the documentation states for Finder Sync:
Badge local OS X folders to let users know the status of items that are remotely synced. You can also implement contextual menus to let users directly manage their synced content.

Programmatically changing system settings or linking to them?

I want the user to be able to change the Notification Center system settings directly from my app without having to go to System Preferences app. I don't know if this can be done at all so I'm willing to settle for having them click a button that directly links to the Notifications pane of System Settings. That way they can be toggling some preferences from my native preferences window, and just click a button to quickly change whatever in the SS notifications and be done with it.
Preference panes are standalone executables. When you want to open a particular one, just launch it:
[[NSWorkspace sharedWorkspace]
openFile:#"/System/Library/PreferencePanes/Notifications.prefPane"];
Obviously you might want to check if the file is there, which might not be the case on older OS X versions.

User settings' persistence in NPAPI?

I was wondering, if there's anything in NPAPI, which supports reading/writing user settings? Currently I use XPCOM, but I want to get rid of this code in order to support other browsers than Firefox.
I understand I can just read/write the file system (or registry on Windows) directly, but I want to explore other options - allowing the user edit these settings from the browser (e.g. about:config in FF) is, arguably, a better user experience than to direct him to edit some custom-made config files.
On the Mac plugins that need preferences sometimes ship a system preference pane with the plugin (and have a context menu item to open that pref pane), so that users have a UI for editing prefs. If there's not something similar for Windows and Linux you could always ship a small app that edits the prefs.
There is no such functionality in NPAPI.

How to access / embed Finder functionality?

Is it possible to embed Finder functionality in a cocoa app, now that Finder is itself cocoa (assuming the app were to function only in snow leopard)?
What I mean is to have a file browser pane as part of the app, actually browsing the file system itself (to edit in another pane), but without writing all the functionality of the Finder. Thanks!
The Finder itself is just an application. It is not a components library nor a framework. While you cannot "embed" Finder functionality in your application, you can influence Finder functionality and invoke Finder functionality.
First off, you can attach Folder Actions to folders. These will trigger when a user does something to the contents of a folder - for instance, they drop a file into it. You set this up in the Finder. You should to learn a little AppleScript, if interacting with the Finder is something you want to do.
Second, since the Finder supports AppleEvents, you can affect the Finder using AppleScript. Take a look at My First AppleScript and My First AppleScript Part II to see how to do this. Here is much more in-depth information, in AppleScript Overview: Scripting with AppleScript. Here is some More Finder Scripting.
Third, there is also support for developing ways for the Finder to do complex things for the user at the click of a button, using Automator (Mac OS X 10.5). You can also create a Service in Automator, beginning in Mac OS X 10.6 ("Snow Leopard"). Take a look at Automator and Finder Actions in Mac OS X 10.6 for an introduction to this latter technique.
Even though Finder windows themselves are not an embeddable component, if you really want to provide the ability to pen, Print, Delete, Duplicate, etc. Files/Folders, and navigate from Folder to Folder, you can develop a simple Folder browser in your application.
It should not be a huge amount of work to do this so long as you do not set your sites on mimicking the finder or duplicating all of its functionality, just the essential basics I have mentioned.
You would need to know how to program the Macintosh, however - not just use AppleScript. The normal way to do this would be to learn the Objective-C programming language and the Cocoa framework. You would need to get familiar with writing applications using a Model-View-Controller architecture.
You would create a subclass of NSObject named something like MyFile, and a subclass of a collection class named something like MyFolder. When the application creates the browsing Windows, and each time the application activates (becomes the frontmost application), you8 would refresh the contents of the browsing menu.
You could put a menu in your menu bar with commands in it: Open, Print, Delete, Duplicate. When the user does one of those commands, your application performs the appropriate actions itself or sends the request to the Finder. After the action has been completely carried out, then you refresh the browsing window for currently displayed Folder, or newly displayed Folder if the user navigated to a different Folder.
If you are familiar with design patterns, object-oriented programming, and frameworks in general - reading up on Cocoa Design Patterns will speed your learning curve immensely.
These are various techniques you can use to harness some of the power of the Finder. As you look these over, I suggest getting very clear in your mind just what benefit this brings to the user of your application. Writing down what the overall objective of this feature is, and what commands you wish to support, will make it easier to choose the path you take in developing it.
The user can always click on a Finder folder window at the click of a button, since Finder is always running. So avoid simply duplicating that functionality for its own sake. Focus on the benefit you are providing the user. Make sure that you do handle the situations where the user updates the Folder you are showing the contents of from another application and then switches back to your application.
No, they have not made Finder simply a host for a framework, like Preview. You still have to write this yourself.

Resources