applescript 1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/applescript.rb +98 -0
  2. metadata +45 -0
@@ -0,0 +1,98 @@
1
+ =begin
2
+ name = AppleScript.gets("What's your name?")
3
+
4
+ AppleScript.puts("Thank you")
5
+
6
+ choice = AppleScript.choose("Which one of these is your name?", ["Leonard", "Mike", "Lucas", name])
7
+
8
+ if name == choice
9
+ AppleScript.say "You are right"
10
+ picture = AppleScript.choose_file("Find a picture of yourself")
11
+
12
+ if File.exists?(picture)
13
+ AppleScript.say "Thanks, I will now post it on Flickr for you"
14
+ # Exercise for the reader, post it on flickr
15
+ end
16
+ else
17
+ AppleScript.say "You are wrong, your name is #{name}"
18
+ end
19
+ =end
20
+
21
+ class AppleScriptError < StandardError; end
22
+
23
+ class AppleScript
24
+ class << self
25
+ def execute(script)
26
+ osascript = `which osascript`
27
+ if not osascript.empty? and File.executable?(osascript)
28
+ raise AppleScriptError, "osascript not found, make sure it is in the path"
29
+ else
30
+ result = `osascript -e "#{script.gsub('"', '\"')}" 2>&1`
31
+ if result =~ /execution error/
32
+ raise AppleScriptError, result
33
+ end
34
+ AppleScriptString.new(result)
35
+ end
36
+ end
37
+
38
+ def say(text)
39
+ execute(%{say "#{text.gsub('"', '\"')}"})
40
+ end
41
+
42
+ def gets(text)
43
+ execute(%{
44
+ tell application "Finder"
45
+ activate
46
+ display dialog "#{text.gsub('"', '\"')}" default answer "" buttons {"Cancel", "OK"} default button "OK"
47
+ end tell
48
+ }).answer
49
+ end
50
+
51
+ def puts(text)
52
+ execute(%{
53
+ tell application "Finder"
54
+ activate
55
+ display dialog "#{text.gsub('"', '\"')}" buttons {"OK"} default button "OK"
56
+ end tell
57
+ }).answer
58
+ end
59
+
60
+ def choose(text="Choose from the following", choices=[], default=nil)
61
+ default ||= choices[0]
62
+ execute(%{
63
+ tell application "Finder"
64
+ activate
65
+ set choiceList to {#{choices.collect{|c| '"'+c+'"'}.join(",")}}
66
+ choose from list choiceList with prompt "#{text.gsub('"', '\"')}"#{%{ default items "#{default}"} if default}
67
+ end tell
68
+ }).choice
69
+ end
70
+
71
+ def choose_file(text="Choose a file", type=nil)
72
+ execute(%{
73
+ tell application "Finder"
74
+ activate
75
+ choose file with prompt "#{text.gsub('"', '\"')}" #{%!of type {"#{type}"}! if type}
76
+ end tell
77
+ }).unix_path
78
+ end
79
+ end
80
+ end
81
+
82
+ class AppleScriptString < String
83
+ def unix_path
84
+ gsub(/^(file|alias).*?:/,"/").gsub(/:/,"/").chomp
85
+ end
86
+
87
+ def button
88
+ gsub(/(.*?)(button returned:)(.*?)($|,.*)/, "\\3").chomp
89
+ end
90
+
91
+ def answer
92
+ gsub(/(.*?)(text returned:)(.*?)($|,.*)/, "\\3").chomp
93
+ end
94
+
95
+ def choice
96
+ gsub(/(.*?)(\{"*?)(.*?)("*?\}.*)/, "\\3").chomp
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: applescript
5
+ version: !ruby/object:Gem::Version
6
+ version: "1.0"
7
+ date: 2006-03-03 00:00:00 -08:00
8
+ summary: A simple AppleScript wrapper for Ruby.
9
+ require_paths:
10
+ - lib
11
+ email: lucas@rufy.com
12
+ homepage: http://rubyforge.org/projects/applescript/
13
+ rubyforge_project:
14
+ description: A simple AppleScript wrapper for Ruby.
15
+ autorequire: applescript
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Lucas Carlson
30
+ files:
31
+ - lib/applescript.rb
32
+ test_files: []
33
+
34
+ rdoc_options: []
35
+
36
+ extra_rdoc_files: []
37
+
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ requirements: []
43
+
44
+ dependencies: []
45
+