christian.scalese

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

Close

Q: Question about Applescript display dialog

  • All replies
  • Helpful answers

Previous Page 2 of 3 last Next
  • by Hiroto,

    Hiroto Hiroto Aug 2, 2015 4:35 PM in response to christian.scalese
    Level 5 (7,276 points)
    Aug 2, 2015 4:35 PM in response to christian.scalese

    You're quite welcome. Glad to know it works for you.

     

    Best wishes,

    Hiroto

  • by Hiroto,

    Hiroto Hiroto Aug 2, 2015 4:43 PM in response to VikingOSX
    Level 5 (7,276 points)
    Aug 2, 2015 4:43 PM in response to VikingOSX

    Hello VikingOSX,

     

    I'm not sure at all but since rubycocoa 1.2.0 seems to work fine for original poster christian.scalese whose screenshot indicates OS X 10.10.x, I'd guess there might be something specific to your environment which interferes installation of rubycocoa 1.2.0. Unfortunately I cannot be more specific, though...

     

    Just my thought.

     

    All the best,

    Hiroto

  • by VikingOSX,

    VikingOSX VikingOSX Aug 2, 2015 8:56 PM in response to Hiroto
    Level 7 (20,544 points)
    Mac OS X
    Aug 2, 2015 8:56 PM in response to Hiroto

    Pilot error on this end.

     

    Accidentally tried to install the 10.9 version into Yosemite instead of the 10.10 release of RubyCocoa for Ruby 2.0. Now, your code works fine on OS X 10.9.5 and 10.10.4.

     

    And I managed to get ActiveState Tk/Tcl working with RVM Ruby 2.1.1 on OS X 10.10.4.

     

    Have a good week.

  • by christian.scalese,

    christian.scalese christian.scalese Aug 3, 2015 2:42 AM in response to Hiroto
    Level 1 (0 points)
    Aug 3, 2015 2:42 AM in response to Hiroto

    Hi Hiroto, thank you very much for you time and for the solution...work very well

    so can i ask you another thing?

    can i put an image like a wallpaper in the display dialog?

     

    thanks a lot

  • by christian.scalese,

    christian.scalese christian.scalese Aug 5, 2015 12:30 AM in response to christian.scalese
    Level 1 (0 points)
    Aug 5, 2015 12:30 AM in response to christian.scalese

    hi, sorry i have a big problem, i install the beta of el capitan and now it doesn't work because i can't install ruby cocoa.

    there is same solution to install ruby cocoa on OS X 10.11?

  • by Hiroto,

    Hiroto Hiroto Aug 5, 2015 4:31 AM in response to christian.scalese
    Level 5 (7,276 points)
    Aug 5, 2015 4:31 AM in response to christian.scalese

    Hello

     

    Sorry for late reply. Here's a revised handler which additionally accepts background image file, text colour and text font size.

     

    Currently image is scaled to fit the window's content's frame. If the aspect ratios of the frame and the image are different, image will be distorted.

     

    Briefly tested under OS X 10.6.8.

     

    Good luck,

    H

     

    P.S. As for OS X 10.11 beta, I have no idea... Since it is beta, it is not good idea to discuss it here.

     

     

     

    set image to "/Library/Desktop Pictures/Plants/Petals.jpg"
    set colour to {0.9, 1, 1, 0.7} -- {red, green, blue, alpha}
    
    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, 400}, |:title|:"", |:background_image|:image, |:text_colour|:colour, |:text_size|:80})
        if result = "Cancel" then error number -128
        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, |:background_image|:bg, |:text_size|:tz, |:text_colour|:tc}
                list sz   : {width, height} of dialogue window, measured in points; default = {600, 300}
                string ti : title of window; default = ""
                string bg : POSIX path of background image file; default is none
                number tz : text font size [point]; default = 24
                list tc   : text colour specified by {r, g, b, a} where r, g, b, a in [0, 1]; default = {0, 0, 0, 1} (black)
            return string : name of button pressed (OK or Cancel)
        *)
        set opts to opts & {|:size|:{600, 300}, |:title|:"", |:text_size|:24, |:text_colour|:{0, 0, 0, 1}, |:background_image|:false}
        set {{w, h}, ti, tz, {r, g, b, a}, bg} to opts's {|:size|, |:title|, |:text_size|, |:text_colour|, |:background_image|}
        
        (* convert decimal separator in "w;h" and "r;g;b;a" to period and semicolon to comma so as to
            guarantee "w,h" and "r,g,b,a" arguments are comma-separated numbers with period as decimal separator. *)
        set nn to {"" & w & ";" & h, "" & r & ";" & g & ";" & b & ";" & a}
        repeat with n in nn
            set n's contents to do shell script "sed 's/,/./g; s/;/,/g' <<< " & n's quoted form
        end repeat
        set {sz, tc} to nn
        
        -- build arguments string
        set args to ""
        repeat with a in {{"-s", sz}, {"-t", ti}, {"-c", tc}, {"-p", tz}, {"-e", t}}
            set {k, v} to a's contents
            set args to args & k & space & ("" & v)'s quoted form & space
        end repeat
        if bg ≠ false then set args to args & "-g " & bg's quoted form & space
        
        do shell script "/usr/bin/ruby -w <<'EOF' - " & args & "
    #   
    #   v0.12
    #       - added text-colour option
    #       - added text-size option
    #       - added background-image option
    #   
    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'
    
    class AppDelegate < NSObject
        def initWithArguments(args)
            @title, @size, @text, @text_colour, @text_size, @background_image = args.values_at(
            :title, :size, :text, :text_colour, :text_size, :background_image)
    
            @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.setFont(NSFont.systemFontOfSize(@text_size))
            @tf.setTextColor(
                NSColor.objc_send(
                    :colorWithDeviceRed, @text_colour[0],
                    :green, @text_colour[1],
                    :blue, @text_colour[2],
                    :alpha, @text_colour[3]
                )
            )
            @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
                )
            )
            
            if @background_image
                image = NSImage.alloc.initWithContentsOfURL(NSURL.fileURLWithPath(@background_image))
                @iv = NSImageView.alloc.initWithFrame(@win.contentView.frame)
                if @iv
                    @iv.setImage(image)
                    @iv.setImageAlignment(NSImageAlignCenter)   # NSImageAlignment [1]
                    @iv.setImageScaling(NSScaleToFit)           # NSImageScaling [2]
                    @win.contentView.addSubview(@iv)
                end
                # [1] NSImageAlignment
                #   NSImageAlignLeft
                #   NSImageAlignRight
                #   NSImageAlignCenter
                #   NSImageAlignTop
                #   NSImageAlignBottom
                #   NSImageAlignTopLeft
                #   NSImageAlignTopRight
                #   NSImageAlignBottomLeft
                #   NSImageAlignBottomRight
                # [2] NSImageScaling
                #   NSScaleProportionally
                #   NSScaleToFit
                #   NSScaleNone
            end
            
            @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]]
            args[:title]        = nil       # dialogue window title
            args[:size]         = [600,300] # [width, height] of dialogue window;  unit = point
            args[:text]         = nil       # text to be displayed
            args[:text_colour]  = [0,0,0,1] # [red, green, blue, alpha]; each value in [0,1]
            args[:text_size]    = 24        # text font size, unit = point
            args[:background]   = nil       # background image file
            
            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( '-c', '--text-colour [R,G,B,A]', Array, 'Text colour.' ) do |c|
                raise OptionParser::InvalidArgument, %Q[#{s.join(',')}] unless c.length == 4
                args[:text_colour] = c.map {|x| x.to_f}
            end
            o.on( '-p', '--text-size [POINT]', Float, 'Text font size [point].' ) do |p|
                args[:text_size] = p.to_f
            end
            o.on( '-g', '--background-image [IMAGE]', String, 'Background image file.' ) do |g|
                args[:background_image] = g
            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]
        args[:text_size] = 24 unless args[:text_size]
        $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
    
  • by VikingOSX,

    VikingOSX VikingOSX Aug 5, 2015 5:38 AM in response to christian.scalese
    Level 7 (20,544 points)
    Mac OS X
    Aug 5, 2015 5:38 AM in response to christian.scalese

    RubyCocoa has not been updated (yet) to support El Capitan (10.11).

  • by christian.scalese,

    christian.scalese christian.scalese Aug 6, 2015 5:48 AM in response to Hiroto
    Level 1 (0 points)
    Aug 6, 2015 5:48 AM in response to Hiroto

    thank you, it is amazing but i can't change the color go the text with this image

     

    Schermata 2015-08-06 alle 14.46.26.png

     

    how i can change the color of the text in red?

    thank you

  • by VikingOSX,

    VikingOSX VikingOSX Aug 6, 2015 6:12 AM in response to christian.scalese
    Level 7 (20,544 points)
    Mac OS X
    Aug 6, 2015 6:12 AM in response to christian.scalese

    Try the following based on the maroon color in your background.

    set colour to {0.45, 0.1, 0.1, 0.7} -- {red, green, blue, alpha} {117,26, 26, 0.7}


    You get the decimal color values by R/255, G/255, B/255.

     

    Looks like the following text:

    Screen Shot 2015-08-06 at 9.03.14 AM.png

     

    Ideally, you need a color picker that allows you to put an eyedropper over the color to view the RGB color values. You can make your own very easily in AppleScript with a single line of code. Save it as an application. Pick the second color icon from the left in the toolbar. That will show you RGB colors.

     

    choose color

  • by Hiroto,

    Hiroto Hiroto Aug 6, 2015 11:20 AM in response to christian.scalese
    Level 5 (7,276 points)
    Aug 6, 2015 11:20 AM in response to christian.scalese

    Hello

     

    Simply replace the second line:

     

    set colour to {0.9, 1, 1, 0.7} -- {red, green, blue, alpha}
    

     

     

    with this:

     

    set colour to {1, 0, 0, 1} -- {red, green, blue, alpha}
    

     

     

    and you'll get a pure opaque red text.

     

     

    Regards,

    H

     

     

    PS. The AppleScript script below will let you choose a colour and return list representing {red, green, blue, alpha} components in result window. The fourth component – apha – is alpha channel value which denotes opacity and is currently set to 1.0.

     

    set rgb to choose color
    repeat with c in rgb
        set c's contents to c / 65535
    end repeat
    set rgba to rgb & 1.0
    

     

     

    EDIT: added PS.

  • by christian.scalese,

    christian.scalese christian.scalese Jan 15, 2016 4:38 AM in response to Hiroto
    Level 1 (0 points)
    Jan 15, 2016 4:38 AM in response to Hiroto

    hi dirotto, do you know how i can instale ruby cocoa on osx 10.11?

    because the script that you said to me it doesn't work now

  • by VikingOSX,

    VikingOSX VikingOSX Jan 15, 2016 5:31 AM in response to christian.scalese
    Level 7 (20,544 points)
    Mac OS X
    Jan 15, 2016 5:31 AM in response to christian.scalese

    The binary distribution of RubyCocoa is incompatible with El Capitan, and the last activity on the source repository occurred in Sept. 2015. There is no information if/when a binary distribution will be made compatible with El Capitan, and the lack of repository activity does not bolster hope.

     

    Being somewhat frustrated that I had Ruby code here without bridge support, I downloaded/compiled/installed the RubyCocoa repository source code into El Capitan. This appears to allow Ruby with Cocoa and other bridge-support dependencies to work. As far as I could tell, no OS X installed Ruby Frameworks were stepped on in this process — a prerequisite to installation by me. There is no assurance that this custom installation of RubyCocoa will continue to work with future El Capitan updates, or later.

     

    If you are uncomfortable around the OS X command-line compilation tools, and potentially having to punt to get the code compiled, then this may not be suitable for you. In that case, perhaps Hiroto can generously offer Python/Objective-C versions of his Ruby code to you.

  • by Hiroto,

    Hiroto Hiroto Jan 17, 2016 9:53 AM in response to christian.scalese
    Level 5 (7,276 points)
    Jan 17, 2016 9:53 AM in response to christian.scalese

    Hello

     

    It seems there's no pre-compiled build of rubycocoa 1.2.0 for OS X 10.11 yet. In such case, you'd need to build it yourself.

     

    You can obtain the source code from:

     

    https://rubycocoa.github.io/

    https://github.com/rubycocoa/rubycocoa/releases

    https://github.com/rubycocoa/rubycocoa/archive/release-1_2_0.tar.gz

     

     

    The archive contains rubycocoa-release-1_2_0/src/doc/getting-started.md, which explains how to build and install rubycocoa although the descripton is old and does not reflect the current file names.

     

    * excerpt from rubycocoa-release-1_2_0/src/doc/getting-started.md

     

    ## Build from Source

     

    Extract RubyCocoa source from the '.tar.gz' file into a directory somewhere.

     

        $ tar xzf RubyCocoa-x.x.x.tar.gz

        $ cd RubyCocoa-x.x.x

        $ ruby install.rb config

        $ ruby install.rb setup

        $ ruby install.rb test

        $ sudo ruby install.rb intstall

     

     

    ---

    In case, here's a recipe to build and install rubycocoa 1.2.0.

     

    0) Have Xcode and its command line tools installed.

     

    1) Save the following script as plain text file named "install.command" (without quotes) on Desktop:

     

     

    #!/bin/bash
    
    SRC=https://github.com/rubycocoa/rubycocoa/archive/release-1_2_0.tar.gz
    DIR=~/desktop/build
    RUBY=ruby
    
    # create working directory
    mkdir "$DIR" && cd "$DIR" || exit
    
    # get source
    curl -L -O ${SRC}
    
    # expand source
    tar xzf release-1_2_0.tar.gz
    cd rubycocoa-release-1_2_0/src || exit
    
    # conifg, setup and test
    $RUBY install.rb config &&
    $RUBY install.rb setup  &&
    $RUBY install.rb test   &&
    
    # install
    sudo -k &&
    sudo $RUBY install.rb intstall
    
    exit 0
    

     

     

     

    and make it executable by the following commands in Terminal:

     

     

    #!/bin/bash
    cd ~/Desktop
    chmod u+x install.command
    

     

     

     

    2) Double click the install.command file in Finder to execute it. It will download the source, build and install rubycocoa 1.2.0. If everything has gone well, you'll be asked for administrator password to execute the last install command. If something has failed, script won't proceed and terminate itself early. You may delete the working directory ~/Desktop/build when done if successfully or not.

     

     

    Tested with ruby 1.8.7 and ruby 2.0.0 under OS X 10.6.8. (I had to edit tests/testall.rb when run with ruby 1.8.7 and fix ext/rubycocoa/extconf.rb.in when run with ruby 2.0.0-p647 under OS X 10.6.8, but I presume it would be fine when run with ruby 2.0.0 under OS X 10.9 or later)

     

    Good luck,

    H

     

    PS. If you're not familiar or comfortable with command line operations, you don't have to be committed to self compiling. I'll be trying to translate the rubycocoa script to pyobjc script which you should be able to run without additional installation.

  • by Hiroto,

    Hiroto Hiroto Jan 17, 2016 1:18 PM in response to Hiroto
    Level 5 (7,276 points)
    Jan 17, 2016 1:18 PM in response to Hiroto

    Oops. There's a typo in the last command... Sorry for that. Correction is as follows.

     

     

    #!/bin/bash
    
    SRC=https://github.com/rubycocoa/rubycocoa/archive/release-1_2_0.tar.gz
    DIR=~/desktop/build
    RUBY=ruby
    
    # create working directory
    mkdir "$DIR" && cd "$DIR" || exit
    
    # get source
    curl -L -O ${SRC}
    
    # expand source
    tar xzf release-1_2_0.tar.gz
    cd rubycocoa-release-1_2_0/src || exit
    
    # conifg, setup and test
    $RUBY install.rb config &&
    $RUBY install.rb setup  &&
    $RUBY install.rb test   &&
    
    # install
    sudo -k &&
    sudo $RUBY install.rb install
    
    exit 0
    

     

     

     

    All the best,

    H

  • by christian.scalese,

    christian.scalese christian.scalese Jan 19, 2016 3:14 AM in response to Hiroto
    Level 1 (0 points)
    Jan 19, 2016 3:14 AM in response to Hiroto

    i'm trying to do what you say me, ill say you if it works

     

    in other options, there are any other tools to make a display dialog for the script that i post in the first page?

Previous Page 2 of 3 last Next