Winterflags Winterflags - 2 months ago 20
AppleScript Question

Script to Play/Pause Soundcloud tab with Apple Remote

I'm working on an AppleScript for my Apple Remote (via BetterTouchTool) that will Play/Pause playback on Soundcloud.

It's supposed to work similarly to the native Apple Remote ▶/❚❚ button for iTunes. On SoundCloud,

keystroke space
() is Play/Pause playback;


  • If no Soundcloud tab exists, open a Soundcloud tab and
    keystroke space
    ().

  • If one Soundcloud tab exists, select it and
    keystroke space
    (will if already playing, or else ).

  • If multiple Soundcloud tabs exist, select the first one and
    keystroke space
    (Will either if the tab is playing, that tab if another tab is playing, or if no tab is playing).



So far I've been able to get it to open a new tab and start playing, or if one already exists select that tab – but it then doesn't play/pause – nor if an Soundcloud tab is already active.

tell application "Google Chrome"
repeat with w in windows
set i to 1
repeat with t in tabs of w
if URL of t starts with "https://soundcloud.com" then
set active tab index of w to i
set index of w to 1
return
end if
set i to i + 1
end repeat
end repeat
open location "https://soundcloud.com/you/likes"
activate
delay 3
tell application "System Events" to keystroke space
end tell


I'm an AppleScript newbie so unfortunately the best I can do is to experiment. Appreciative of any help.

Answer

The script must exit the loops, and the script need an if condition, like this:

set soundcloudTab to false
tell application "Google Chrome"
    repeat with w in windows
        set i to 1
        repeat with t in tabs of w
            if URL of t starts with "https://soundcloud.com" then
                set active tab index of w to i
                set index of w to 1
                set soundcloudTab to true
                exit repeat
            end if
            set i to i + 1
        end repeat
        if soundcloudTab then exit repeat -- exit windows loop
    end repeat
    activate
    if not soundcloudTab then -- no Soundcloud tab exist
        open location "https://soundcloud.com/you/likes"
        delay 3
    end if
end tell
tell application "System Events" to keystroke space

Another solution is to use JavaScript to click on the playPause button (no need to activate the tab and "Google Chrome" doesn't need to be in the foreground):

set soundcloudTab to missing value
tell application "Google Chrome"
    repeat with w in windows
        tell (first tab of w whose URL starts with "https://soundcloud.com") to if exists then
            set soundcloudTab to it
            exit repeat
        end if
    end repeat
    if soundcloudTab is missing value then
        open location "https://soundcloud.com/you/likes"
        delay 3
        set soundcloudTab to active tab of front window
    end if
    tell soundcloudTab to execute javascript "document.getElementsByClassName('playControls__playPauseSkip')[0].getElementsByTagName('button')[1].click()"
end tell
Comments