Alfred Workflow - Obtain App ID Quickly

Quickly obtain the app ID and version information, enhancing efficiency on Mac.

Jan 29, 2022 · 2 min read · 276 Words · -Views -Comments · Software & Tools
Alfred Workflow 🇨🇳 阅读中文版
Read with AI

Choose an AI assistant

ChatGPT Open in a new tab with the article link and prompt
Claude Copy the prompt, then open Claude

Open Claude
Trouble reading the article?

If the prompt is missing, copy the full prompt below into your AI assistant. If the AI cannot read the article, paste the visible passage you want to discuss and add your question.

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.

    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.

    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.

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.

Authors
Developer, digital product enthusiast, tinkerer, sharer, open source lover