********** 更新的解决方案 **********
此更新是对OP原始问题的直接解决方案。
下面的AppleScript代码将添加“播放/暂停YouTube”状态菜单项,其中包含在浏览器是否可见的情况下在Google Chrome或Safari中播放或暂停任何YouTube视频的选项。将下面的AppleScript代码保存为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
如果您希望新的...播放暂停YouTube状态Menu.app仅在状态菜单中而不在Dock中可见,则可以在Finder中右键单击该应用,然后选择“显示包内容”选项。在目录文件夹中,在任何文本编辑器中打开Info.plist文件,并添加以下两行。然后保存并关闭该文件。
<key>LSBackgroundOnly</key>
<true/>
如果您不满意直接编辑.plist文件,则下面的AppleScript代码将允许您选择要在Dock运行时隐藏的应用程序。
如果已将选定的应用程序设置为从Dock隐藏,则您将获得的唯一选择是取消隐藏该应用程序,使其在Dock运行时不可见……反之亦然。
该脚本特别适用于隐藏的“保持打开的应用程序”,其中闲置处理程序的应用程序图标在运行时不会出现在Dock中。
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
************ 原始解决方案 ************
无论是否显示Google Chrome,此脚本都会在YouTube上的YouTube视频中单击视频上的播放/暂停按钮。
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)
这是用于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)
为了使OP拥有完整的AppleScript解决方案,我将原始答案又向前了一步。
更新
我终于弄明白了。我用Xcode创建了一个AppleScript应用程序。最初,我的项目仅从一个按钮窗口开始,以控制当前在Chrome或Safari中处于活动状态的YouTube视频。这个项目已经成长为包含多个实用程序的应用程序。此GIF显示了YouTube暂停按钮,可在Chrome和Safari中控制YouTube。我将按钮操作链接到我最初在脚本编辑器中编写的AppleScript。
这是在AppDelegate.applescript文件中工作的Xcode应用程序的快照。
这是我创建的使程序正常运行的文件中的代码。
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
我更新了代码,以便在单击Xcode中创建的YouTube暂停按钮时,无需将Chrome中的YouTube标签设为可见或活动标签
这是下载整个Xcode项目的链接
警告:桌面屏幕保护程序功能将冻结该应用程序。强制退出并重新打开后,退出活动屏幕保护程序的桌面屏幕保护程序功能将起作用。
事后思考: 我可能应该将每个AppleScript代码包装在“ try”语句中,以避免那些与此项目和系统类型不同的人遇到各种错误消息。(MacBook Pro 15英寸操作系统Sierra 10.12.6)
要使缩放功能起作用,必须在系统偏好设置中将其启用。
为了正确切换“听写开/关”,在系统首选项中启用听写命令的快捷方式必须与脚本中使用的快捷方式匹配
on doSomething13:sender -- Dictation On/Off
tell application "System Events"
keystroke "x" using {option down}
end tell
end doSomething13:
目前,我正在研究仅在运行窗口的应用程序或菜单栏之间切换的功能