Guest User

Untitled

a guest
Feb 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. require "escape.rb"
  2. module CocaDialog
  3. module_function
  4.  
  5. def request_string(hash = Hash.new,&block)
  6. options = self.default_hash
  7. options["type"] = "inputbox"
  8. options["title"] = hash[:title] || "Enter String"
  9. options["informative-text"] = hash[:prompt] || ""
  10. options["text"] = hash[:default] || ""
  11. return self.dialog(options,&block)
  12. end
  13. def request_secure_string(hash = Hash.new,&block)
  14. options = self.default_hash
  15. options["type"] = "secure-inputbox"
  16. options["title"] = hash[:title] || "Enter Password"
  17. options["informative-text"] = hash[:prompt] || ""
  18. options["text"] = hash[:default] || ""
  19. return self.dialog(options,&block)
  20. end
  21. def drop_down(hash = Hash.new,&block)
  22. items = hash[:items] || []
  23. if items.empty? then
  24. block_given? raise SystemExit : return nil
  25. elsif items.length == 1 then
  26. block_given? yield items[0] : return items[0]
  27. else
  28. options = self.default_hash
  29. options["type"] = "dropdown"
  30. options["title"] = hash[:title] || "Select Item"
  31. options["text"] = hash[:prompt] || ""
  32. return self.dialog(options,&block)
  33. end
  34. end
  35.  
  36. private
  37.  
  38. def dialog(options)
  39. type = options.delete("type")
  40. str = ""
  41. options.each_pair do |key, value|
  42. str << " --#{e_sh key} "
  43. str << Array(value).map { |s| e_sh s }.join(" ")
  44. end
  45. cd = ENV['TM_SUPPORT_PATH'] + '/bin/CocoaDialog.app/Contents/MacOS/CocoaDialog'
  46. result = %x{#{e_sh cd} 2>/dev/console #{e_sh type} #{str} --float}
  47. return_value, result = result.to_a.map{|line| line.chomp}
  48. if return_value == "Cancel" then
  49. if block_given? then
  50. raise SystemExit
  51. else
  52. return nil
  53. end
  54. else
  55. block_given? ? yield result : result
  56. end
  57. end
  58. def default_hash
  59. return {
  60. "string-output" => "",
  61. "button1" => "Ok",
  62. "button2" => "Cancel"
  63. }
  64. end
  65. end
Add Comment
Please, Sign In to add comment