I'm trying to make an AppleScript that will toggle Bluetooth, but I can't seem to get past the following error:
Expected end of line, etc. but found “"”.
Here's my code:
tell application "System Preferences"
reveal pane id "com.apple.preferences.Bluetooth"
tell application "System Events" to tell process "System Preferences"
set bluetooth to checkbox "On" of window 1
set bluetoothBool to value of checkbox "On" of window 1 as boolean
tell bluetooth
if bluetoothBool = false then
click bluetooth
display dialog "Bluetooth on" with title "Bluetooth"
buttons "OK" "Turn Bluetooth off"
default button "OK"
else if bluetoothBool = true then
click bluetooth
display dialog "Bluetooth off" with title "Bluetooth"
buttons "OK" "Turn Bluetooth on"
default button "OK"
end if
end tell
end tell
quit
end tell
"OK" "Turn Bluetooth off" needs to be {"OK", "Turn Bluetooth off"}.
Also, display dialog statements need to go all on one line, unless you "continue” a line with ¬, entered by typing Option-l (lowercase L).
tell application "System Preferences"
reveal pane id "com.apple.preferences.Bluetooth"
tell application "System Events" to tell process "System Preferences"
set bluetooth to checkbox "On" of window 1
set bluetoothBool to value of checkbox "On" of window 1 as boolean
tell bluetooth
if bluetoothBool = false then
click bluetooth
display dialog "Bluetooth on" with title ¬
"Bluetooth" buttons {"OK", "Turn Bluetooth off"} ¬
default button "OK"
else if bluetoothBool = true then
click bluetooth
display dialog "Bluetooth off" with title ¬
"Bluetooth" buttons {"OK", "Turn Bluetooth on"} ¬
default button "OK"
end if
end tell
end tell
quit
end tell
Source: AppleScript Language Guide
Related
I'm pretty new with apple scripts, and I'm trying to change the focus mode in the system preferences via Apple Script (the shortcut way is not an option). And I found one script that enables the Don't Disturb mode. But it doesn't change the mode.
How can I inspect the window and click select the focus mode?
Here is the script:
tell application "System Preferences"
set current pane to pane "com.apple.preference.notifications"
end tell
delay 0.5
tell application "System Events"
tell application process "System Preferences"
click radio button "Focus" of tab group 1 of window "Notifications & Focus"
click checkbox 1 of group 1 of tab group 1 of window "Notifications & Focus"
set theCheckbox to checkbox 1 of group 1 of tab group 1 of window "Notifications & Focus"
tell theCheckbox
set theCheckboxStatus to value of theCheckbox as boolean
if theCheckboxStatus is true then
display notification "Do not disturb is on"
else
delay 0.2
display notification "Do not disturb is off"
end if
end tell
end tell
end tell
And this is the window:
tell application "System Preferences"activateend tell
tell application "System Events"
set _0 to "Standard"
set _90 to "90°"
set _180 to "180°"
set _270 to "270°"
set preference to application process "System Preferences"
click UI element "Displays" of scroll area 1 of window "System Preferences" of preference
delay 1
click button "Display Settings..." of window "Displays" of preference
set monitor to window "LG 4K HDR" of preference
click monitor
set options to pop up button 1 of tab group 1 of monitor
set portrait to value of options is _270
click options
if portrait then
--switch to landscape mode
click menu item _0 of menu 1 of options
else
--switch to portrait mode
click menu item _270 of menu 1 of options
delay 5
click UI element "Confirm" of sheet 1 of monitor
end if
end tell
tell application "System Preferences"quitend tell
Result: error "System Events got an error: Can’t get button "Display Settings..." of window "Displays" of application process "System Preferences"." number -1728 from the button "Display Settings..." of window "Displays" of application process "System Preferences"
The above code is an automator script that pivots my monitor works until Mojave. After I update my mac, it doesn't work.
I actually don't know much about Apple Script. I got an error from 'click button "Display Settings..." of window "Displays" of preference'. I was trying to change 'button' as 'UI element', index, and system language. It doesn't work.
I think the error is because the button name is an ellipsis character and not triple dots ...
Can you try replacing that line with this instead (Monterey):
click (first button of window "Displays" of preference whose title starts with "Display Settings")
I am trying to use applescript to disable Siri, and I need to click the confirmation button.
Here is my code:
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.speech"
end tell
delay 0.5
tell application "System Events"
tell process "System Preferences"
-- click checkbox "Enable Ask Siri" of window "Siri"
if value of checkbox "Enable Ask Siri" of window "Siri" is 1 then
click checkbox "Enable Ask Siri" of window "Siri"
end if
click button "Turn Off" of window "Dictation" of window "Siri"
end tell
end tell
The following line is the one that is not working
click button "Turn Off" of window "Dictation" of window "Siri"
The button you're trying to click in on a sheet, e.g.:
Change:
click button "Turn Off" of window "Dictation" of window "Siri"
To:
click button "Turn Off" of sheet 1 of window "Siri"
I've made it to here:
tell application "System Events"
tell process "System Preferences"
tell window "Flash Player"
click radio button "Advanced" of tab group 1
tell scroll area "Advanced"
click button "Check Now" of window 2
end tell
end tell
end tell
end tell
but I can't get applescript to click the "Check Now" Button.
I'm thinking I need to add another tell application "System Events" to click the second area of the advanced tab, but so far I just get errors
This works perfectly at me:
tell application "System Events"
tell process "System Preferences"
tell window "Flash Player"
tell tab group 1
click radio button "Advanced"
tell scroll area 1
click button "Check Now"
end tell
end tell
end tell
end tell
end tell
In such cases the tool Accessibility Inspector is very helpful.
I wanted to share a very useful AppleScript with other Mac admins but it is losing focus. When it's run in AppleScript editor it runs to a certain stage and then instead of copying text to required windows it replaces text in AppleScript editor. Can anyone suggest why?
set vpnname to "VPN (Primary)"
set vpnserver to "vpn.website.com"
set vpnsecret to "abcdefghij"
set vpnusername to system attribute "USER"
set groupname to "GROUP"
activate application "System Preferences"
tell application "System Events"
-- Checks whether Universal Access is enabled - this is required for all scripts to work
if not (UI elements enabled) then
tell application "System Preferences"
activate
set current pane to pane id "com.apple.preference.universalaccess"
display dialog "This script requires access for assistive devices be enabled." & return & return & "To continue, click the OK button and enter an administrative password in the security dialog." with icon note
end tell
set UI elements enabled to true
if UI elements enabled is false then return "User Cancelled"
delay 1
end if
tell process "System Preferences"
click button "Network" of scroll area 1 of window "System Preferences"
--Creating VPN (CU Primary) interface
tell window "Network"
click button "Add Service"
delay 1
end tell
tell sheet 1 of window "Network"
click pop up button 1
click menu item "VPN" of menu 1 of pop up button 1
delay 1
click pop up button 2
click menu item "Cisco IPSec" of menu 1 of pop up button 2
set focused of text field 1 to true
keystroke "a" using command down
keystroke vpnname
click button "Create"
delay 1
end tell
--Entering server / account details
tell group 1 of window "Network"
click checkbox "Show VPN status in menu bar"
set focused of text field 3 to true
keystroke vpnserver
set focused of text field 1 to true
keystroke vpnusername
click button 2 --pressing "Advanced…" button
delay 1
end tell
--Entering "Advanced…" details
tell sheet 1 of window "Network"
activate
set focused of text field 2 to true
keystroke vpnsecret
set focused of text field 1 to true
keystroke groupname
click button "OK"
delay 1
end tell
-- Apply all changes
tell window "Network"
click button "Apply"
delay 1
end tell
tell application "System Preferences"
quit saving yes
end tell
end tell
end tell
I'm not sure if it will work but if any of the key strokes are going into applescript just run the
tell application "application name here"
activate
end tell
before the keystrokes that are being put into apple script then it will focus on the window you have told it to
Sorry for the lack of code snippet I am new to this forum and I'm not sure how to use it properly