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?


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:

Here is a window with NSTextView and NSScrollView

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

thanks, but I need NSTableView example…

Check out the table examples on Takaaki’s site:

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

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.

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.

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

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: