2

I created this AppleScript to play/pause Youtube videos running on Chrome:

on run {input, parameters}

    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('ytp-play-button ytp-button')[0].click();"
    end tell

    return input
end run

It works fine when I run it from Automator. I saved the service and added a keyboard shortcut +J for running the service. I also added Automator to the Accessibility settings (following this SO answer).

Now here's the strange part: it works when I have the System Preferences window on focus. If I close it, or switch to any other window, the shortcut doesn't work. I noticed that it's working because there's a menu item in System Preferences window that corresponds to the shortcut, and that is getting triggered when I press the shortcut.

Please help. Thanks in advance.

  • Use another shortcut, because it's a shortcut that already exists in several applications. (Preview, Script Editor, Mail, TextEdit, iTunes, Safari, Finder, Terminal, Microsoft Word, TextWrangler, Automator, etc..) – jackjr300 Jul 28 '17 at 16:44
1

As I mentioned in my comment to wch1zpink's answer "Making sure the Service's settings are correct and the shortcut key sequence is not in use by another is the first step" and mentioned afterwards by jackjr300 in his comment. Changing the keyboard shortcut to one that is not used by anything else in the system or any application (that is if 'Services receives no input in any application' is set), works for me, whether or not System Preferences is open, when using the code, as is, in the OP and a non-conflicting key sequence.

There also is no need to have any applications set in System Preferences > Security & Privacy > Privacy > Accessibility for this particular use case, whether running from Automator or when using a non-conflicting keyboard shortcut in System Preferences > Keyboard > Shortcuts > Services > (Name You Gave The Service).

To resolve your issue, you must use a keyboard shortcut that is not already assigned elsewhere in the system and or any applications.

You might have to use a three or four key sequence as there are very few, if any, two key combos that aren't already in use.


On a side note, return input is not at all needed in the code in this particular use case.

0

Try substituting that entire code in your post with exactly this:

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)

enter image description here

  • Tried this. Still the same behavior. Works when Preferences window is open, not otherwise. – th3an0maly Jul 28 '17 at 15:50
  • 1
    Try adding Google Chrome to the Accessibility settings also – wch1zpink Jul 28 '17 at 15:51
  • I'm curious as to why you think wrapping the code in a handler and calling the handler to run the execute command is going to produce any different results then it does when calling a properly formed execute command _ directly, without the use of a handler? Using a _handler is not going to make one bit of difference in this use case as both coding variations do exactly the same thing, the both actually execute the following command: execute tab id # of window id # javascript "document.getElementsByClassName('ytp-play-button ytp-button')[0].click();" – user3439894 Jul 28 '17 at 15:53
  • The fact that the code, as written in the OP, already worked when run in Automator (or Script Editor) shows there is nothing wrong with the code itself and no explicit need to use it within a handler to resolve the expressed issue. On a side note, it's a known issue that user added shortcut key sequences are buggy, work sporadically at times or not at all for some users. Changing the manner of how the code is executed is not going to resolve the issue in this use case. Making sure the Service's settings are correct and the shortcut key sequence is not in use by another is the first step. – user3439894 Jul 28 '17 at 15:53
  • I have the code wrapped in a handler because originally I have a script that I have tons of snippets in and I usually call the handler from a load of script action. I just copied the code with the handler In case others want to try to use it multiple times In a script. – wch1zpink Jul 28 '17 at 15:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .