/
cocoa-app.rb
67 lines (52 loc) · 1.8 KB
/
cocoa-app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env ruby -wKU
#
# A quick guide to runtime name mangling:
#
# ObjC becomes Ruby
# [ obj method ] obj.method
# [ obj method: arg ] obj.method(arg)
# [ obj method: arg1 withOtherArgs: arg2 ] obj.method_withOtherArgs( arg1, arg2 )
require 'osx/cocoa'
include OSX
class AppDelegate < NSObject
def applicationDidFinishLaunching(aNotification)
puts "Hello, World!"
end
def sayHello(sender)
puts "Hello again, World!"
speak "Hello again, World!"
end
def speak(str)
script = NSAppleScript.alloc.initWithSource("say \"#{str}\"")
script.executeAndReturnError(nil)
end
end
if $0 == __FILE__ then
$stderr.print "just wait..." ; $stderr.flush
app = NSApplication.sharedApplication()
app.setDelegate(AppDelegate.alloc.init)
frame = [200.0, 300.0, 250.0, 100.0]
win = NSWindow.alloc.initWithContentRect_styleMask_backing_defer(frame, 15, 2, 0)
win.setTitle 'HelloWorld'
win.setLevel(3) # floating window
hel = NSButton.alloc.initWithFrame [10.0, 10.0, 80.0, 80.0]
win.contentView.addSubview(hel)
hel.setBezelStyle(4)
hel.setTitle( 'Hello!' )
hel.setTarget( app.delegate )
hel.setAction( "sayHello:" )
# beep = NSSound.alloc.initWithContentsOfFile_byReference( '/System/Library/Sounds/Tink.Aiff', 1 )
# hel.setSound( beep )
bye = NSButton.alloc.initWithFrame [100.0, 10.0, 80.0, 80.0]
win.contentView.addSubview(bye)
bye.setBezelStyle( 4 )
bye.setTarget(app)
bye.setAction('stop:')
bye.setEnabled( true )
bye.setTitle( 'Goodbye!' )
adios = NSSound.alloc.initWithContentsOfFile_byReference( '/System/Library/Sounds/Basso.aiff', true )
bye.setSound( adios )
win.display()
win.orderFrontRegardless() ## but this one does
app.run()
end