Monday, July 26, 2021

#1 2021-06-14 10:01:17 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1954

Choose from list dialog using AsObjC

Hi, all.

I am trying to show Choose from list dialog using AsObjC. But my code doesn't work. What I do wrong?

Applescript:


use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use scripting additions
property arguments : missing value

if my NSThread's isMainThread() as boolean then
   my performTextView:arguments
else
   my performSelectorOnMainThread:"chooseFromListDialog:" withObject:arguments waitUntilDone:true
end if
end run


on chooseFromListDialog:arguments
   
   -- create and show window
   set windowSize to current application's NSMakeRect(0, 0, 700, 600)
   set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer)
   set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:yes
   set windowController to current application's NSWindowController's alloc()'s initWithWindow:theWindow
   windowController's |window|'s |center|()
   windowController's |window|'s makeKeyAndOrderFront:me
   
   -- create scroll view
   set theScrollView to current application's NSScrollView's alloc()'s initWithFrame:windowSize
   (theScrollView's setHasVerticalScroller:yes)
   
   -- add scroll view to window
   theWindow's contentView()'s addSubview:theScrollView
   
   -- create table view
   set theTableView to current application's NSTextView's alloc()'s initWithFrame:windowSize
   set rowName to current application's NSString's stringWithString:"Row1"
   set row1 to (current application's NSTableRow's alloc())'s initWithIdentifier:rowName
   set rowName to current application's NSString's stringWithString:"Row2"
   set row2 to (current application's NSTableRow's alloc())'s initWithIdentifier:rowName
   (row1's setHeight:20)
   (row2's setHeight:20)
   (theTableView's addTableRow:row1)
   (theTableView's addTableRow:row2)
   -- (theTableView's setDelegate:me)
   -- (theTableView's setDataSource:me)
   -- (theTableView's reloadData())
   
   -- add table to scroll view
   (theScrollView's setDocumentView:theTableView)
   
   
end chooseFromListDialog:

Last edited by KniazidisR (2021-06-14 10:04:46 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#2 2021-06-14 11:11:50 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: Choose from list dialog using AsObjC

Here is a window with NSTextView and NSScrollView

Applescript:

use framework "Foundation"
use framework "AppKit"
use scripting additions

property arguments : missing value

on run
   -- We call UI and the arguments to be running in main thread
   
   if my NSThread's isMainThread() as boolean then
       set arguments to current application's NSDictionary's dictionaryWithDictionary:arguments
       my performDialog:arguments
   else
       my performSelectorOnMainThread:"performDialog:" withObject:arguments waitUntilDone:true
   end if
end run

on performDialog:arguments
   
   -- Set up the Text view with all parameters if any.
   set theTextView to createTextView(0, 0, 700, 600)
   
   -- Set up the Scroll view with all parameters if any.
   set theScrollView to createScrollView(0, 0, 700, 600)
   (theScrollView's setHasVerticalScroller:yes)
   (theScrollView's setDocumentView:theTextView)
   
   -- Add views as subview to the window
   set subviewItems to {theScrollView}
   
   -- Main window
   set theWindow to createWindowWithRect(subviewItems, 0, 0, 700, 600)
   
   theWindow's |center|()
   theWindow's makeKeyAndOrderFront:me
end performDialog:

on createScrollView(x, y, width, height)
   set scrollViewSize to current application's NSMakeRect(x, y, width, height)
   set theScrollView to current application's NSScrollView's alloc()'s initWithFrame:scrollViewSize
   return theScrollView
end createScrollView

on createTextView(x, y, width, height)
   set textViewSize to current application's NSMakeRect(x, y, width, height)
   set theTextView to current application's NSTextView's alloc()'s initWithFrame:textViewSize
   return theTextView
end createTextView

on createWindowWithRect(subviewItems, x, y, width, height)
   set windowSize to current application's NSMakeRect(x, y, width, height)
   set winStyle to (current application's NSWindowStyleMaskTitled as integer) + (current application's NSWindowStyleMaskClosable as integer) + (current application's NSWindowStyleMaskResizable as integer)
   set theWindow to current application's NSWindow's alloc()'s initWithContentRect:windowSize styleMask:winStyle backing:2 defer:true
   
   repeat with anSubview in subviewItems
       (theWindow's contentView()'s addSubview:anSubview)
   end repeat
   return theWindow
end createWindowWithRect


if you are the expert, who will you call if its not your imagination.

Offline

 

#3 2021-06-15 01:48:05 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1954

Re: Choose from list dialog using AsObjC

thanks, but I need NSTableView example...


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#4 2021-06-15 02:05:07 am

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 6677

Re: Choose from list dialog using AsObjC

Check out the table examples on Takaaki's site:

http://piyocast.com/as/archives/tag/nstableview


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#5 2021-06-15 09:29:23 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1954

Re: Choose from list dialog using AsObjC

Thanks, Shane, for the advice. This is exactly what I need.

I want to build a choose from list dialog with the option to select multiple non-sequential list items. I'm not sure yet if I can do it. But the examples you indicated are very informative.


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#6 2021-06-16 03:30:39 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: Choose from list dialog using AsObjC

KniazidisR wrote:

I want to build a choose from list dialog with the option to select multiple non-sequential list items


Is that for learning or would you like to include more options and the standard choose from list.

If you didn't know non-sequetial list items could be done by pressing command key with your selections.


if you are the expert, who will you call if its not your imagination.

Offline

 

#7 2021-06-16 05:05:35 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1954

Re: Choose from list dialog using AsObjC

To learn AsObjC better and to make advanced choose from list dialogs for myself big_smile


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 14.1
Ram: 4 GB

Offline

 

#8 2021-06-16 07:55:45 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 854

Re: Choose from list dialog using AsObjC

So you know... to make a dialog like choose from list you also need to use:

Shanes Dialog Toolkit is helpful.
NSAlert and runModal()
and put the NSView as asseccory view

or maybe you could use runModalForWindow: (I have not done this yet)

and figure out how you could return a property of NSTextField.
stringValue() and setStringValue:

Or you could make a Nib in Xcode Interface Builder, and make script bundle to load the nib
and you call your script library from other script. Would properly be easier... wink

Last edited by Fredrik71 (2021-06-16 07:59:48 am)


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)