Fun with Ruby and Cocoa

  • View
    3.467

  • Download
    2

Embed Size (px)

DESCRIPTION

Use RubyCocoa and MacRuby to write scripts that take advantage of the Cocoa frameworks.

Text of Fun with Ruby and Cocoa

  • 1.Ruby and CocoaFacilitate development on OSX

2. About me 3. About me 4. Rapperswil 5. http://www.ickr.com/photos/turtlemom_nancy/4026208166/sizes/l/ 6. huesler informatik 7. huesler informatikupstream agile 8. huesler informatikupstream agileco-up.de 9. Ruby and Cocoa 10. Ruby 11. passionate http://www.ickr.com/photos/gi/378823/sizes/o/ 12. Apple OSX 13. passionate http://www.ickr.com/photos/gi/378823/sizes/o/ 14. Landscapehttp://www.ickr.com/photos/kiumo/4203883504/sizes/o/ 15. Cocoa http://www.ickr.com/photos/luder5/4100921399/sizes/l/ 16. Cocoa 17. Cocoa Core Foundation 18. Cocoa Core Foundation Appkit 19. Cocoa Core Foundation Appkit Core * (Audio/Video/Image/Data etc.) 20. Cocoa Core Foundation Appkit Core * (Audio/Video/Image/Data etc.) Scripting bridge 21. Cocoa Core Foundation Appkit Core * (Audio/Video/Image/Data etc.) Scripting bridge others 22. Interface Builder 23. XCode 24. Objective-C 25. NSMutableArray *anArray = [ [NSMutableArray alloc] init ]; [anArray addObject:@"Element 1"]; [anArray addObject:@"Element 2"]; [anArray addObject:@"Element 3"];//Use a for each loop to iterate through the array for (NSString *s in anArray) {NSLog(s); } //Release the array [anArray release] 26. Ruby And Cocoa 27. Ruby Cocoa 28. HotCocoa picture shamelessly cropped from http://www.slideshare.net/mattetti/macruby-hotcocoa-presentation-by-rich-kilmer 29. Scripting Bridge http://www.ickr.com/photos/bensonkua/2851908095/sizes/l/ 30. Good to know 31. Good to know Available commands for each application are in a .sdef le 32. Good to know Available commands for each application are in a .sdef le Terminal.app/Contents/Resources/ Terminal.sdef 33. Good to know Available commands for each application are in a .sdef le Terminal.app/Contents/Resources/ Terminal.sdef gem rb-appscript (native extension so not for MacRuby) 34. 1 3 4 6 9 10 11 12 13 16 17 18 19 20 21 23 24 35. Control Terminal 36. 1 #!/usr/bin/env ruby 2 require 'rubygems' 3 require 'appscript' 4 include Appscript 5 6 terminal = app('Terminal') 7 current_window = terminal.windows.first 8 current_tab = current_window.tabs.last 9 process = app("System Events").application_processes[10 "Terminal.app"11 ]1213 process.keystroke('t', :using => :command_down)14 terminal.do_script('top', :in => current_tab) 37. 1 #!/usr/bin/env ruby 2 require 'rubygems' 3 require 'appscript' 4 include Appscript 5 6 terminal = app('Terminal') 7 current_window = terminal.windows.first 8 current_tab = current_window.tabs.last 9 process = app("System Events").application_processes[10 "Terminal.app"11 ]1213 process.keystroke('t', :using => :command_down)14 terminal.do_script('top', :in => current_tab) 38. 1 #!/usr/bin/env ruby 2 require 'rubygems' 3 require 'appscript' 4 include Appscript 5 6 terminal = app('Terminal') 7 current_window = terminal.windows.first 8 current_tab = current_window.tabs.last 9 process = app("System Events").application_processes[10 "Terminal.app"11 ]1213 process.keystroke('t', :using => :command_down)14 terminal.do_script('top', :in => current_tab) 39. 7 current_window = terminal.windows.first 8 current_tab = current_window.tabs.last 9 process = app("System Events").application_processes[10 "Terminal.app"11 ]1213 process.keystroke('t', :using => :command_down)14 terminal.do_script('top', :in => current_tab) 40. Problems with MacRuby 41. Problems with MacRuby 42. Problems with MacRuby Some methods dont seem to be available 43. iTunes 44. 1 #!/usr/bin/env macruby 2 3 framework 'ScriptingBridge' 4 5 itunes = SBApplication.applicationWithBundleIdentifier( 6 "com.apple.iTunes" 7 ) 8 library = itunes.sources.objectWithName ("Library") 910 library.userPlaylists.each do |playlist|11 puts playlist.name12 end 45. 2 3 framework 'ScriptingBridge' 4 5 itunes = SBApplication.applicationWithBundleIdentifier( 6 "com.apple.iTunes" 7 ) 8 library = itunes.sources.objectWithName ("Library") 910 library.userPlaylists.each do |playlist|11 puts playlist.name12 end 46. 2 3 framework 'ScriptingBridge' 4 5 itunes = SBApplication.applicationWithBundleIdentifier( 6 "com.apple.iTunes" 7 ) 8 library = itunes.sources.objectWithName ("Library") 910 library.userPlaylists.each do |playlist|11 puts playlist.name12 end 47. Core Location 48. 1 #!/usr/bin/env macruby2 framework 'CoreLocation'34 loc = CLLocationManager.alloc.init5 loc.delegate = self6 loc.startUpdatingLocation78 # keep the script running9 NSRunLoop.currentRunLoop.runUntilDate( 10 NSDate.distantFuture 11 ) 49. 1 #!/usr/bin/env macruby2 framework 'CoreLocation'34 loc = CLLocationManager.alloc.init5 loc.delegate = self6 loc.startUpdatingLocation78 # keep the script running9 NSRunLoop.currentRunLoop.runUntilDate( 10 NSDate.distantFuture 11 ) 50. 1 def locationManager( 2 manager, 3 didUpdateToLocation: new_location, 4 fromLocation: old_location 5 ) 6 7 puts "loc: #{new_location.description}" 8 end 51. 2 framework 'CoreLocation'34 loc = CLLocationManager.alloc.init5 loc.delegate = self6 loc.startUpdatingLocation78 # keep the script running9 NSRunLoop.currentRunLoop.runUntilDate( 10 NSDate.distantFuture 11 ) 52. 1 #!/usr/bin/env macruby2 framework 'CoreLocation'34 def locationManager(manager,5didUpdateToLocation: new_location,6fromLocation: old_location7)8 puts "location: #{new_location.description}"9 end 10 11 loc = CLLocationManager.alloc.init 12 loc.delegate = self 13 loc.startUpdatingLocation 14 15 # keep the script running 16 NSRunLoop.currentRunLoop.runUntilDate( 17 NSDate.distantFuture 18 ) 53. Grand Central Dispatch 54. Grand Central Dispatch 55. Grand Central Dispatch MacRuby only 56. Grand Central Dispatch MacRuby only Synchronous 57. Grand Central Dispatch MacRuby only Synchronous Asynchronous 58. Grand Central Dispatch MacRuby only Synchronous Asynchronous Parallel 59. Grand Central Dispatch MacRuby only Synchronous Asynchronous Parallel Synchronization 60. Asynchronous 61. 1 #!/usr/bin/env macruby23 queue = Dispatch::Queue.new(4 'ch.huesler-informatik.scotrubyconf.gcd'5 )67 queue.async do8 puts 'Starting asyn. NONE BLOCKING!'9 sleep 2.00 10 puts "Finished asyn" 11 end 12 puts "code not being blocked" 13 14 NSRunLoop.currentRunLoop.runUntilDate( 15 NSDate.distantFuture 16 ) 17 62. 1 #!/usr/bin/env macruby23 queue = Dispatch::Queue.new(4 'ch.huesler-informatik.scotrubyconf.gcd'5 )67 queue.async do8 puts 'Starting asyn. NONE BLOCKING!'9 sleep 2.00 10 puts "Finished asyn" 11 end 12 puts "code not being blocked" 13 14 NSRunLoop.currentRunLoop.runUntilDate( 15 NSDate.distantFuture 16 ) 17 63. 7 queue.async do8 puts 'Starting asyn. NONE BLOCKING!'9 sleep 2.00 10 puts "Finished asyn" 11 end 12 puts "code not being blocked" 13 14 NSRunLoop.currentRunLoop.runUntilDate( 15 NSDate.distantFuture 16 ) 17 64. Synchronous 65. 1 #!/usr/bin/env macruby23 queue = Dispatch::Queue.new(4 'ch.huesler-informatik.scotrubyconf.gcd'5 )67 queue.sync do8 puts 'Starting sync. BLOCKING!'9 sleep 3.0 10 puts 'Finished sync' 11 end 12 puts "code being blocked" 13 14 NSRunLoop.currentRunLoop.runUntilDate( 15 NSDate.distantFuture 16 ) 17 66. Synchronized 67. 1 #!/usr/bin/env macruby23 worker_queue = Dispatch::Queue.new(4 'ch.huesler-informatik.scotrubyconf.gcd'5 )6 group = Dispatch::Group.new78 0.upto(10) do |i|9 puts "Dispatch #{i} to GCD" 10 worker_queue.async(group) do 11 puts "working on #{i}" 12 end 13 end 14 puts "waiting for gcd" 15 group.wait 16 puts "done" 17 68. 1 #!/usr/bin/env macruby23 worker_queue = Dispatch::Queue.new(4 'ch.huesler-informatik.scotrubyconf.gcd'5 )6 group = Dispatch::Group.new78 0.upto(10) do |i|9 puts "Dispatch #{i} to GCD" 10 worker_queue.async(group) do 11 puts "working on #{i}" 12 end 13 end 14 puts "waiting for gcd" 15 group.wait 16 puts "done" 17 69. 78 0.upto(10) do |i|9 puts "Dispatch #{i} to GCD" 10 worker_queue.async(group) do 11 puts "working on #{i}" 12 end 13 end 14 puts "waiting for gcd" 15 group.wait 16 puts "done" 17 70. Concurrent 71. 1 #!/usr/bin/env macruby23 group = Dispatch::Group.new4 result = []5 1.upto(10).each do |i|6 Dispatch::Queue.concurrent.async(group) do7 sleep 28 result