I was wondering if I could add an image to Myriad Tables.
I found this thread which contains the following:
set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:theFile
set theView to current application's NSImageView's alloc()'s initWithFrame:{{0, 0}, {64, 64}}
theView's setImage:theImage
theView's setImageScaling:(current application's NSImageScaleProportionallyUpOrDown)
I attempted to modify Shane’s “Accessory view sample” script included with Myriad Tables to show an image before attempting to integrate it with my script, but I can’t get that working.
use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use script "Myriad Tables Lib" version "1.0.9"
use scripting additions
property theCheckbox : missing value
property theAccessoryView : missing value
property theImageView : missing value
-- This sample uses the 'accessory view' parameter. This is a more complex process requiring some knowledge of AppleScriptObjC.
-- Manipulation of views should be done on the main thread
if current application's NSThread's isMainThread() as boolean then
my buildAccViewWithCheckbox()
my buildAccViewWithImage()
else
my performSelectorOnMainThread:"buildAccViewWithCheckbox" withObject:(missing value) waitUntilDone:true
my performSelectorOnMainThread:"buildAccViewWithImage" withObject:(missing value) waitUntilDone:true
end if
-- make a table and add the accessory view
set theTable to make new table with data {"One", "Two", "Three", "Four", "Five"}
modify table theTable accessory view theAccessoryView
modify table theTable accessory view theImageView
set theAccessoryView to missing value -- to avoid error messages when saving
set theImageView to missing value
display table theTable
-- get the state of the checkbox
set theState to theCheckbox's state() as boolean
set theCheckbox to missing value -- to avoid error messages when saving
display dialog theState as text
on buildAccViewWithCheckbox()
-- build a checkbox
set my theCheckbox to current application's NSButton's alloc()'s initWithFrame:{{10, 10}, {150, 18}}
tell theCheckbox
its setButtonType:(current application's NSSwitchButton)
its setTitle:"This is a checkbox"
its setTarget:me
its setAction:"checkboxClicked:" -- a handler in this script
end tell
-- make a view and add the checkbox to it; maximum size is 600pts x 200pts
set my theAccessoryView to current application's NSView's alloc()'s initWithFrame:{{0, 0}, {160, 28}}
theAccessoryView's addSubview:theCheckbox
end buildAccViewWithCheckbox
on buildAccViewWithImage()
set theFile to "/Users/work/Downloads/26ba7a0e-65bb-4a56-bd36-9a772ac5aa97.png"
set theImage to current application's NSImage's alloc()'s initWithContentsOfURL:theFile
set my theImageView to current application's NSImageView's alloc()'s initWithFrame:{{0, 0}, {64, 64}}
theImageView's setImage:theImage
theImageView's setImageScaling:(current application's NSImageScaleProportionallyUpOrDown)
end buildAccViewWithImage
-- this is called when the checkbox is clicked
on checkboxClicked:sender
-- make an alert and show it as a sheet over the table window
set theWindow to sender's |window|()
set theAlert to current application's NSAlert's alloc()'s init()
theAlert's setMessageText:"You clicked the checkbox"
theAlert's setInformativeText:"Now close this sheet"
theAlert's beginSheetModalForWindow:theWindow completionHandler:(missing value)
end checkboxClicked:
Sorry, probably a bone-headed mistake. I keep meaning to learn ASObjC, but never find the time, so I have no idea what I’m doing.
Also, ideally, I’d like to have it switch what image is displayed there whenever the user selects a different row. (multiple selections are not allowed). I’ve been trying to figure out what the equivalent to
its setAction:"checkboxClicked:"
that could make the dialog run code to update the image whenever the selection is changed.
If that’s not easy/practical, I could just make a button near the image display that does the update when the user clicks.
Thanks in advance for any help.