Jump to content

Reveal Universal Actions from the Terminal


Recommended Posts

Sometimes when working in a Terminal you may want to interact with files via a workflow. Alfred supports AppleScript, so an External Trigger is the perfect solution for something you use often. But what if you want quick access to all available Universal Actions? That’s easy too. Add the following to your ~/.zshrc, and you’ll be able to trigger the Universal Actions window from a Terminal by executing ua followed by the desired file paths.

 

function ua {
  # Verify all paths are valid
  for file_path in "${@}"
  do
    if [[ ! -e "${file_path}" ]]
    then
      echo "Path does not exist: ${file_path}" >&2
      return 1
    fi
  done

  # Expand arguments into full paths
  local -r absolute_paths=("${(@f)$(realpath "${@}")}")

  # Open Universal Actions
  /usr/bin/osascript -l JavaScript -e 'function run(argv) { Application("com.runningwithcrayons.Alfred").action(argv) }' "${absolute_paths[@]}"
}

 

A couple of notes:

  • The function is Zsh-specific. It is possible to do the same in Bash, but the focus above is on macOS’ default.
  • It requires macOS Ventura or above. Again, it is possible to make it work on older versions, but it would be more verbose.
  • Most of the code is checking if paths exist. That’s not strictly necessary and you can make it significantly shorter by only including the local -r and /usr/bin/osascript lines.

Link to comment
Share on other sites

  • vitor locked this topic
Guest
This topic is now closed to further replies.
×
×
  • Create New...