Where does macOS store the desktop picture change time? - macos

I want to determine programmatically whether a user has enabled automatic desktop picture change in the background & screensaver preferences or not (and also the change time).
I know that recent versions of macOS store desktop picture information in the sqlite3 database ~/Library/Application Support/Dock/desktoppicture.db. But it lacks any details regarding the picture change settings.
In older versions of macOS there existed a plist file com.apple.desktop.plist, but it seems to be no longer in use.
Does anyone know where macOS stores this information nowadays?

Is AppleScript an option?
tell application "System Events"
set pictureRotation to picture rotation of desktops
set changeIntervals to change interval of desktops
end tell
Both results are a list because you can have multiple displays.
pictureRotation contains the information whether the user has checked "Change Picture" (0 or 1).
changeIntervals contains the interval in seconds.

Related

Seamlessly change all desktop spaces' wallpaper on Mac without killall Dock

I would like to change the wallpaper on ALL desktops including spaces on Mac but without needing to call killall Dock each minute. (Dock restarting forces wallpaper refresh).
I have an AppleScript that changes the desktop wallpaper instantly:
tell application "System Events" to tell every desktop to set picture to wallpaperPath
but that only changes the wallpaper on the active space (meaning that if the main desktop is not active, its background will not change).
I found this question How to loop through all Mac desktop spaces which suggests updating wallpaper path values in the SQLite database located at ~/Library/Application Support/Dock/desktoppicture.db. This changes the wallpaper at every space which is great but it requires restart of the dock using killall Dock which is undesirable as it disrupts the workflow.
My question is - is there some way to combine these two approaches? Seamlessly change wallpapers on every desktop space?
Any suggestions would be appreciated. I have no preferred language, it can be in C, Swift, Python, Bash, AppleScript etc.
I figured it out.
I am looping through all available screens and setting the wallpaper using setDesktopImageURL:forScreen:options:error::
for screen in NSScreen.screens {
try! NSWorkspace.shared.setDesktopImageURL(url, for: screen, options: [:])
}
This changes the wallpapers seamlessly, without the need for killall Dock on all the screens but only if the desktop is the active space.
To make sure the wallpaper is changed when I am on another space (usually a fullscreen app), I added an observer for NSWorkspace.activeSpaceDidChangeNotification on the NSWorkspace.shared.notificationCenter which sets the desktop images again (using the code above). So whenever I go back to the desktop, this notification is invoked and the wallpaper is seamlessly updated.
I even went one step further and added the same observer also for the NSWorkspace.didWakeNotification which updates the wallpaper as soon as the device wakes up which is cool!

MacOS: Determine which monitor any window application is on?

I'm currently attempting to have my system pipe audio to a specific output depending on which monitor that application is running on. For instance, if a web browser is playing videos on monitor A, sound comes out of output 1. If it is on monitor B, it comes out of output 2. Is there any way to poll which monitor any/all applications are located/active on macOS?
The question hasn't given many details, but in principle you can recover the position of the window using code like the:
tell application "System Events"
tell process "App Name"
set windowPos to position of first window
end tell
end tell
... and then compare that to the frame of each desktop to discover which display the window is in. If you need to determine the frame of each desktop programmatically (instead of hand-coding them in), that will take some extra work: either digging the values out of a system plist file or using AppleScriptObjC to get the frame dimensions from NSScreen. Let me know if you need that, and I'll update when I get a chance.

Allow JavaScript from Apple Events in Safari through Terminal Mac

I'm writing a program that executes do javascript in Safari. The only problem is that I'm trying to make the app give its self permission to do it. I'm trying to locate the file that handles the Safari developer preferences so that I can do this. Does anyone have any idea where this might be or how to change these settings?
It's in Safari's preferences plist at ~/Library/Preferences/com.apple.Safari.plist. The key you want is AllowJavaScriptFromAppleEvents. You can set it using defaults:
#to turn it on
defaults write -app Safari AllowJavaScriptFromAppleEvents 1
#to turn it off
defaults write -app Safari AllowJavaScriptFromAppleEvents 0
The virtual keyboard thing did not work for me. As StarPlayr at apple's develepoer forum has found out the problem is in something else.
For me problem occurred when i tried to do that on remote mac.
For some people plugging in a keyboard and mouse to the Server allowed to turn on JavaScript Apple Events in Safari and set the password.
However, for me that wasn't an option, so the next best thing is use an accessbility scripting feature and have the machine think a user is doing the clicks, allowing you to set the password:
-- The delays can be shorter, coordinates may vary
-- Best way to get the coordinates is with Apple screen capture (command-shift-3) from upper right to lower left (the coordinates will be shown)
-- if one spends the time, the click events can be converted to Accessibility AppleScript objects by capturing them as variables, or checking the events and using the events instead of the click coordinates
tell application "System Events"
tell application "Safari"
activate
end tell
delay 1
-- click develop menu (make sure its on first)
click at {430, 12}
delay 1
-- click Allow Javascript menu from Apple Events
click at {615, 615}
delay 1
-- Click the Allow Button
click at {1010, 386}
end tell

If Preview.app in OS X is not Applescriptable why does this work?

I have asked another question on AskDifferent about Preveiw scripting dictionaries, and have done a subsequent search here which says Preview is not scriptable.
However when I run:
tell application "Preview"
set save_location to ¬
(choose file with prompt "Choose the png to be modified")
activate
open save_location
end tell
It works. Does that mean that Preview.app is scriptable with Applescript 2.2.1 and Max OS X 10.7.5 which is what I am using?
If so then where can I find a listing of the objects?
All Mac applications respond to the Open and Activate commands even if they do not have dictionaries. Your script works because it exploits the built-in capabilities of any application. The absence of a dictionary means that you cannot query or manipulate open documents and windows (i.e. the application's data model).
However, you can use UI Scripting to select menu items, click buttons within windows, and send keystrokes to the application.
You can find out what Preview's dictionary is by launching /Applications/Utilities/AppleScript Editor and choosing File>Open Dictionary from the menu bar, finding Preview in the list of applications shown there, and clicking Choose.

use defaults to read desktop wallpaper change value in mac

Is there a way to obtain the current setting for desktop wallpaper change either using Apple Script or defaults read? Basically I want to know by looking at the plist file if the option of changing wallpaper every X minutes is enabled or not.
See the AppleScript dictionary for the Desktop Suite class of the System Events helper app. It contains the property:
picture rotation (integer) : never, using interval, using login, after sleep
There are separate values for each desktop, for instance, if you have more than one monitor active. For example:
tell application "System Events"
get picture rotation of its first desktop
end tell

Resources