********** SOLUSI DIPERBARUI **********
Pembaruan ini merupakan solusi langsung ke pertanyaan awal OP.
Kode AppleScript berikut ini akan menambahkan item menu status "Putar / Jeda YouTube" dengan opsi untuk memutar atau menjeda video YouTube apa pun di Google Chrome atau Safari, apakah browsernya terlihat atau tidak. Simpan kode AppleScript berikut ini sebagai aplikasi "tetap buka" di 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
Jika Anda ingin yang baru ... Mainkan Jeda Menu Status YouTube. Aplikasi hanya akan terlihat di menu status dan bukan di Dok, Anda dapat mengeklik kanan aplikasi di Finder dan memilih opsi untuk "Tampilkan Konten Paket". Di folder Konten, buka file Info.plist di editor teks apa pun dan tambahkan dua baris berikut. Kemudian simpan dan tutup file itu.
<key>LSBackgroundOnly</key>
<true/>
Jika Anda tidak nyaman mengedit file .plist secara langsung, kode AppleScript berikut ini akan memungkinkan Anda memilih aplikasi untuk disembunyikan dari Dock saat sedang berjalan.
Jika aplikasi yang dipilih sudah diatur untuk disembunyikan dari Dock, satu-satunya pilihan Anda akan diberikan adalah untuk menyembunyikan aplikasi agar tidak terlihat di Dock saat sedang berjalan ... Dan sebaliknya.
Skrip ini sangat berguna untuk "aplikasi aplikasi tetap terbuka" yang bersembunyi dengan ikon aplikasi penangan kosong agar tidak muncul di Dock saat berjalan.
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
************ SOLUSI ASLI ************
Script ini akan mengklik tombol Play / Pause pada video yang diputar di YouTube di Google Chrome, apakah Google Chrome terlihat atau tidak.
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)
Ini adalah versi skrip untuk bekerja dengan 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)
Dalam upaya memberikan OP A solusi AppleScript lengkap, saya telah mengambil jawaban asli saya selangkah lebih maju ..
MEMPERBARUI
Saya akhirnya menemukan jawabannya. Saya membuat aplikasi AppleScript di Xcode. Awalnya, proyek saya hanya dimulai dengan jendela satu tombol untuk mengontrol video YouTube yang saat ini aktif di Chrome atau Safari. Proyek ini telah berkembang sedikit menjadi aplikasi yang berisi beberapa utilitas. GIF ini menampilkan tombol Jeda YouTube yang mengontrol YouTube di Chrome dan Safari. Saya menautkan tindakan tombol ke AppleScript yang awalnya saya tulis di editor skrip.
Ini adalah snapshot dari aplikasi Xcode yang Bekerja di file AppDelegate.applescript.
Berikut adalah kode dalam file yang saya buat untuk membuat program bekerja.
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
Saya memperbarui kode sehingga tab YouTube di Chrome tidak perlu menjadi tab yang terlihat atau aktif saat mengklik Tombol Jeda YouTube yang dibuat dalam Xcode
Berikut ini tautan untuk mengunduh seluruh proyek Xcode
PERINGATAN: Fungsi screen saver desktop akan membekukan Aplikasi. Setelah paksa berhenti dan dibuka kembali, fungsi screensaver desktop untuk keluar dari screen saver aktif akan berfungsi.
Renungan: Saya mungkin harus membungkus masing-masing kode AppleScript dalam pernyataan "coba" untuk menghindari semua jenis pesan kesalahan bagi mereka yang bermain dengan proyek ini, yang tidak memiliki sistem dan jenis komputer yang sama dengan yang saya lakukan. (MacBook Pro 15 "OS Sierra 10.12.6)
Agar fungsi zoom berfungsi, ini harus diaktifkan di preferensi sistem.
Agar matikan "Dictation on / off" berfungsi dengan benar, jalan pintas untuk mengaktifkan perintah dikte dalam preferensi sistem harus cocok dengan cara pintas yang digunakan dalam skrip
on doSomething13:sender -- Dictation On/Off
tell application "System Events"
keystroke "x" using {option down}
end tell
end doSomething13:
Saat ini saya sedang bekerja pada kemampuan untuk beralih antara aplikasi yang menjalankan windowed atau menu bar saja