Shopping list with checkboxes (Automator + AppleScript)

Hello guys,

I would like to create in Automator a shopping list with checkboxes like this:

https://imgur.com/a/YOF7a9A

and only the selected checkboxes to be copied to clipboard.

So far I have amended part of sample code that the member “maro” created (http://piyocast.com/as/archives/8067)

and have this:

[format]
use AppleScript version “2.4”
use scripting additions
use framework “Foundation”
use framework “AppKit”

property NSView : a reference to current application’s NSView
property NSAlert : a reference to current application’s NSAlert
property NSButton : a reference to current application’s NSButton
property NSOnState : a reference to current application’s NSOnState
property NSOffState : a reference to current application’s NSOffState
property NSMutableArray : a reference to current application’s NSMutableArray
property NSButtonTypeOnOff : a reference to current application’s NSButtonTypeOnOff
property NSButtonTypeSwitch : a reference to current application’s NSButtonTypeSwitch
property NSRunningApplication : a reference to current application’s NSRunningApplication

property theResult : 0
property returnCode : 0
property bArray : {} --Checkbox button object array

set Basics to {“Bread”, “Milk”, “Eggs”, “Sugar”, “Honey”}
set Fruits to {“Apples”, “Oranges”, “Bananas”}
set Vegetables to {“Potatoes”, “Carrots”, “Onions”, “Tomatoes”}
set Others to {“Item 1”}

set cRes1 to «event LCCBCCKB» given «class CMMS»:“Please select:”, «class CSMS»:“Basics”, «class CSCS»:1, «class COLL»:Basics, «class COLC»:«constant enumcstd», «class COLR»:«constant enumdata»

set cRes2 to «event LCCBCCKB» given «class CMMS»:“Please select:”, «class CSMS»:“Fruits”, «class CSCS»:1, «class COLL»:Fruits, «class COLC»:«constant enumcstd», «class COLR»:«constant enumdata»

set cRes3 to «event LCCBCCKB» given «class CMMS»:“Please select:”, «class CSMS»:“Vegetables”, «class CSCS»:1, «class COLL»:Vegetables, «class COLC»:«constant enumcstd», «class COLR»:«constant enumdata»

on «event LCCBCCKB» given «class CMMS»:mainMes as string : “”, «class CSMS»:subMes as string : “”, «class CSCS»:cNum as integer : 1, «class COLL»:tList as list : {}, «class COLC»:eNum1 : «constant enumcstd», «class COLR»:eNum2 : «constant enumitmn»

set en1Str to eNum1 as string
set en2Str to eNum2 as string

set paramObj to {myMessage:mainMes, mySubMessage:subMes, mySuppression:"", myColNum:cNum, matrixTitleList:tList, chkType:0}
my performSelectorOnMainThread:"chooseItemByCheckBox:" withObject:(paramObj) waitUntilDone:true
set tmpList to theResult as list

return tmpList

set tmp2 to {}
repeat with i in tmpList
	set the end of tmp2 to contents of item i of tList
end repeat

return tmp2

end «event LCCBCCKB»

on chooseItemByCheckBox:(paramObj)
set aMainMes to myMessage of paramObj
set aSubMes to mySubMessage of paramObj
set aSubMes1 to mySubMessage of paramObj
set aMatList to (matrixTitleList of paramObj)
set aLen to aMatList’s |count|()
set aSupMes to mySuppression of paramObj
set aCheckBoxType to (chkType of paramObj) as integer

set aMatList to aMatList as list

set colNum to (myColNum of paramObj) as integer
set rowNum to (aLen div colNum) + (aLen mod colNum)

set aButtonCellWidth to 240
set aButtonCellHeight to 24

set viewWidth to aButtonCellWidth * colNum
set viewHeight to aButtonCellHeight * rowNum

--define the matrix size where you'll put the radio buttons
set matrixRect to current application's NSMakeRect(0.0, 0.0, viewWidth, viewHeight)
set aView to NSView's alloc()'s initWithFrame:(matrixRect)

set aCount to 1
set bArray to current application's NSMutableArray's new()
repeat with y from 1 to rowNum
	repeat with x from 1 to colNum
		if aCount ≤ aLen then
			set j to contents of item aCount of aMatList
			set tmpB to (NSButton's alloc()'s initWithFrame:(current application's NSMakeRect(((x - 1) * aButtonCellWidth), ((aLen - aCount) div colNum) * aButtonCellHeight, aButtonCellWidth, aButtonCellHeight)))
			
			(tmpB's setTitle:j)
			(tmpB's setShowsBorderOnlyWhileMouseInside:true)
			(tmpB's setTag:(aCount))
			(tmpB's setTarget:me)
			(tmpB's setAction:("clicked:"))
			if aCheckBoxType = 0 then
				(tmpB's setButtonType:(NSButtonTypeSwitch))
			else
				(tmpB's setButtonType:(NSButtonTypeOnOff))
			end if
			(bArray's addObject:tmpB)
			
		end if
		
		set aCount to aCount + 1
	end repeat
end repeat

--Select the first radio button item
--(tmpArray's objectAtIndex:0)'s setState:(current application's NSOnState)
set theResult to {}

(aView's setSubviews:bArray)

-- set up alert	
set theAlert to NSAlert's alloc()'s init()
tell theAlert
	its setMessageText:aMainMes
	its setInformativeText:aSubMes
	its addButtonWithTitle:"OK"
	its addButtonWithTitle:"Cancel"
	its setAccessoryView:aView
	
	--for suppression check box ( No use for this case? )
	if (aSupMes as string) is not equal to "" then
		its setShowsSuppressionButton:(true) -- Show "Do not show this message again" checkbox
		set suppressionB to its suppressionButton
		suppressionB's setTitle:(aSupMes)
	else
		its setShowsSuppressionButton:(false)
	end if
end tell

-- show alert in modal loop
NSRunningApplication's currentApplication()'s activateWithOptions:0
my performSelectorOnMainThread:"doModal:" withObject:(theAlert) waitUntilDone:true
if (my returnCode as number) = 1001 then error number -128

set theResult to (tmpArray's valueForKeyPath:"status") as list

end chooseItemByCheckBox:

on doModal:aParam
set (my returnCode) to aParam’s runModal()
end doModal:

on clicked:aParam
set aTag to (tag of aParam) as integer
if aTag is not in (theResult as list) then
set the end of theResult to aTag
else
set theResult to my deleteItem:aTag fromList:theResult
end if
end clicked:

on deleteItem:anItem fromList:theList
set theArray to NSMutableArray’s arrayWithArray:theList
theArray’s removeObject:anItem
return theArray as list
end deleteItem:fromList:

[/format]

However I have three different windows popping up instead of a unified one, and also the next “Copy to Clipboard” action gives me the error “The action was not supplied with the required data.”

In Script Editor I see that the selected checkboxes are returned in a list like {“Milk”, “Honey”}. Can this selection to paste into clipboard like this?
[format]Milk
Honey[/format]

Thank you in advance.