title: Trying to write a basic bash script, but failing to do a keystroke space
body: <p>I would like to write a script that opens a Youtube song and &quot;presses&quot; the spacebar to play the song. I've spent quite a lot of time trying to fix that last bit, &quot;pressing&quot; the spacebar. I'm trying to get this to work on my Mac.</p>
  <p>Here's the script</p>
  <pre><code>#!/bin/bash
  open https://www.youtube.com/watch?v=fG6o_C9SOW8&amp;list=LL&amp;index=10&amp;ab_channel=mick73funk
  sleep 5s
  #printf &quot;0x20&quot;
  keystroke space
  </code></pre>
  <p>It opens the tab with the song in Firefox, so far so good. But then gives an error</p>
  <pre><code>$ ./finally_friday.sh 
  ./finally_friday.sh: line 9: keystroke: command not found
  </code></pre>
  <p>I tried <code>printf &quot;0x20&quot;</code> before and different variants of <code>[SPACE]</code> like <code>{SPACE}</code> but it always comes back with &quot;command not found&quot;.</p>
  <p>Update, 17 april: I kind of got it working with Apple script(thank you Mr R). If I execute it from the Terminal the space key is pressed in the Terminal and not in Firefox. But if I schedule the script in crontab it works as expected.</p>
  <pre><code>osascript &lt;&lt; EOF
  
  tell application &quot;Firefox&quot;
      open location &quot;https://www.youtube.com/watch?v=fG6o_C9SOW8&amp;list=LL&amp;index=10&amp;ab_channel=mick73funk&quot;
      delay 4
      tell application &quot;System Events&quot;
          keystroke space
      end tell
  end tell
  EOF
  </code></pre>
  <p>A friend of mine solved the task with Python, here is his code that works executing it from the Terminal as well.</p>
  <pre><code>import os
  import time
  from pynput.keyboard import Key, Controller
  
  keyboard = Controller()
  
  path = &quot;https://www.youtube.com/watch?v=fG6o_C9SOW8&amp;list=LL&amp;index=10&amp;ab_channel=mick73funk &quot;
  os.system(f&quot;open {path}&quot;)
  
  time.sleep(5)
  # Press and release space
  keyboard.press(Key.space)
  keyboard.release(Key.space)
  </code></pre>
  <p>A big thank you to all that has replied to my post!</p>