0

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?

Mira Weller
  • 2,406
  • 22
  • 27
  • Maybe upgrade? `Object#id` does not exist anymore in Ruby 1.9 – steenslag May 16 '13 at 20:55
  • I was looking for a more general solution, but as there probably isn't one, I'll definitively give that a try. For the moment, I've just switched over to Python ;-) – Mira Weller May 16 '13 at 22:10

1 Answers1

0
  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
    
foo
  • 3,171
  • 17
  • 18
  • Okay, this doesn't really help me, because NSRunningApplication doesn't seem to have the `windows` property I need as well. I'll just mark it as answered because I solved it otherwise. – Mira Weller May 20 '13 at 10:09