Vor dem Upgrade auf Yosemite habe ich Keyboard Maestro verwendet, um dieses Applescript mit einer Verknüpfung aufzurufen:
tell application "System Preferences"
reveal pane "com.apple.preferences.Bluetooth"
end tell
tell application "System Events" to tell process "System Preferences"
click button 6 of window 1
end tell
quit application "System Preferences"
Es verhielt sich wie erwartet und schaltete Bluetooth um, wenn ich meine Verknüpfung drückte.
Es funktioniert jedoch nicht mehr mit Yosemite, ich bin mir sicher, dass es etwas damit zu tun hat, dass Apple das Systemeinstellungsfenster oder die Reihenfolge der Symbole geändert hat, aber ich bin mir nicht sicher, was ich ändern soll. Die Tastenkombination Keyboard Maestro ruft die Datei auf, weil ich den Ton höre, den ich ihr zugewiesen hatte, also liegt es definitiv am Skript.
Folgendes erhalte ich unter "Antworten", wenn ich dies im Skripteditor von Apple ausführe:
tell application "System Preferences"
reveal pane "com.apple.preferences.Bluetooth"
--> missing value
end tell
tell application "System Events"
click button 6 of window 1 of process "System Preferences"
--> button 6 of window "Bluetooth" of application process "System Preferences"
end tell
tell application "Script Editor"
quit
end tell
Aktualisierung:
Es ist definitiv nicht die Scheibe, die das Problem verursacht. Zum Debuggen habe ich den Code geändert in:
tell application "System Preferences"
set current pane to pane id "com.apple.preferences.bluetooth"
end tell
Und es öffnet korrekt das Bluetooth-Fenster. Jetzt muss ich nur noch herausfinden, welche Art von Aktion ich in diesem Bereich ausführen möchte:
Update zu Lösungen: Danke weiter weg! Sowohl die Lösungen von markhunte als auch die von fartheraway funktionierten für mich, aber ich entschied mich für letztere, weil sie meinem Code ähnlicher war. Ich denke, Sie können nicht zwei "beste" Antworten auswählen. Ich wünschte, es gäbe eine Lösung, damit es funktioniert, ohne das Einstellungsfenster (visuell) aufzurufen, wie es mein Skript mit Mavericks getan hat, aber diese beiden sollten gut genug sein.
Aktualisierte/bessere Antwort:
1) Dieses neue Skript blinkt nicht.
2) Aus Gründen, die dem Menschen und der Logik (oder nur mir) unbekannt sind, scheitert Applescript manchmal/fast immer daran, Bluetooth auszuschalten , wenn das Systemeinstellungsfenster im Hintergrund ist. Anstatt sich auszuschalten, aktiviert sich Bluetooth tatsächlich sofort wieder, sodass sich das Fenster in einem frischen Zustand befindet: Es ist eingeschaltet, aber keine Verbindungen.
Um dies zu überwinden, können Sie SysPref wie in der ursprünglichen Antwort nach vorne bringen. Oder führen Sie eine Schleife aus, die erneut (oder ein drittes Mal) auf die Schaltfläche klickt, bis Bluetooth wirklich ausgeschaltet ist. Deshalb gibt es im Skript zwei Variablen und eine Schleife. Dies sollte das Skript zuverlässiger machen. statName
Variable zeichnet den ursprünglichen Zustand auf. Loop
wird so lange auf die Schaltfläche klicken, bis sich der Status geändert hat. failSafe
stellt sicher, dass das Skript im Fehlerfall nicht für immer ausgeführt wird. Alles auf Kosten der Ästhetik des Codes.
tell application "System Events"
tell process "System Preferences"
activate
end tell
tell application "System Preferences"
set current pane to pane "com.apple.preferences.Bluetooth"
end tell
tell process "System Preferences"
set statName to name of button 3 of window 1 as string
set failSafe to 0
repeat until statName is not name of button 3 of window 1 as string ¬
or failSafe is 10
click button 3 of window 1
set failSafe to failSafe + 1
delay 0.1
end repeat
end tell
tell application "System Preferences"
quit
end tell
end tell
Ursprüngliche Antwort:
tell application "System Preferences"
activate --Change 1/2
reveal pane "com.apple.preferences.Bluetooth"
end tell
tell application "System Events" to tell process "System Preferences"
click button 3 of window 1 --Change 2/2
end tell
quit application "System Preferences"
Vom Accessibility Inspector :
button 3
im Punkt Nr. 6 auf der Liste. Die 6. Schaltfläche ist Nr. 11 in der Liste. Wenn Sie button 6
das Einstellungsfenster aufrufen, wird Genie. Ich denke, Mavericks hatte alle Knöpfe vorne gebündelt.
Ich habe hier viel gelernt, ich hoffe dieser Beitrag hilft auch jemandem! Ich habe festgestellt, dass die Verwendung von "Start" anstelle von "Aktivieren" die App sichtbar öffnet, jedoch nicht als Vordergrundfenster. Der andere Trick, oder zumindest "eine nette Sache, die ich kürzlich gelernt habe, war möglich", besteht darin, anstelle eines "Verzögerungs" -Werts, den ich auch verwende, eine leere Wiederholungsschleife zu verwenden, um darauf zu warten, dass das Fenster geladen wird (und damit die Schaltfläche existiert). um zu überprüfen, ob die Änderung funktioniert hat, bevor eine Benachrichtigung angezeigt wird. Im Rest meines Codes geht es darum, den Status der Systemeinstellungen beizubehalten, wenn er bereits geöffnet war, oder ihn andernfalls zu beenden.
set bundleID to "com.apple.systempreferences"
-- Check for System Preferences running already
tell the application "System Events" to set runningApps to (bundle identifier of every application process)
if bundleID is in runningApps then
set stayOpen to true
else
set stayOpen to false
end if
tell application id "com.apple.systempreferences"
-- Problem with this setting is that the toggle doesn't work if the prefPane is open in the background — the window /must/ be visible
if not (stayOpen) then launch
-- If it's already running, save the current prefPane for later
if (stayOpen) then set prevPane to current pane
set current pane to pane id "com.apple.preferences.bluetooth"
end tell
tell the application "System Events"
-- An empty repeat loop to keep checking for the window
-- Here I am lazy and don't use the identifier
repeat until window "Bluetooth" of process "System Preferences" exists
end repeat
tell window "Bluetooth" of process "System Preferences"
if button "Turn Bluetooth Off" exists then
-- Click and wait for it to change, then send a notification
click button "Turn Bluetooth Off"
repeat until button "Turn Bluetooth On" exists
end repeat
display notification "Bluetooth Off"
else
click button "Turn Bluetooth On"
repeat until button "Turn Bluetooth Off" exists
end repeat
display notification "Bluetooth On"
end if
end tell
end tell
tell application id "com.apple.systempreferences"
if (stayOpen) then
if prevPane is not missing value then set current pane to prevPane
else if not (stayOpen) then
quit
end if
end tell
Einfaches Ein- oder Ausschalten, das nicht zuerst den Status prüfen muss.
property thePane : "com.apple.preferences.bluetooth"
tell application "System Preferences"
activate
set the current pane to pane id thePane
--delay 1
end tell
tell application "System Events"
tell application process "System Preferences"
try
click button "Turn Bluetooth Off" of window "Bluetooth"
on error
click button "Turn Bluetooth On" of window "Bluetooth"
end try
end tell
end tell
tell application "System Preferences" to quit
Hier ist meine Antwort:
tell application "System Preferences"
reveal pane id "com.apple.preferences.Bluetooth"
-- activate
set the current pane to pane id "com.apple.preferences.Bluetooth"
try
tell application "System Events" to tell process "System Preferences"
click button "Turn Bluetooth Off" of window "Bluetooth"
click button "Turn Bluetooth Off" of sheet 1 of window "Bluetooth" of application process "System Preferences" of application "System Events"
end tell
delay 1
on error
tell application "System Events" to tell process "System Preferences"
click button "Turn Bluetooth On" of window "Bluetooth"
quit
end tell
end try
end tell
Hier ist ein einfaches Toggle-Bluetooth-Skript, das blueutil (verfügbar über Homebrew) verwendet, kein UI-Skripting. Passen Sie die blueutil
Variable nach Bedarf an, um auf die blueutil
Binärdatei zu verweisen, wenn Sie nicht über Homebrew installieren. Dies basiert lose auf einem alten Skript, das ich herumliegen hatte, das Growl-Benachrichtigungen enthielt und wahrscheinlich ursprünglich von http://macosxhints.com (RIP) stammte.
set blueutil to "/usr/local/bin/blueutil"
set powerStatus to do shell script blueutil & " power"
if powerStatus is "1" then
do shell script blueutil & " power 0"
else if powerStatus is "0" then
do shell script blueutil & " power 1"
end if
no input
any application
Bluetooth Toggle.Workflow
tell application "System Events"
tell process "ControlCenter"
set BluetoothButton to menu bar item "Bluetooth" of menu bar 1
click BluetoothButton
delay 1
set OnSwitch to checkbox "Bluetooth" of group 1 of window "Control Center"
click OnSwitch
end tell
key code 53
end tell
Siehe: Einrichten von Bluetooth mit AppleScript in Big Sur – r/applescript, 1.12.20
Bluetooth Toggle.Workflow
tell application "System Events"
tell application process "Control Center"
click menu bar item "Bluetooth" of menu bar 1
tell window "Control Center"
try
click checkbox "Bluetooth"
on error
click checkbox "Bluetooth"
end try
end tell
end tell
key code 53 -- # escape key
end tell
Siehe: AppleScript-Fehler – Gruppe 1 des Fensters „Control Center“ kann nicht abgerufen werden – StackExchange, 10.11.21
Für macOS Big Sur funktioniert die Antwort von Enrique Scherer nicht mehr. Das Dienstprogramm blueutil wurde jedoch aktualisiert und kann beispielsweise von Homebrew installiert werden.
Zwieback
Nullhedge
Zwieback
Nullhedge
Zwieback
weiter weg