Questo script farà clic sul pulsante Riproduci / Pausa su un video riprodotto su YouTube in Google Chrome, indipendentemente dal fatto che Google Chrome sia visibile o meno.
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)
Questa è la versione dello script per funzionare con Safari
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)
Nel tentativo di fornire all'OP una soluzione AppleScript completa, ho portato la mia risposta originale un ulteriore passo avanti.
Aggiorna
Finalmente l'ho capito. Ho creato un'applicazione AppleScript in Xcode. Inizialmente, il mio progetto è iniziato solo con una finestra con un solo pulsante per controllare i video di YouTube attualmente attivi in Chrome o Safari. Questo progetto è cresciuto un po 'in un'applicazione che contiene diverse utilità. Questa GIF mostra il pulsante Pausa di YouTube che controlla YouTube in Chrome e Safari. Ho collegato le azioni dei pulsanti all'AppleScript che avevo originariamente scritto nell'editor di script.
Questaèun'istantaneadell'applicazioneXcodechelavoranelfileAppDelegate.applescript.
Eccoilcodiceinquelfilechehocreatoperfarfunzionareilprogramma.
scriptAppDelegatepropertyparent: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
Ho aggiornato il codice in modo che la scheda YouTube in Chrome non debba essere la scheda visibile o attiva quando fai clic sul pulsante Pausa di YouTube creato in Xcode
Ecco un link per scaricare l'intero progetto Xcode
ATTENZIONE:lafunzionediscreensaverdeldesktopbloccal'applicazione.Dopolachiusuraforzataelariapertura,lafunzionedisalvaschermodeldesktopperusciredalloscreensaverfunzionerà.
Ripensamenti:probabilmenteavreidovutoracchiudereognunodeicodiciAppleScriptnelleistruzioni"try" per evitare tutti i tipi di messaggi di errore per chi giocava con questo progetto, che non ha lo stesso sistema e tipo di computer che faccio. (MacBook Pro 15 "OS Sierra 10.12.6)
Per la funzione di zoom Per funzionare, deve essere abilitato nelle preferenze di sistema.
Perattivare/disattivarelafunzione"Dettatura", la scorciatoia per abilitare i comandi di dettatura nelle preferenze di sistema deve corrispondere alla scorciatoia usata nello script
ondoSomething13:sender--DictationOn/Offtellapplication"System Events"
keystroke "x" using {option down}
end tell
end doSomething13:
Attualmente sto lavorando sulla possibilità di passare dall'applicazione solo alla finestra o alla barra dei menu