Gibt es eine Software, die es ermöglicht, ein aktuell wiedergegebenes YouTube-Video (oder idealerweise alle Online-Video-/Audiomedien) mit einer Tastenkombination oder einer bequem zugänglichen Schaltfläche (z befindet sich in der Menüleiste oben rechts auf dem Bildschirm)? Je weniger Klicks nötig sind, desto besser.
Der Schlüssel hier ist, dass ich die Möglichkeit haben möchte, das Video in jeder Anwendung anzuhalten, das heißt, wenn Google Chrome nicht die wichtigste Anwendung ist (z. B. TextEdit oder Microsoft Word ist die wichtigste Anwendung).
iOS hat diese Verknüpfung eingebaut. Wenn man vom unteren Bildschirmrand nach oben wischt, erscheinen Mediensteuerelemente. Diese Steuerelemente können alle Audiodaten manipulieren, die aus einer Safari-Registerkarte stammen.
Mein Webbrowser ist Google Chrome.
OS X El Capitan, Version 10.11.6.
Ich wäre auch offen dafür, dies mit einem AppleScript zu erreichen (das dann einer Tastenkombination in FastScripts.app zugewiesen werden kann). Aber ich kann mir nicht vorstellen, dass eine so komplexe Aufgabe über AppleScript möglich ist.
********** AKTUALISIERTE LÖSUNG **********
Dieses Update ist eine direkte Lösung für die ursprüngliche Frage des OP.
Der folgende AppleScript-Code fügt ein Statusmenüelement „YouTube abspielen/anhalten“ mit den Optionen zum Abspielen oder Anhalten von YouTube-Videos in Google Chrome oder Safari hinzu, unabhängig davon, ob die Browser sichtbar sind oder nicht. Speichern Sie den folgenden AppleScript-Code als „geöffnet bleiben“-Anwendung in Script Editor.app.
use framework "Foundation"
use framework "AppKit"
use scripting additions
property StatusItem : missing value
property selectedMenu : ""
property defaults : class "NSUserDefaults"
property internalMenuItem : class "NSMenuItem"
property externalMenuItem : class "NSMenuItem"
property newMenu : class "NSMenu"
my makeStatusBar()
my makeMenus()
on makeStatusBar()
set bar to current application's NSStatusBar's systemStatusBar
set StatusItem to bar's statusItemWithLength:-1.0
-- set up the initial NSStatusBars title
StatusItem's setTitle:"Play/Pause YouTube"
-- set up the initial NSMenu of the statusbar
set newMenu to current application's NSMenu's alloc()'s initWithTitle:"Custom"
newMenu's setDelegate:me (*
Requied delegation for when the Status bar Menu is clicked the menu will use the delegates method (menuNeedsUpdate:(menu)) to run dynamically update.*)
StatusItem's setMenu:newMenu
end makeStatusBar
on makeMenus()
newMenu's removeAllItems() -- remove existing menu items
set someListInstances to {"Play/Pause YouTube - Safari", "Play/Pause YouTube - Chrome", "Quit"}
repeat with i from 1 to number of items in someListInstances
set this_item to item i of someListInstances
set thisMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:this_item action:("someAction" & (i as text) & ":") keyEquivalent:"")
(newMenu's addItem:thisMenuItem)
(thisMenuItem's setTarget:me) -- required for enabling the menu item
end repeat
end makeMenus
on someAction1:sender
clickClassName2("ytp-play-button ytp-button", 0)
end someAction1:
on someAction2:sender
clickClassName("ytp-play-button ytp-button", 0)
end someAction2:
on someAction3:sender
quit me
end someAction3:
to clickClassName2(theClassName, elementnum)
if application "Safari" is running then
try
tell application "Safari"
tell window 1 to set current tab to tab 1 whose URL contains "youtube"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end try
end if
end clickClassName2
to clickClassName(theClassName, elementnum)
tell application "Google Chrome" to (tabs of window 1 whose URL contains "youtube")
set youtubeTabs to item 1 of the result
tell application "Google Chrome"
execute youtubeTabs javascript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
end tell
end clickClassName
Wenn Sie möchten, dass Ihre neue... Play Pause YouTube Status Menu.app nur im Statusmenü und nicht im Dock sichtbar ist, können Sie im Finder mit der rechten Maustaste auf die App klicken und die Option "Paketinhalt anzeigen" auswählen. Öffnen Sie im Ordner Contents die Datei Info.plist in einem beliebigen Texteditor und fügen Sie die folgenden zwei Zeilen hinzu. Speichern und schließen Sie dann diese Datei.
<key>LSBackgroundOnly</key>
<true/>
Wenn Sie die .plist-Datei nicht direkt bearbeiten möchten, können Sie mit dem folgenden AppleScript-Code die Anwendung auswählen, die beim Ausführen im Dock ausgeblendet werden soll.
Wenn die ausgewählte Anwendung bereits so eingestellt ist, dass sie im Dock ausgeblendet wird, haben Sie nur die Möglichkeit, die Anwendung im Dock sichtbar zu machen, während sie ausgeführt wird … Und umgekehrt.
Dieses Skript ist besonders praktisch, um „Anwendungen offen bleiben“ zu verbergen, wobei die Anwendungssymbole von inaktiven Handlern nicht im Dock angezeigt werden, während sie ausgeführt werden.
property fileTypes : {"com.apple.application-bundle"}
property plistFileItem : " <key>LSBackgroundOnly</key>" & linefeed & " <true/>"
activate
set chosenApp to (choose application with prompt ¬
"Choose The Application You Want Hidden From The Dock While It Is Running" as alias)
tell application "System Events" to set appName to name of chosenApp
set plistFile to ((POSIX path of chosenApp) & "/Contents/info.plist") as string
set plistFileContents to (read plistFile)
set plistFileItemExists to plistFileItem is in plistFileContents
if plistFileItemExists then
activate
set theChoice to button returned of (display dialog ¬
"Would you like to un-hide " & quote & appName & quote & ¬
" from the Dock while it's running?" buttons {"Cancel", "Un-Hide"} ¬
default button 2 cancel button 1 with title "Make A Choice")
else
activate
set theChoice to button returned of (display dialog ¬
"Would you like to hide " & quote & appName & quote & ¬
" from the Dock while it's running?" buttons {"Cancel", "Hide"} ¬
default button 2 cancel button 1 with title "Make A Choice")
end if
if theChoice is "Hide" then
tell application "System Events" to tell contents of property list file plistFile ¬
to make new property list item at end with properties ¬
{kind:string, name:"LSBackgroundOnly", value:true}
else if theChoice is "Un-Hide" then
tell application "System Events" to tell contents of property list file plistFile ¬
to make new property list item at end with properties ¬
{kind:string, name:"LSBackgroundOnly", value:false}
else
return
end if
************ ORIGINALLÖSUNG ************
Dieses Skript klickt auf die Play/Pause-Schaltfläche eines Videos, das in YouTube in Google Chrome abgespielt wird, unabhängig davon, ob Google Chrome sichtbar ist oder nicht.
to clickClassName(theClassName, elementnum)
tell application "Google Chrome" to (tabs of window 1 whose URL contains "youtube")
set youtubeTabs to item 1 of the result
tell application "Google Chrome"
execute youtubeTabs javascript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
end tell
end clickClassName
clickClassName("ytp-play-button ytp-button", 0)
Dies ist die Skriptversion, die mit Safari funktioniert
to clickClassName2(theClassName, elementnum)
tell application "Safari"
tell window 1 to set current tab to tab 1 whose URL contains "youtube"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName2
clickClassName2("ytp-play-button ytp-button", 0)
In dem Bemühen, dem OP eine vollständige AppleScript-Lösung zu geben, habe ich meine ursprüngliche Antwort noch einen Schritt weiter gebracht.
AKTUALISIEREN
Ich habe es endlich herausgefunden. Ich habe eine AppleScript-Anwendung in Xcode erstellt. Ursprünglich begann mein Projekt nur mit einem Ein-Knopf-Fenster zur Steuerung von YouTube-Videos, die derzeit in Chrome oder Safari aktiv sind. Dieses Projekt ist ein wenig zu einer Anwendung gewachsen, die mehrere Dienstprogramme enthält. Dieses GIF zeigt die YouTube-Pause-Schaltfläche, die YouTube in Chrome und Safari steuert. Ich habe die Tastenaktionen mit dem AppleScript verknüpft, das ich ursprünglich im Skripteditor geschrieben habe.
Dies ist ein Schnappschuss der Xcode-Anwendung, die in der Datei AppDelegate.applescript arbeitet.
Hier ist der Code in dieser Datei, den ich erstellt habe, damit das Programm funktioniert.
script AppDelegate
property parent : class "NSObject"
-- IBOutlets
property theWindow : missing value
to clickClassName(theClassName, elementnum) -- Handler for pausing YouTube in Chrome
if application "Google Chrome" is running then
try
tell application "Google Chrome" to (tabs of window 1 whose URL contains "youtube")
set youtubeTabs to item 1 of the result
tell application "Google Chrome"
execute youtubeTabs javascript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
end tell
end try
end if
end clickClassName
to clickClassName2(theClassName, elementnum) -- Handler for pausing YouTube in Safari
if application "Safari" is running then
try
tell application "Safari"
tell window 1 to set current tab to tab 1 whose URL contains "youtube"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end try
end if
end clickClassName2
on doSomething:sender -- Calls the Chrome YouTube Handler
clickClassName("ytp-play-button ytp-button", 0)
end doSomething:
on doSomething14:sender -- Calls the Safari YouTube Handler
clickClassName2("ytp-play-button ytp-button", 0)
end doSomething14:
on doSomething2:sender -- Hide and or show the Menu Bar
tell application "System Preferences"
reveal pane id "com.apple.preference.general"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "General"
click checkbox "Automatically hide and show the menu bar"
end tell
delay 1
quit application "System Preferences"
end doSomething2:
on doSomething3:sender -- Sets Display resolution to the second lowest setting (15 inch Built In Retina Display - MBP)
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
click radio button "Scaled" of radio group 1 of tab group 1
click radio button 2 of radio group 1 of group 1 of tab group 1
end tell
quit application "System Preferences"
end doSomething3:
on doSomething4:sender -- Sets Display resolution to the second highest setting (15 inch Built In Retina Display - MBP)
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
click radio button "Scaled" of radio group 1 of tab group 1
click radio button 4 of radio group 1 of group 1 of tab group 1
end tell
quit application "System Preferences"
end doSomething4:
on doSomething5:sender -- Sets Display resolution to the highest setting (15 inch Built In Retina Display - MBP)
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
click radio button "Scaled" of radio group 1 of tab group 1
click radio button 5 of radio group 1 of group 1 of tab group 1
end tell
quit application "System Preferences"
end doSomething5:
on doSomething6:sender -- Sets Display resolution to the lowest setting (15 inch Built In Retina Display - MBP)
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
click radio button "Scaled" of radio group 1 of tab group 1
click radio button 1 of radio group 1 of group 1 of tab group 1
delay 0.1
click button "OK" of sheet 1
quit application "System Preferences"
end tell
end doSomething6:
on doSomething7:sender -- Displays a dialog with your current IP
tell current application to display dialog (do shell script "curl ifconfig.io") with icon 2 buttons "OK" default button 1 with title "Your Current IP Address Is.." giving up after 5
end doSomething7:
on doSomething8:sender -- Shows hidden files in Finder
do shell script "defaults write com.apple.finder AppleShowAllFiles TRUE\nkillall Finder"
end doSomething8:
on doSomething9:sender -- Hides hidden files in Finder if they are showing
do shell script "defaults write com.apple.finder AppleShowAllFiles FALSE\nkillall Finder"
end doSomething9:
on doSomething10:sender -- Brightness Highest
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
set value of value indicator 1 of slider 1 of group 2 of tab group 1 to 12
end tell
quit application "System Preferences"
end doSomething10:
on doSomething11:sender -- Brightness Lowest
tell application "System Preferences"
reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
end tell
tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
set value of value indicator 1 of slider 1 of group 2 of tab group 1 to 0.1
end tell
quit application "System Preferences"
end doSomething11:
on doSomething12:sender -- Zoom
tell application "System Events"
key code 28 using {command down, option down}
end tell
end doSomething12:
on doSomething13:sender -- Dictation On/Off
tell application "System Events"
keystroke "x" using {option down}
end tell
end doSomething13:
on doSomething15:sender -- Enables Screensaver as Desktop background
tell application "System Events"
do shell script "/System/Library/Frameworks/ScreenSaver.framework/Resources/ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background"
end tell
end doSomething15:
on doSomething16:sender -- Kills Screensaver Desktop background
try
tell application id "com.apple.ScreenSaver.Engine" to quit
end try
end doSomething16:
on applicationWillFinishLaunching:aNotification
-- Insert code here to initialize your application before any files are opened
end applicationWillFinishLaunching:
on applicationShouldTerminate:sender
-- Insert code here to do any housekeeping before your application quits
return current application's NSTerminateNow
end applicationShouldTerminate:
on applicationShouldTerminateAfterLastWindowClosed:sender -- Quits app when clicking red x
return TRUE
end applicationShouldTerminateAfterLastWindowClosed:
end script
Ich habe den Code so aktualisiert, dass die YouTube-Registerkarte in Chrome nicht die sichtbare oder aktive Registerkarte sein muss, wenn auf die in Xcode erstellte YouTube-Pause-Schaltfläche geklickt wird
Hier ist ein Link zum Herunterladen des gesamten Xcode-Projekts
WARNUNG: Die Desktop-Bildschirmschonerfunktion friert die App ein. Nach dem erzwungenen Beenden und erneuten Öffnen funktioniert die Desktop-Bildschirmschonerfunktion zum Beenden des aktiven Bildschirmschoners.
Nachtrag: Ich hätte wahrscheinlich jeden der AppleScript-Codes in "try"-Anweisungen packen sollen, um alle möglichen Fehlermeldungen für diejenigen zu vermeiden, die mit diesem Projekt spielen, die nicht das gleiche System und den gleichen Computertyp haben wie ich. (MacBook Pro 15" Betriebssystem Sierra 10.12.6)
Damit die Zoomfunktion funktioniert, muss sie in den Systemeinstellungen aktiviert werden.
Damit das Umschalten von „Diktat ein/aus“ korrekt funktioniert, muss die Tastenkombination zum Aktivieren von Diktierbefehlen in den Systemeinstellungen mit der im Skript verwendeten Tastenkombination übereinstimmen
on doSomething13:sender -- Dictation On/Off
tell application "System Events"
keystroke "x" using {option down}
end tell
end doSomething13:
Derzeit arbeite ich an der Möglichkeit, zwischen der Anwendung, die nur im Fenster oder nur in der Menüleiste ausgeführt wird, umzuschalten
display dialing ...
benötigen Sie nur diese eine Codezeile tell application "Google Chrome" to execute front window's active tab javascript "document.getElementsByClassName('ytp-play-button ytp-button')['0'].click();"
. Da das OP ein aktuell abgespieltes YouTube-Video anhalten (und wieder anhalten) möchte, ist Google bereits geöffnet und es könnte minimiert werden, wenn der aktive Tab abgespielt wird, und die oben erwähnte Codezeile wird darauf reagieren. Daher ist es nicht erforderlich, das Fenster zu aktivieren oder wie in Ihrem Code zu verwenden, launch
da dies in der Dokumentation angegeben ist, Fortsetzung im nächsten Kommentar ...clickClassName
Handler bringt , warum ist es überhaupt da, es ist überhaupt nicht notwendig, da theClassName
es elementnum
direkt wie in der im ersten Kommentar zitierten Zeile gesetzt werden kann. Diesen Handler zu haben ist in Ordnung, wenn Sie mehr als eine Verwendung von getElementsByClassName
haben, aber in diesem Fall und wie ich es sehe, tun Sie das nicht, es sei denn, Sie bauen ein Programm, um das zu tun, was BeardedSpice bereits tut. Was natürlich eine beträchtliche Codierung erfordern würde. Auf überflüssiges Codieren! :)Hier erfahren Sie, wie Sie mit reinem AppleScript in die Menüleiste gelangen. Als Anwendung speichern mit stay open after run handler
:
PS Ich habe den Code für die eigentlichen Play/Pause-Funktionen von @wch1zpink gestohlen, also stimme bitte auch deren Antwort zu
--AppleScript: menu bar script -- Created 2017-03-03 by Takaaki Naganoya adapted by Josh Brown
--2017 Piyomaru Software
use AppleScript version "2.4"
use scripting additions
use framework "Foundation"
use framework "AppKit"
--http://piyocast.com/as/archives/4502
property aStatusItem : missing value
on run
init() of me
end run
on init()
set aList to {"Google Chrome", "⏯", "", "Safari", "⏯", "", "Quit"}
set aStatusItem to current application's NSStatusBar's systemStatusBar()'s statusItemWithLength:(current application's NSVariableStatusItemLength)
aStatusItem's setTitle:"🎛"
aStatusItem's setHighlightMode:true
aStatusItem's setMenu:(createMenu(aList) of me)
end init
on createMenu(aList)
set aMenu to current application's NSMenu's alloc()'s init()
set aCount to 1
repeat with i in aList
set j to contents of i
if j is not equal to "" then
set aMenuItem to (current application's NSMenuItem's alloc()'s initWithTitle:j action:"actionHandler:" keyEquivalent:"")
else
set aMenuItem to (current application's NSMenuItem's separatorItem())
end if
(aMenuItem's setTarget:me)
(aMenuItem's setTag:aCount)
(aMenu's addItem:aMenuItem)
if j is not equal to "" then
set aCount to aCount + 1
end if
end repeat
return aMenu
end createMenu
on actionHandler:sender
set aTag to tag of sender as integer
set aTitle to title of sender as string
if aTitle is "Quit" then
current application's NSStatusBar's systemStatusBar()'s removeStatusItem:aStatusItem
end if
#Chrome
if aTitle is "⏯" then
clickClassName("ytp-play-button ytp-button", 0)
end if
#Safari
if aTitle is "⏯" then
clickClassName2("ytp-play-button ytp-button", 0)
end if
end actionHandler:
to clickClassName(theClassName, elementnum)
tell application "Google Chrome" to (tabs of window 1 whose URL contains "youtube")
set youtubeTabs to item 1 of the result
tell application "Google Chrome"
execute youtubeTabs javascript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();"
end tell
end clickClassName
to clickClassName2(theClassName, elementnum)
tell application "Safari"
tell window 1 to set current tab to tab 1 whose URL contains "youtube"
do JavaScript "document.getElementsByClassName('" & theClassName & "')[" & elementnum & "].click();" in document 1
end tell
end clickClassName2
quit
Befehl hinzufügen , um dies zu beheben. Das zweite Problem ist, dass Symbole, die Sie verwenden, nicht gut angezeigt werden, wenn die dunkle Menüleiste verwenden und die allgemeine Systemeinstellung Dock ausgewählt sind. Sie können die Symbole wirklich nicht sehen, bis Sie mit der Maus darüber fahren. Sie können dem Menüpunkt mit den Symbolen Text hinzufügen, z. B.:if aTitle is "Quit" then
current application's ...
Play/Pause YouTube ⏯
LSUIElement = 1
hinzugefügt wird. name.app/Contents/Info.plist
IMO Für diese Art von Menü-Extra-App muss die Dock-Kachel der App nicht angezeigt werden.--http://piyocast.com/as/archives/4502
Kommentar im Code nicht mehr gültig ist, aber diese Antwort Applescript aus der Menüleiste ausführen? vom Autor des Originalcodes enthält den Originalcode, der sich früher unter dieser URL befand. Die Antwort enthält auch den defaults
Befehl zum Ausblenden der Dock-Kachel, z. B.:defaults write /Applications/name_of_app.app/Contents/Info.plist LSUIElement -bool yes
Monomet
Rubiks Sphäre
enzo
Rubiks Sphäre
calum_b