Q: Question about Applescript display dialog
hello to everyone, can i do a question?
i would know if anyone know how i can set the size of the dialog display in this code :
set listaNomi to {"ACERBI", "AGOSTINI", "ALBERTAZZI"}
repeat (count of listaNomi) times
set nomeEstratto to some item of listaNomi
display dialog nomeEstratto
set listaNomi to removeFromList(nomeEstratto, listaNomi)
end repeat
on removeFromList(theItem, aList)
set newList to {}
repeat with x in aList
if x as string is not equal to theItem as string then
set newList to newList & x
end if
end repeat
return newList
end removeFromList
Well i want set the height and weight of the dialog display, and make it much big
anyone help me? please
MacBook (13-inch Mid 2010), OS X Yosemite (10.10.4)
Posted on Jul 29, 2015 10:30 AM
Hello
You may replace the display dialog command with cocoa_dialogue() handler as listed below.
Here I have changed the TEXT_SIZE constant in ruby code to 72.0, which makes the text much larger as to be suitable for short string such as person's name in your case. You can change it as you see fit.
Also you can change the text colour by changing this statement -
@tf.setTextColor(NSColor.brownColor)
to, for example -
@tf.setTextColor(NSColor.blackColor)
etc.
* I made very minor revision to the ruby code, that is a) to have removed MARGIN_T constant which was a residue of a test version and b) to have let the help being output to stderr. These changes do not affect the functions.
Hope this may help,
H
set listaNomi to {"ACERBI", "AGOSTINI", "ALBERTAZZI"}
repeat (count of listaNomi) times
set nomeEstratto to some item of listaNomi
--display dialog nomeEstratto
cocoa_dialogue(nomeEstratto, {|:size|:{600, 300}, |:title|:""})
set listaNomi to removeFromList(nomeEstratto, listaNomi)
end repeat
on removeFromList(theItem, aList)
set newList to {}
repeat with x in aList
set x to x's contents
if x ≠ theItem then
set newList's end to x
end if
end repeat
return newList
end removeFromList
on cocoa_dialogue(t, opts)
(*
string t : text to display
record opts : {|:size|:sz, |:title|:ti}
list sz : { (integer) width, (integer) height } of dialogue window, measured in points; default = {600, 300}
string ti : title of window; default = ""
return string : name of button pressed (OK or Cancel)
*)
set opts to opts & {|:size|:{600, 300}, |:title|:""}
set {{w, h}, ti} to opts's {|:size|, |:title|}
set args to ""
repeat with a in {"-t", ti, "-s", w & "," & h, "-e", t}
set args to args & ("" & a)'s quoted form & space
end repeat
do shell script "/usr/bin/ruby -w <<'EOF' - " & args & "
#
# v0.10
# written by Hiroto, 2015-07
require 'osx/cocoa'
include OSX
MARGIN_B = 15.0
MARGIN_R = 15.0
BUTTON_MIN_H = 30.0
BUTTON_MIN_W = 90.0
BUTTON_TITLE_H_MARGIN = 16.0
BUTTON_TITLE_SIZE = 14.0
BUTTON_TITLE_OK = 'OK'
BUTTON_TITLE_CANCEL = 'Cancel'
TEXT_SIZE = 72.0
class AppDelegate < NSObject
def initWithArguments(args)
@title, @size, @text = args.values_at(:title, :size, :text)
@win = NSWindow.alloc.objc_send(
:initWithContentRect, NSMakeRect(0, 0, *@size),
:styleMask, NSTitledWindowMask | NSMiniaturizableWindowMask, # | NSClosableWindowMask | NSResizableWindowMask,
:backing, NSBackingStoreBuffered,
:defer, false)
@win.setLevel(NSStatusWindowLevel)
@win.setTitle(@title)
@win.center
# OK button
@bt1 = NSButton.alloc.initWithFrame(NSZeroRect)
@bt1.setTitle(BUTTON_TITLE_OK)
@bt1.setFont(NSFont.systemFontOfSize(BUTTON_TITLE_SIZE))
@bt1.setKeyEquivalent(%[\\r]) # U+000D CARRIAGE RETURN
@bt1.setButtonType(NSMomentaryLightButton)
@bt1.setTarget(self)
@bt1.setAction(:bttnPressed_)
@bt1.setBezelStyle(NSRoundedBezelStyle)
@bt1.setFrameSize(
NSMakeSize(
[@bt1.attributedTitle.size.width + BUTTON_TITLE_H_MARGIN * 2, BUTTON_MIN_W].max,
BUTTON_MIN_H
)
)
@bt1.setFrameOrigin(
NSMakePoint(
@win.contentView.frame.size.width - @bt1.frame.size.width - MARGIN_R,
MARGIN_B
)
)
# Cancel button
@bt2 = NSButton.alloc.initWithFrame(NSZeroRect)
@bt2.setTitle(BUTTON_TITLE_CANCEL)
@bt2.setFont(NSFont.systemFontOfSize(BUTTON_TITLE_SIZE))
@bt2.setKeyEquivalent(%[\\x1b]) # U+001B ESCAPE
@bt1.setButtonType(NSMomentaryLightButton)
@bt2.setTarget(self)
@bt2.setAction(:bttnPressed_)
@bt2.setBezelStyle(NSRoundedBezelStyle)
@bt2.setFrameSize(
NSMakeSize(
[@bt2.attributedTitle.size.width + BUTTON_TITLE_H_MARGIN * 2, BUTTON_MIN_W].max,
BUTTON_MIN_H
)
)
@bt2.setFrameOrigin(
NSMakePoint(
@win.contentView.frame.size.width - @bt2.frame.size.width - MARGIN_R - @bt1.frame.size.width,
MARGIN_B
)
)
# text field
@tf = NSTextField.alloc.initWithFrame(NSZeroRect)
@tf.setBordered(false)
@tf.setDrawsBackground(false)
@tf.setSelectable(false)
@tf.setStringValue(@text.chomp)
@tf.setTextColor(NSColor.brownColor)
@tf.setFont(NSFont.systemFontOfSize(TEXT_SIZE))
@tf.sizeToFit
@tf.setFrameOrigin(
NSMakePoint(
(@win.contentView.frameSize.width - @tf.frameSize.width) / 2.0,
(@win.contentView.frameSize.height - @tf.frameSize.height + @bt1.frameSize.height + MARGIN_B) / 2.0
)
)
@win.contentView.addSubview(@tf)
@win.contentView.addSubview(@bt1)
@win.contentView.addSubview(@bt2)
@win.makeFirstResponder(@bt1)
self
end
def applicationDidFinishLaunching(notif)
begin
if NSApp.respondsToSelector?('setActivationPolicy_')
NSApp.setActivationPolicy(NSApplicationActivationPolicyRegular) # OS X 10.6 or later
end
NSApp.activateIgnoringOtherApps(true)
@win.orderFrontRegardless
@win.displayIfNeeded
rescue
NSApp.terminate(self)
raise
end
end
def applicationShouldTerminateAfterLastWindowClosed(app)
true
end
def applicationShouldTerminate(sender)
NSTerminateNow
end
def bttnPressed(sender)
puts sender.title
NSApp.terminate(self)
end
end
def parse_options(argv)
require 'optparse'
args = {}
op = OptionParser.new do |o|
o.banner = %Q[Usage: #{File.basename($0)} [options]]
# default values
args[:title] = nil # dialogue window title
args[:size] = [600,300] # [width, height] of dialogue window; unit = point
args[:text] = nil # text to be displayed
o.on( '-t', '--title [TITLE]', String, 'Window title.' ) do |t|
args[:title] = t
end
o.on( '-s', '--size [W,H]', Array, 'Dialogue width and height [point].' ) do |s|
raise OptionParser::InvalidArgument, %Q[#{s.join(',')}] unless s.length == 2
args[:size] = s.map {|x| x.to_f}
end
o.on( '-e', '--text [TEXT]', String, 'Text to be displayed.' ) do |e|
args[:text] = e
end
o.on( '-h', '--help', 'Display this help.' ) do
$stderr.puts o
exit 1
end
end
begin
op.parse!(argv)
rescue => ex
$stderr.puts %Q[#{ex.class} : #{ex.message}]
$stderr.puts op.help()
exit 1
end
args[:text] = '' unless args[:text]
args[:title] = '' unless args[:title]
$stderr.puts 'Extra argument is ignored : %s' % argv.join(' ') if argv.length > 0
args
end
def main(argv)
args = parse_options(argv)
app = NSApplication.sharedApplication
delegate = AppDelegate.alloc.initWithArguments(args)
app.setDelegate(delegate)
app.run
end
main(ARGV)
EOF"
end cocoa_dialogue
Posted on Aug 1, 2015 11:01 AM


