Class: AppleScript
- Inherits:
-
Object
- Object
- AppleScript
- Defined in:
- lib/applescript.rb
Class Method Summary collapse
- .choose(text = "Choose from the following", choices = [], default = nil) ⇒ Object
- .choose_file(text = "Choose a file", type = nil) ⇒ Object
- .execute(script) ⇒ Object
- .gets(text) ⇒ Object
- .puts(text) ⇒ Object
- .say(text) ⇒ Object
Class Method Details
.choose(text = "Choose from the following", choices = [], default = nil) ⇒ Object
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/applescript.rb', line 60 def choose(text="Choose from the following", choices=[], default=nil) default ||= choices[0] execute(%{ tell application "Finder" activate set choiceList to {#{choices.collect{|c| '"'+c+'"'}.join(",")}} choose from list choiceList with prompt "#{text.gsub('"', '\"')}"#{%{ default items "#{default}"} if default} end tell }).choice end |
.choose_file(text = "Choose a file", type = nil) ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/applescript.rb', line 71 def choose_file(text="Choose a file", type=nil) execute(%{ tell application "Finder" activate choose file with prompt "#{text.gsub('"', '\"')}" #{%!of type {"#{type}"}! if type} end tell }).unix_path end |
.execute(script) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/applescript.rb', line 25 def execute(script) osascript = `which osascript` if not osascript.empty? and File.executable?(osascript) raise AppleScriptError, "osascript not found, make sure it is in the path" else result = `osascript -e "#{script.gsub('"', '\"')}" 2>&1` if result =~ /execution error/ raise AppleScriptError, result end AppleScriptString.new(result) end end |
.gets(text) ⇒ Object
42 43 44 45 46 47 48 49 |
# File 'lib/applescript.rb', line 42 def gets(text) execute(%{ tell application "Finder" activate display dialog "#{text.gsub('"', '\"')}" default answer "" buttons {"Cancel", "OK"} default button "OK" end tell }).answer end |
.puts(text) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/applescript.rb', line 51 def puts(text) execute(%{ tell application "Finder" activate display dialog "#{text.gsub('"', '\"')}" buttons {"OK"} default button "OK" end tell }).answer end |
.say(text) ⇒ Object
38 39 40 |
# File 'lib/applescript.rb', line 38 def say(text) execute(%{say "#{text.gsub('"', '\"')}"}) end |