Apple Script to turn File Sharing off & on in one script - macos

I am pulling my hair out to write a script that does the following in 10.8. :
-uncheck 'File Sharing' in the 'Sharing' Controlpanel
-check 'File Sharing' in the 'Sharing' Controlpanel
-make sure that it is checked when the script finishes
Why I want to do this ? Because there is a bug in 10.8. with Samba (cannot smb-login from another machine), if File sharing gets turned off an back on when starting up, all is fine.
Can anyone help me out on this … ? Should be an easy one for you guys :-)
Thank you very much in advance, best- Ph!L!pp

This code should toggle the sharing preferences, wait 1 second, then toggle them again.
tell application "System Preferences"
activate
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "Sharing" of menu "View" of menu bar 1
delay 2
tell window "Sharing"
click checkbox 1 of row 3 of table 1 of scroll area 1 of group 1
delay 1
if (exists sheet 1) then
if (exists button "Turn AirPort On" of sheet 1) then
click button "Turn AirPort On" of sheet 1
delay 1
end if
click button "Start" of sheet 1
end if
end tell
end tell
end tell
delay 1
tell application "System Events"
tell process "System Preferences"
click menu item "Sharing" of menu "View" of menu bar 1
delay 2
tell window "Sharing"
click checkbox 1 of row 3 of table 1 of scroll area 1 of group 1
delay 1
if (exists sheet 1) then
if (exists button "Turn AirPort On" of sheet 1) then
click button "Turn AirPort On" of sheet 1
delay 1
end if
click button "Start" of sheet 1
end if
end tell
end tell
end tell

Related

Need accessibility help on mac os sound output using applescript

set outputB to 2 --change this to the actual 'line number' of your second desired output
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.sound"
end tell
tell application "System Events"
tell application process "System Preferences"
repeat until exists tab group 1 of window "Sound"
end repeat
tell tab group 1 of window "Sound"
click radio button "Output"
if (selected of row outputA of table 1 of scroll area 1) then
set selected of row outputB of table 1 of scroll area 1 to true
else
set selected of row outputA of table 1 of scroll area 1 to true
end if
end tell
end tell
end tell
--tell application "System Preferences" to quit
I have this apple script from Arthur Hammer. It works perfect if I'm in script editor. When I export it as an application and try to open it from my desktop it only gives me the error "System Events got an error: Output 1 is not allowed assistive access. (-25211)".
I verified that sys pref > security & privacy > privacy > accessibility > Output 1 and script editor are enabled. I've restarted my computer as well but I still can only get that error.
Any ideas how to fix it?

Applescript Click item in system menu bar after opening it

I can open the bluetooth dropdown menu item with this code but I don't know how to actually click on any of the items in the menu.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
end tell
What would be a command to click on something in the open bluetooth menu?
This following AppleScript code should accomplish what you are trying to achieve. Just replace the "Mac Pro" part of the code with the name of the item you want to click.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
click checkbox "Mac Pro" of scroll area 1 of window 1
key code 53 -- Press escape key
end tell
This following AppleScript code will return the names of the checkboxes so you can easily know your options to use in the first code.
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
set checkBoxNames to name of checkboxes of scroll area 1 of window 1
end tell

AppleScript to Connect Bluetooth Device in Monterey (12.0.1)

I want to write a simple script to connect my MacBook to the bluetooth speaker in my living room, but I can't figure out the final click.
-- Turn Bluetooth on
tell application "System Preferences"
activate
reveal pane "com.apple.preferences.Bluetooth"
end tell
delay 1
tell application "System Events"
tell process "System Preferences"
if button "Turn Bluetooth On" of window "Bluetooth" exists then
click button "Turn Bluetooth On" of window "Bluetooth"
end if
end tell
end tell
-- Connect Living Room speakers
tell application "System Events"
tell process "ControlCenter"
set BluetoothButton to menu bar item "Bluetooth" of menu bar 1
click BluetoothButton --This works and makes the dialog pop up
delay 3
set TheCheckbox to button "Living Room" of scroll area 1 of window "Control Centre"
if TheCheckbox exists then return "Yay"
if value of TheCheckbox is 0 then click TheCheckbox
end tell
end tell
-- This is how to do it through the System Preferences
-- But this doesn't work because there's no way to double click the device in the bluetooth menu
(*set thePathPref to (path to library folder from system domain as text) & "PreferencePanes:"
tell application "System Preferences"
activate
reveal pane "com.apple.preferences.Bluetooth"
end tell
delay 1
tell application "System Events"
tell process "System Preferences"
if button "Turn Bluetooth On" of window "Bluetooth" exists then
click button "Turn Bluetooth On" of window "Bluetooth"
end if
delay 1
set LivingRoomButton to UI element "Living Room" of row 3 of table 1 of scroll area 1 of window "Bluetooth" of application process "System Preferences" of application "System Events"
--if LivingRoomButton exists then
click LivingRoomButton -- Needs to be a double click but that's impossible
--end if
end tell
end tell
*)
If I
return every UI element of window "Control Centre"
I get {} back, which makes me think window "Control Centre" isn't the right place to be looking for the button, but using an application called UI Browser, that definitely seems like where it lives. So I don't know what to do.
As it stands I get the error "System Events got an error: Can’t get scroll area 1 of window "Control Centre" of process "ControlCenter". Invalid index." on the line
set TheCheckbox to button "Living Room" of scroll area 1 of window "Control Centre"
This is an old question, but the 1st result in a search so I'll answer here. This works for me in 12.6:
tell application "System Events" to tell process "ControlCenter"
click menu bar item "Bluetooth" of menu bar 1
repeat until exists of checkbox 1 of scroll area 1 of window 1
delay 0.1
end repeat
repeat with uiElem in checkboxes of scroll area 1 of window 1 as list
if name of uiElem = "Your_device_name_here" then
click uiElem
end if
end repeat
key code 53 -- # escape key to close the window
end tell
I don't have a Bluetooth speaker, but I use the following script to connect to my HomePod (Airplay). The HomePod shows up in the list under System Preferences -> Sound. If your Bluetooth speaker shows up in that list as well, this script should work on macOS 12 Monterey:
set outPutSrc to "Living Room"
tell application "System Preferences"
reveal anchor "output" of pane id "com.apple.preference.sound"
activate
tell application "System Events"
tell process "System Preferences"
try
delay 1
select (row 1 of table 1 of scroll area 1 of tab group 1 of window "Sound" whose value of text field 1 is outPutSrc)
end try
end tell
end tell
quit
end tell

UI automation, appleScript Keyboard shortcuts for non-menued items Preview?

I am trying to create a keyboard shortcut action for Preview, namely Draw and Sketch. However, they are are the NON-menued items, which means can't get it done in System Preference I see someone done it from inspiration, it is possible, but when I try to follow alone, here is my code so far and please help me complete this. here are the action.
Here is the error I am facing now
after some researches, does the UI/accessibility inspector help?
activate application "Preview"
delay 0.4
set the menuItem to "Draw"
tell application "System Events"
try
tell application process "Preview" to click radio button menuItem of radio group 1 of splitter group 1 of window 1
on error
try
tell application process "Preview" to click radio button menuItem of radio group 1 of window 1
on error errorM
display dialog errorM
end try
end try
end tell
ideally, trying to make it to work when all bars are hidden,
but if not possible. can we make it to work vwith mark up bar is shown. as below.
This work for me using the latest version of macOS Mojave
tell application "Preview" to activate
repeat while application "Preview" is not running
delay 0.2
end repeat
tell application "System Events"
try
click menu item "Show Markup Toolbar" of menu 1 of menu bar item "View" of menu bar 1 of application process "Preview"
end try
delay 0.5
try
click menu item "Show Toolbar" of menu 1 of menu bar item "View" of menu bar 1 of application process "Preview"
end try
delay 0.5
repeat while not (exists of toolbar 1 of window 1 of application process "Preview")
delay 0.2
end repeat
set description2 to a reference to every checkbox of toolbar 1 of window 1 of application process "Preview"
set theCheckboxes to description of description2
if item 1 of theCheckboxes is "Draw" then
set checkBoxDraw to 1
else
set checkBoxDraw to 2
end if
if item 1 of theCheckboxes is "Sketch" then
set checkBoxSketch to 1
else
set checkBoxSketch to 2
end if
delay 1
-- Below, insert either checkBoxSketch for "Sketch" or checkBoxDraw for "Draw"
click checkbox checkBoxDraw of toolbar 1 of window 1 of application process "Preview"
end tell
-- without these next following lines, the toolbar "Draw" or "Sketch" do not appear to be selected
tell application "Preview" to tell window 1
set visible to false
set visible to true
end tell
delay 3

How to download a new voice for say's function with AppleScript?

I'm searching a method to install different voices (2 to be exact) on the system preferences.
The voices are "Alex" for an english voice and "Thomas" for a french voice.
I've tried directly by console but didn't succeed, that's why I've turned to the AppleScript language, but I never used this language.
The code I've for the moment is
set osver to system version of (system info)
if osver is equal to "10.6.8" then
display dialog ("Downloading voices is only available in OS X Lion and higher")
else
tell application "System Preferences"
activate
reveal (pane id "com.apple.preference.speech")
end tell
try
tell application "System Events"
click radio button 2 of tab group 1 of window 1 of process "System Preferences"
repeat until (exists pop up button of tab group 1 of window 1 of process "System Preferences")
delay 2
end repeat
delay 2
click pop up button 1 of tab group 1 of window 1 of process "System Preferences"
delay 2
click menu item -1 of menu 1 of pop up button of tab group 1 of window 1 of process "System Preferences"
delay 2
end tell
on error
display dialog ("An error happend")
end try
end if
This program is opening the voice window but the display dialog appears every time whatever the index I put.
If you have another idea to download the voices, or if you can help me to understand what is not working, I will be grateful.
This worked for me in 10.9:
tell application "System Preferences"
reveal anchor "TTS" of pane id "com.apple.preference.speech"
activate
end tell
tell application "System Events" to tell window 1 of process "System Preferences"
tell pop up button 1 of tab group 1
click
click menu item "Customize..." of menu 1
end tell
delay 1
repeat with r in UI element 1 of rows of table 1 of scroll area 1 of sheet 1
if exists static text 1 of r then
if {"Alex", "Thomas"} contains value of static text 1 of r then
if value of checkbox 1 of r is 0 then click checkbox 1 of r
end if
end if
end repeat
click button "OK" of sheet 1
end tell
It took multiple seconds to run the script though.

Resources