PDA

View Full Version : Applescript Change Checkbox in System Preferences




Darkroom
Sep 20, 2008, 11:03 AM
System Prefs > Desktop & Screen Saver > Desktop > Change picture: (checkbox).

so i'm trying to make a script that says if this checkbox is checked, than to uncheck it... simple right? not really... i kinda suck at applescript...

this is what i have so far...


tell application "System Events"
tell application process "System Preferences"
if checked of checkbox "Change picture:" of group 1 of tab group 1 of window "Desktop & Screen Saver" is true then
set checkbox "Change picture:" of group 1 of tab group 1 of window "Desktop & Screen Saver" to false
end if
end tell
end tell


and unfortunately it doesn't work. i receive this message from the compiler:

"System Events got an error: Can't make checked of checkbox "Change picture:" of group 1 of tab group 1 of window "Desktop & Screen saver" of application process "System Preferences into type reference."

any thoughts?



Sayer
Sep 20, 2008, 11:20 AM
Change the script to this:

if value of checkbox "Change picture:" of group 1 of tab group 1 of window "Desktop & Screen Saver" is 1 then
click checkbox "Change picture:" of group 1 of tab group 1 of window "Desktop & Screen Saver"
end if

There's a utility at: /Developer/Applications/Utilities/Accessibility Tools/

called Accessibility Inspector.app

It shows the actions and proper value get/set for UI scripting.

Darkroom
Sep 20, 2008, 11:24 AM
tell application "System Events"
tell application process "System Preferences"
if value of checkbox "Change picture:" of group 1 of tab group 1 of window "Desktop & Screen Saver" is 1 then
click checkbox "Change picture:" of group 1 of tab group 1 of window "Desktop & Screen Saver"
end if
end tell
end tell


i see how your code makes sense, but it gives this error:

System Events got an error: Access for assistive devices is disabled.

hummmm... :confused:

Darkroom
Sep 20, 2008, 11:33 AM
oh i see what that error means now... i have to check "enable access for assistive devices" under universal access in system prefs...

but since it seems i have to check "enable access for assistive devices" first, i may not be able to do what i want. essentially, in my application i want to include this apple script in an IBAction... but i guess it wouldn't work unless the user's "enable access for assistive devices" is checked? maybe is there a way to allow my app to check that checkbox first? hah...

round and round we go...

Darkroom
Sep 21, 2008, 10:50 AM
?

Darkroom
Sep 24, 2008, 06:45 PM
was that a dumb question? i really have no idea... :confused:

if i publish an applescript online, or a cocoa app with applescript embedded in an IBAction that toggels a checkbox on the user's system (sys prefs > desktop & screensaver > desktop > change picture), and a user downloads the script/app expecting it to work, will it not work for them if their own "enable access for assistive devices" is not checked in sys preferences > universal access?

if someone could just respond YES or NO that would be great... :o

kainjow
Sep 24, 2008, 08:18 PM
Yes, that is right. You can check for it programmatically though via something like this:
tell application "System Events"
if UI elements enabled then
-- continue
end if
end tell

Darkroom
Sep 24, 2008, 08:24 PM
Yes, that is right. You can check for it programmatically though via something like this:
tell application "System Events"
if UI elements enabled then
-- continue
end if
end tell

sorry, are you saying that it's possible to check to see if the "enable access for assistive devices" is checked on the users computer first, if it's not then check it, and THEN launch the applescript to check the "change picture"?

ouff! so confusing...

kainjow
Sep 24, 2008, 11:49 PM
You can't (AFAIK) automatically enable it if it's disabled.

Darkroom
Sep 25, 2008, 12:15 AM
ok thanks kainjow

cocoascripter
Sep 12, 2009, 08:42 AM
"maybe is there a way to allow my app to check that checkbox first?"

yeah, there is a way. No idea if you're still interested, but here's my little dirty hack to get assistive Devices enabled in System Preferences without GUI-scriptings [snip]:

set PWD to "YourPasswordHere"

do shell script "touch /private/var/db/.AccessibilityAPIEnabled" password PWD with administrator privileges
do shell script "chown 0 /private/var/db/.AccessibilityAPIEnabled" password PWD with administrator privileges
do shell script "chgrp 0 /private/var/db/.AccessibilityAPIEnabled" password PWD with administrator privileges
do shell script "chmod 0444 /private/var/db/.AccessibilityAPIEnabled" password PWD with administrator privileges

The code is not formatted right, but I guess you'll get the idea.

In step 1 we create an invisible file named ".AccessibilityAPIEnabled" in a system folder, then in step 2 - 4 we change its privileges as needed for our purpose.
Please note that this is exact the way Apple handles the storage of this setting, so it's less a hack than sort of reverse engineering I've done years before ...

Furthermore: You'll need an admin password to perform this.
Enter it once in "YourPasswordHere" or read it out by a plist you have stored before.

Warning: Use correct privs for such a plist! If the admin password is stored as clear text in a plist it'll be readable by every user.
A massive security risk imo.

Hope that helps, cheers.