Adapt script to Xcode?

I have a script that works well in the Script Editor. I want to make a Preferences window so I created an Xcode project. I have created a button in AppDelegate.applescipt The button works and it opens the System Preferences window, but it does not click the checkbox. What am I missing?


on buttonClicked_(sender)

tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preference.dock"
	delay 1
end tell

tell application "System Events"
	click checkbox "Automatically hide and show the dock" of window "Dock" of application process "System Preferences" of application "System Events"
end tell

end buttonClicked_

As I am running the system in French I was forced to use localized items.

To get a running script I had to edit your code as :

--on buttonClicked:sender
set theBundle to ((path to library folder from system domain as text) & "PreferencePanes:Appearance.prefPane")
# Get the localized version of the string:
set localized_title to localized string "Gmk-ia-4rw.ibExternalAccessibilityDescription" from table "LookPref" in bundle file theBundle --> "Masquer/afficher automatiquement la barre des menus"
# Alternate key
-- set localized_title to localized string "Psr-dn-2cx.title" from table "LookPref" in bundle file theBundle
tell application "System Preferences"
	activate
	set the current pane to pane id "com.apple.preference.general"
	delay 1
end tell

tell application "System Events" to tell process "System Preferences"
	click checkbox localized_title of window 1
end tell

--end buttonClicked:

I apologize but here the target window is not named “Dock” but “Général”.
I didn’t find the way to get the localized title of the window “General” which is “Général” so I used the index 1.

My understanding is that there is an extraneous specifier in your code.
But it would be useful to replace “Dock” by “General
Try with:

tell application "System Events"
   click checkbox "Automatically hide and show the dock" of window "Dock" of application process "System Preferences" --of application "System Events"
end tell

or, as I did,

tell application "System Events" to tell process "System Preferences"
   click checkbox "Automatically hide and show the dock" of window "Dock" --of application process "System Preferences" of application "System Events"
end tell

CAUTION: under 10.13.6, there are two keys for the wanted string.
I used “Gmk-ia-4rw.ibExternalAccessibilityDescription” but I would have the same result using the alternate key “Psr-dn-2cx.title”

Would be useful to check which key(s) is(are) used in more recent systems which I can’t install on my machine.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) dimanche 17 novembre 2019 17:58:12

In Mojave and Catalina there are more restrictions to be able to send Apple Events

In the project in Info.plist add this key-value pair

<key>NSAppleEventsUsageDescription</key>
<string>MyApp needs access to System Events and System Preferences</string>

The string inside the tag is arbitrary.

Further – if Hardened Runtime is available in Signing & Capabilities – check the Apple Events check box.

@StefanK thank you. I am new and I feel quite lost. I have added a key and value in the info.plist I suppose that it must be without the tags and is that right?

On the other hand I could find the Singning & Capabilities but where is the Hardened Runtime?

The key-value syntax appears in Source Code view.

In Property List view the key is represented by Privacy - AppleEvents Sending Usage Description

In Xcode 11 Hardened Runtime is added when you are asked to update to recommended settings

Ok, I solved the first part.

But when you say “In Xcode 11 Hardened Runtime is added when you are asked to update to recommended settings” I do not see any time that I am asked that. Sorry, I just do not understand and I do not know where to go to learn.

Often after updating Xcode there are changes in the default settings and the user is prompted to update the settings with a yellow warning.

@Yvan Koenig: Your answer is very good to make that script usable in different languages. I have tested and it works very well. But the problem here is security warnings:

Not authorized to send Apple events to System Preferences. (error -1743)
System Events got an error: appleScriptTest is not allowed assistive access. (error -1719)

If it is for me I can include the app to the System Preferences / Security & Privacy / Privacy / Accessibility and Automation

But that means that I cannot distribute the app to other people because it creates a horrible experience.