Alfred Workflow - Obtain App ID Quickly

Sometimes, retrieving an app’s ID or version information on a Mac can be inefficient if you manually check and copy it via Finder. Therefore, I used Alfred’s Universal action to achieve a one-click retrieval.

Screenshot

Pressing Enter copies it directly to the clipboard.

Principle

  1. AppleScript can quickly obtain the version/ID information of a specified app.

    1
    2
    set appId to name of application "/Users/alanhe/Applications/JetBrains Toolbox/WebStorm.app"
    copy appId to stdout

    You can find out what specific attributes can be obtained here.

  2. MacOS comes with a Ruby/Python environment, so both can be used to read apps in the Applications directory.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    require 'json'
    require 'open3'
    require 'pathname'

    apps = Pathname('/Applications').children
    .concat(Pathname('/Applications/Utilities').children)
    .map(&:to_path)
    .sort_by { |p| File.basename(p).downcase }
    .select { |e| e.end_with?('.app') }

    script_filter_items = apps.map do |app|
    app_id_command = "osascript -e 'id of application \"#{app}\"'"
    app_version_command = "osascript -e 'version of application \"#{app}\"'"
    id_output, id_status = Open3.capture2(app_id_command)
    version_output, version_status = Open3.capture2(app_version_command)
    {
    title: File.basename(app, '.app'),
    subtitle: "ID: #{id_output.strip}, Version: #{version_output.strip}",
    arg: id_output.strip,
    }
    end

    puts JSON.generate({ items: script_filter_items })
  3. Combine Alfred’s workflow to achieve the effect.

Steps

  1. Create a new Alfred workflow.

  2. Add a “Universal Action” input to receive the selected application path.

  3. Add a “Run Script” action to process the selected app using Ruby or Python.

  4. Use clipboard action to copy the result to the clipboard.

1
2
3
4
5
6
7
8
import subprocess

def get_app_id(path):
command = f"osascript -e 'id of application \"{path}\"'"
result = subprocess.run(command, shell=True, capture_output=True, text=True)
return result.stdout.strip()

print(get_app_id("/Applications/Safari.app"))
  1. Test and optimize as needed.

At the end

By using Alfred’s powerful workflow and macOS scripting capabilities, you can streamline the process of retrieving application details like App ID and version. This saves time and ensures accuracy and consistency in handling app information.