【问题标题】:Activate Select Element Safari - AppleScript激活选择元素 Safari - AppleScript
【发布时间】:2019-05-03 23:55:37
【问题描述】:

我是AppleScript 的新手,我正在尝试弄清楚如何激活所选元素。

我可怜的尝试让 safari 激活目标元素:

我通过 Tab 键选择元素。活动元素是播放按钮:

.

然而,当我尝试运行脚本时,出现以下错误:

谁能告诉我我的错误和/或展示一个解决方法的例子吗?

【问题讨论】:

标签: macos safari applescript


【解决方案1】:

在您的 AppleScript 代码中使用 JavaScript,您可以在 Safari 中使用以下代码在 Script Editor 应用程序中单击 YouTube 视频上的播放/暂停按钮

clickYouTubePlayButton("ytp-play-button ytp-button", 0)

to clickYouTubePlayButton(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 clickYouTubePlayButton


响应用户 @user3439894 的评论,关于必须启用 Safari 以允许 JavaScript 事件,以上代码才能工作...如果需要,以下代码将自动执行在 Safari 中启用 JavaScript 事件的过程成为..

使用最新版本的 macOS Mojave 和最新版本的 Safari 进行测试

global windowName, errMsg, errNum

clickYouTubePlayButton("ytp-play-button ytp-button", 0)

to clickYouTubePlayButton(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
        on error errMsg number errNum
            if errNum is 8 then
                my enableJavascript()
            end if
        end try
    end if
end clickYouTubePlayButton

on enableJavascript()
    tell application "Safari" to activate
    tell application "System Events"
        tell application process "Safari"
            click menu bar item "Safari" of menu bar 1
            click menu item 4 of menu 1 of menu bar item "Safari" of menu bar 1
            delay 1
            set windowName to get name of window 1
            click UI element "Advanced" of toolbar 1 of window windowName
            click checkbox "Show Develop menu in menu bar" of group 1 of group 1 ¬
                of window "Advanced"
            click UI element 2 of window "Advanced"
            click menu bar item "Develop" of menu bar 1
            click menu item "Allow JavaScript from Apple Events" of menu 1 of ¬
                menu bar item "Develop" of menu bar 1
            delay 0.5
            if exists of UI element "Allow" of window 1 then
                click UI element "Allow" of window 1
            end if
        end tell
    end tell
end enableJavascript

【讨论】:

  • 可能需要注意的是,要使其正常工作,请在 Safari 中隐藏的 Developer 菜单上Allow JavaScript from Apple Events b> 需要勾选,默认不勾选。
  • @user3439894 我为您的评论发布了一个解决方案,作为对我原始答案的编辑。好奇你对我帖子中的第二个脚本的想法
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-11
  • 1970-01-01
  • 2015-01-05
  • 1970-01-01
  • 2018-10-24
相关资源
最近更新 更多