Jump to content

Control playing audio (not itunes music) with alfred remote


Recommended Posts

I don't use apple music or itunes,but instead rely on youtube music (be it on chrome with youtube,or as the standalone YT music app )

Is there no way to use alfred remote to simulate the OS button press ? Such as pause,next,previous ? 

I would like to control my music/video from my phone using alfred remote ,isnt it possible ?

Link to comment

Welcome @avichou,

 

First, add your Remote Trigger. Connect it to a Run Script Action with language set to /usr/bin/swift and the following Script:

 

import Quartz

let supportedKeys: [String: UInt32] = [
  "screen-brightness-up": 2,
  "screen-brightness-down": 3,
  "keyboard-brightness-up": 21,
  "keyboard-brightness-down": 22,
  "play-pause": 16,
  "fast-forward": 17,
  "rewind": 18,
  "volume-mute-toggle": 7,
  "volume-up": 0,
  "volume-down": 1,
]

func pressKey(_ key: UInt32) {
  func keyDown(_ down: Bool) {
    let flags = NSEvent.ModifierFlags(rawValue: (down ? 0xa00 : 0xb00))
    let data1 = Int((key << 16) | (down ? 0xa00 : 0xb00))

    let ev = NSEvent.otherEvent(
      with: NSEvent.EventType.systemDefined,
      location: NSPoint(x: 0, y: 0),
      modifierFlags: flags,
      timestamp: 0,
      windowNumber: 0,
      context: nil,
      subtype: 8,
      data1: data1,
      data2: -1
    )

    let cev = ev?.cgEvent
    cev?.post(tap: CGEventTapLocation.cgSessionEventTap)
  }

  keyDown(true)
  keyDown(false)
}

pressKey(supportedKeys[CommandLine.arguments[1]]!)

 

Now go back to the Remote Trigger and set “Argument” to “Text”. In the text box, write one of the options at the top of the code, like fast-forward or play-pause.

 

Duplicate the Remote Trigger and change the Text for each action you want. They can all connect to the same Run Script.

 

The first time you run it, macOS may ask you to install the command-line developer tools. Let it (takes two clicks), and it’ll work from then on.

Link to comment
5 hours ago, vitor said:

Welcome @avichou,

 

First, add your Remote Trigger. Connect it to a Run Script Action with language set to /usr/bin/swift and the following Script:

 

import Quartz

let supportedKeys: [String: UInt32] = [
  "screen-brightness-up": 2,
  "screen-brightness-down": 3,
  "keyboard-brightness-up": 21,
  "keyboard-brightness-down": 22,
  "play-pause": 16,
  "fast-forward": 17,
  "rewind": 18,
  "volume-mute-toggle": 7,
  "volume-up": 0,
  "volume-down": 1,
]

func pressKey(_ key: UInt32) {
  func keyDown(_ down: Bool) {
    let flags = NSEvent.ModifierFlags(rawValue: (down ? 0xa00 : 0xb00))
    let data1 = Int((key << 16) | (down ? 0xa00 : 0xb00))

    let ev = NSEvent.otherEvent(
      with: NSEvent.EventType.systemDefined,
      location: NSPoint(x: 0, y: 0),
      modifierFlags: flags,
      timestamp: 0,
      windowNumber: 0,
      context: nil,
      subtype: 8,
      data1: data1,
      data2: -1
    )

    let cev = ev?.cgEvent
    cev?.post(tap: CGEventTapLocation.cgSessionEventTap)
  }

  keyDown(true)
  keyDown(false)
}

pressKey(supportedKeys[CommandLine.arguments[1]]!)

 

Now go back to the Remote Trigger and set “Argument” to “Text”. In the text box, write one of the options at the top of the code, like fast-forward or play-pause.

 

Duplicate the Remote Trigger and change the Text for each action you want. They can all connect to the same Run Script.

 

The first time you run it, macOS may ask you to install the command-line developer tools. Let it (takes two clicks), and it’ll work from then on.

Thanks a lot but I really have no knowledge about that ...i really don't know what to do ahha

Where is the remote trigger option so I can create a new one ?

Link to comment
1 minute ago, vitor said:

You have a . there, that’s not part of the name.

Ahaha I just came to tell you that 

Thanks a lot 

Works wonders ! 

And I can use any other command,such as increase display brightness or anything ? So cool !

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...