Alfred实现一键获取AppID等信息

· 1 min read

有时想获取Mac下App的ID/版本等信息,finder中来回查看拷贝低效,因此利用Alfred Universal action实现下一键获取。

效果

回车即拷贝至剪贴板

原理

  1. AppleScript可以方便获取指定App的版本/ID信息

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

​ 具体可以获取哪些属性查看这里

  1. Mac自带Ruby/Python环境,因此都可以读取/Applications下App程序

    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.each do |app|
      version = Open3.capture2('mdls', '-raw', '-name', 'kMDItemVersion', app).first
      next if version == '(null)'
      name = File.basename(app, '.app')
      script_filter_items.push(title: name, subtitle: version, arg: version, icon: { type: 'fileicon', path: app })
    end
    
    puts({ items: script_filter_items }.to_json)
    

写在最后

如何,就问你Alfred香不香。