Question

I'm trying to get a list of running processes on OS X through Ruby's ScriptingBridge.

Everything works fine, except getting the process id. The problem seems to be that Ruby's internal Object#id property is called instead of the SystemEvents.process.id selector.

This is my current sample code:

#!/usr/bin/env ruby

# Lists all active processes

require 'osx/cocoa'
include OSX
OSX.require_framework 'ScriptingBridge'

app = SBApplication.applicationWithBundleIdentifier_("com.apple.SystemEvents")
procs = app.processes

procs.each do |x|
    puts "Process No. #{x.id}: #{x.name}"
end

This is (part of) it's output:

merlin:mw ~/> /Users/mw/Projekte/Ruby/winlist.rb 
/Users/mw/Projekte/Python/winlist.rb:13: warning: Object#id will be deprecated; use Object#object_id
Process No. 2275604960: loginwindow
/Users/mw/Projekte/Python/winlist.rb:13: warning: Object#id will be deprecated; use Object#object_id
Process No. 2275603460: talagent
/Users/mw/Projekte/Python/winlist.rb:13: warning: Object#id will be deprecated; use Object#object_id
Process No. 2275600720: Dock
[... snipped list of all my processes ...]

How can I make sure the ScriptingBridge is called, not Object#id?

Was it helpful?

Solution

  1. Scripting Bridge is flawed and often prone to application incompatibilities and other problems. Consider using AppleScript (either directly or via NSAppleScript, osascript, or AppleScriptObjC) instead.

  2. If you're using Cocoa or other system APIs to get information from System Events.app, you're doing it wrong. SE is just an AppleScript wrapper around those same APIs. In this case, use NSWorkspace:

    #!/usr/bin/env ruby
    
    # Lists all active processes
    
    require 'osx/cocoa'
    include OSX
    
    procs = NSWorkspace.sharedWorkspace.runningApplications
    
    procs.each do |x|
        puts "Process No. #{x.processIdentifier} Name. #{x.localizedName}"
    end
    
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top