Using AppleScript to modify settings/system preferences

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-28 09:34:05

问题


I am trying to make an AppleScript that toggles automatic rearranging of spaces. I am able to get the AppleScript to open system preferences and go into mission control settings, however i am not sure how to check the box which i want to change.

    tell application "System Preferences"
    activate
end tell

tell application "System Events"
    tell process "System Preferences"
        click menu item "Mission Control" of menu "View" of menu bar 1
    delay 2
    tell window "Mission Control"
            //additional code goes here
    end tell
    end tell
end tell

Is there a way to see what the components of the window are so i know if i need to go into a table, or something else, before i am able to access the check boxes that toggle the settings


回答1:


This should to what you want.

In this example Automatically rearrange Spaces based on most recent use is the checkbox you want to check.

tell application "System Preferences"
    activate
    delay 2
    set the current pane to pane id "com.apple.preference.expose"
    delay 2
    tell application "System Events"
        click checkbox "Automatically rearrange Spaces based on most recent use" of group 2 of window "Mission Control" of application process "System Preferences"
    end tell
    quit
end tell

And this if you wanna check it only if it's not checked:

tell application "System Preferences"
    activate
    delay 2
    set the current pane to pane id "com.apple.preference.expose"
    delay 2
    tell application "System Events"
        tell checkbox "Automatically rearrange Spaces based on most recent use" of group 2 of window "Mission Control" of application process "System Preferences"
            if (get its value) = 0 then click it
        end tell
    end tell
    quit
end tell

And if you wanna list all the UIElements in the window:

set myArray to {}
tell application "System Preferences"
    activate
    delay 2
    set the current pane to pane id "com.apple.preference.expose"
    delay 2
    tell application "System Events"
        tell window "Mission Control" of application process "System Preferences"
            repeat with uiElem in entire contents as list
                set myArray to myArray & ((class of uiElem as string) & " : " & name of uiElem as string)
            end repeat
        end tell
    end tell
end tell



回答2:


This is an alternative method using a shell command via AppleScript, which has the benefit of not requiring System Preferences to be open/running/visible. It's also much faster.

If you do happen to have it open in order to monitor whether the script below works, bear in mind that the GUI (what you see) is not updated until you close System Preferences, and open it up again.

To set Automatically rearrange Spaces based on most recent use to true (i.e. so the checkbox is ticked):

do shell script "defaults write com.apple.dock 'mru-spaces' -bool true; killall Dock"

To set it to false, i.e. untick the checkbox, change true to false in the line of code above.

To toggle the setting, this short script will achieve that:

    set currentSetting to ¬
        (do shell script ¬
            "defaults read com.apple.dock 'mru-spaces'") ¬
            as integer as boolean

    do shell script ¬
        "defaults write com.apple.dock 'mru-spaces' -bool " & ¬
        (not currentSetting) & ¬
        "; killall Dock"

Note: Tested with MacOS High Sierra, but should work (I believe) in OS X Mavericks and later.


Other Settings

When switching to an application, switch to a Space with open windows for the application

do shell script "defaults write -g AppleSpacesSwitchOnActivate -bool true"

(or false if you want the option off.)

Group windows by application

do shell script "defaults write com.apple.dock 'expose-group-apps' -bool true; killall Dock"

(or false if you want the option off.)




回答3:


Let me start by saying while both of the other answers prior to this one do work, nonetheless I wouldn't use either one of them for the following reasons.

The answer presented by shadowsheep works however it needlessly exposes the System Preferences GUI and I believe unless your system is really slow the value of the delay command is excessive by 50% and only one should be necessary in this use case.

The answer presented by CJK works however it uses killall Dock which is visually distracting and causes all minimized windows on all Desktops to be unminimized leading to further visual distractions, and clutters the Desktop(s), which can then require the User to cleanup the mess. Even without other windows open it's still more so a visual distraction then what I'll present.

Now every User has different work habits so maybe none of the reasons mentioned are of any consequence to you. Personally, I work between four virtual Desktops and can have dozens of windows opened in numerous apps across the Desktops with many, if not most minimized at times. Using killall Dock for me is the last thing I want to do most of the time.

With that said, here's an alternative to both of the existing answers prior to this one.

It's probably safe to say that most Users don't open and leave open System Preferences however the following example AppleScript code checks to see if it's running and if so closes it. This is so it can be opened without showing the GUI, so as not to have to see the the visual distraction of have the GUI change as the script progresses.

This example AppleScript code simply toggles the state of the target checkbox:

if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
tell application "System Preferences"
    reveal pane id "com.apple.preference.expose"
    delay 1
    tell application "System Events"
        tell group 2 of window 1 of application process "System Preferences"
            click checkbox "Automatically rearrange Spaces based on most recent use"
        end tell
    end tell
    quit
end tell

This example AppleScript code conditionally clicks the target checkbox using 0 or 1 in
if value is equal to 0 then click it. Use 0 to only click it if it's not checked and 1 to only click it if it's checked.

if running of application "System Preferences" then
    quit application "System Preferences"
    delay 1
end if
tell application "System Preferences"
    reveal pane id "com.apple.preference.expose"
    delay 1
    tell application "System Events"
        tell group 2 of window 1 of application process "System Preferences"
            tell checkbox "Automatically rearrange Spaces based on most recent use"
                if value is equal to 0 then click it
            end tell
        end tell
    end tell
    quit
end tell

Both example AppleScript code blocks shown work fast and without seeing the System Preferences GUI and the only visual effect is the Dock Tile for System Preferences does a single bounce and may not even be noticeable, especially when compared to the visual distraction of killall Dock.


Note that the value of the delay commands may need to be adjusted for your system, and or additional delay commands may or may not be needed. Adjust values of and or add/remove the delay commands as appropriate.


Note: The example AppleScript code is just that and does not employ any other error handling then what's shown and is meant only to show one of many ways accomplish a task. The onus is always upon the User to add/use appropriate error handling as needed/wanted.



来源:https://stackoverflow.com/questions/49214368/using-applescript-to-modify-settings-system-preferences

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!