How to close Safari preference window using AppleScript? - macos

I am using the below script to enable developer menu.
tell application "Safari"
tell application "System Events"
tell process "Safari"
click menu item "Preferences…" of menu 1 of menu bar item "Safari" of menu bar 1
click button "Advanced" of toolbar 1 of window 1
click checkbox "Show Develop menu in menu bar" of group 1 of group 1 of window 1
-- delay 2
keystroke "w" using {command down} -- > not working
end tell
end tell
end tell
How to close the preference window? With keystroke "w" using {command down}, I am getting cannot The document cannot be closed while the script is running. error.
Also how to enable checkbox only if it is not enabled? Currently, if I run the script twice, it toggles.

Just click the first button of the first window and check the value of the checkbox
tell application "Safari"
tell application "System Events"
tell process "Safari"
click menu item "Preferences…" of menu 1 of menu bar item "Safari" of menu bar 1
click button "Advanced" of toolbar 1 of window 1
tell checkbox "Show Develop menu in menu bar" of group 1 of group 1 of window 1
if value is 0 then click it
end tell
click button 1 of window 1
end tell
end tell
end tell

This AppleScript code works for me using the latest version of macOS Mojave.
tell application "System Events"
tell application process "Safari"
set frontmost to true
end tell
keystroke "." using {command down}
end tell

Related

How to programatically switch between touchbar layouts on MacOS?

System
M1 MacBook Pro
MacOS Big Sur
Problem
I like the default Mac touchbar layout for everyday use, but I prefer the F1-F12 keys at my fingertips when programming. I also don't like holding down the fn key. That's why I wrote two AppleScripts to switch the layouts (included below).
The scripts work, but they are buggy. This is because they rely on opening the System Preferences app and navigating through the menus. I made a couple "apps" with Automator
that simply run the scripts and then assigned them to keyboard shortcuts.
This is an ok solution, but I'd like to do something more elegant. Ideally, my script should run behind the scenes and instantly change the touchbar layout instead of opening System Preferences, selecting items from drop-down-lists, and then finally closing System Preferences.
I messed around with the shell for quite awhile with no success before resorting to using Automator. Any suggestions from those who are more savvy with sort of thing?
Code
This one makes the F1-F12 keys the default touchbar layout:
tell application "System Preferences"
set the current pane to pane id "com.apple.preference.keyboard"
delay 0.25
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 2 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 2 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 4 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "Show App Controls" of menu 1 of pop up button 4 of tab group 1 of window "Keyboard"
end tell
end tell
quit application "System Preferences"
And this one does the reverse (makes app controls the default touchbar layout):
tell application "System Preferences"
set the current pane to pane id "com.apple.preference.keyboard"
delay 0.25
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 2 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "App Controls" of menu 1 of pop up button 2 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click pop up button 4 of tab group 1 of window "Keyboard"
end tell
end tell
tell application "System Events"
tell process "System Preferences"
click menu item "Show F1, F2, etc. Keys" of menu 1 of pop up button 4 of tab group 1 of window "Keyboard"
end tell
end tell
quit application "System Preferences"
On macOS Catalina when toggling the target setting it changes the value of the PresentationModeGlobal key in ~/Library/Preferences/com.apple.touchbar.agent.plist from appWithControlStrip to functionKeys or vise versa for those two choices. However, toggling it programmatically using the defaults command while it changes it in the UI it does not change it on the Touch Bar without also restarting the ControlStrip process.
The following example shell script code is what I use with a single keyboard shortcut to toggle between between Show App Controls and F1, F2, etc. Keys as that is what they are set to respectively in System Preferences > Keyboard > Keyboard on my system.
Example shell script code:
#!/bin/zsh
pmg="$(defaults read com.apple.touchbar.agent 'PresentationModeGlobal')"
if [[ $pmg == "appWithControlStrip" ]]; then
defaults write com.apple.touchbar.agent 'PresentationModeGlobal' 'functionKeys'
killall "ControlStrip"
else
defaults write com.apple.touchbar.agent 'PresentationModeGlobal' 'appWithControlStrip'
killall 'ControlStrip'
fi
Notes:
Other versions of macOS may require additional settings to be changed and or additional processes to be restarted, e.g., pkill 'Touch Bar agent' if applicable.
In Terminal you can use the read verb of the defaults command to examine changes to com.apple.touchbar.agent.plist as you make them in the UI to see if the value for additional keys needs to be changed too.
Side Note
As to your AppleScript code, here is how I'd do it based on your code as a single script to toggle between the two choices.
You didn't say what version of macOS are you running and since I do not have a pop up button 4 I cannot test the example AppleScript code shown below, however, this should eliminate having two separate scripts and it just toggles between the two with a single keyboard shortcut.
Example AppleScript code:
tell application "System Preferences"
set the current pane to pane id "com.apple.preference.keyboard"
delay 0.25
reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell
tell application "System Events"
tell tab group 1 of window 1 of process "System Preferences"
if value of pop up button 2 is "Show App Controls" then
click pop up button 2
click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 2
click pop up button 4
click menu item "Show App Controls" of menu 1 of pop up button 4
else
click pop up button 2
click menu item "Show App Controls" of menu 1 of pop up button 2
click pop up button 4
click menu item "F1, F2, etc. Keys" of menu 1 of pop up button 4
end if
end tell
end tell
quit application "System Preferences"
Note: The example AppleScript code is just that and sans any included error handling does not contain any additional error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors. Additionally, the use of the delay command may be necessary between events where appropriate, e.g. delay 0.5, with the value of the delay set appropriately.

Not able to clear History using applescript in El Capitan OSX

I have been using below script to clear the safari history from selenium test in Yosemite OSX. I have upgraded the OS to El Capitan and now the script wont work.
Error returned : Can't get "Clear History" button
Any help on this please.
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
tell menu bar 1
tell menu bar item "History"
tell menu 1
click menu item "Clear History…"
end tell
end tell
end tell
delay 1 -- optional
click button "Clear History"
end tell
end tell
tell application "Safari" to activate
set frontmost to true
tell application "System Events"
tell process "Safari"
set frontmost to true
click menu item "Clear History…" of menu "History" of menu bar 1
delay 3
click button "Clear History" of sheet 1 of window 1
end tell
end tell
tell application "Safari" to quit

Need applescript for clear cookie on MAC for Safari 8 browser

I need to perform below things using applescript.
Launch Safari,
click "Clear History and Website Data" on Safari menu bar,
click "Clear History" button,
close safari browser.
I have tried with below code:
tell application "Safari" to activate
tell application "System Events"
click menu item "Clear History and Website Data…" of menu "Safari" of menu bar item "Safari" of menu bar 1 of application process "Safari"
delay 1
click button "Clear History" of pop up "Safari"
quit
end tell
tell application "Safari" to activate
set frontmost to true
tell application "System Events"
tell process "Safari"
set frontmost to true
click menu item "Clear History…" of menu "History" of menu bar 1
delay 3
click button "Clear History" of sheet 1 of window 1
end tell
end tell
tell application "Safari" to quit

XCode 4 Debugger: Attach to process from command line

I would like to launch an application from the command line and immediately attach the graphical debugger in XCode 4 to it, without having to click in the Xcode menu Product->Attach To Progress. Is it possible to script this using a bash or Apple script?
This seems to work in the Xcode-Beta Version 6.2 (6C121). For Xcode, just change "Xcode-Beta" to Xcode. Also, you'll want to change the process name "Staging" to the process you'd like to attach.
tell application "Xcode-Beta"
activate
end tell
tell application "System Events"
tell application process "Xcode"
click (menu item "By Process Identifier (PID) or Name…" of menu 1 of menu item "Attach to Process" of menu 1 of menu bar item "Debug" of menu bar 1)
end tell
tell application process "Xcode"
set value of text field 1 of sheet 1 of window 1 to "Staging"
end tell
tell application process "Xcode"
click button "Attach" of sheet 1 of window 1
end tell
end tell
Worked for Xcode 12
tell application "Xcode"
activate
end tell
tell application "System Events"
tell application process "Xcode"
click (menu item "Attach to Process by PID or Name…" of menu 1 of menu bar item "Debug" of menu bar 1)
end tell
tell application process "Xcode"
set value of text field 1 of sheet 1 of window 1 to "Your App/Process Name"
end tell
tell application process "Xcode"
click button "Attach" of sheet 1 of window 1
end tell
end tell
tell application "our App/Process Name"
activate
end tell

Dropdown in Monitor System Preferences

tell application "System Events"
tell application "System Preferences"
activate
set current pane to pane "com.apple.preference.displays"
set theWindows to windows
set win2 to item 2 of theWindows
tell win2
set index to 1
set visible to false
set visible to true
end tell
set value of combo box 1 to "90°"
end tell
end tell
I'm trying to change the Rotation of the External Screen via Applescript, but I do
not find out how to access this dropdown menu. Google seems to give me a lot about combo box and pop menu, but under Lion at least all this stuff doesn't work.
This worked for me on 10.8.
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences"
tell pop up button 1 of tab group 1 of window 1
click
click menu item 3 of menu 1
end tell
end tell

Resources