Sunday, June 13, 2021

#1 2021-05-16 02:34:10 pm

Registered: 2019-10-23
Posts: 798

NSWindow, NSImage and NSImageView

Find some old code from @Shane to build NSWindow to show image.
The code from 2018 did not work in Script Editor but I made some change to it.

Here is Shanes original code … 70#p193370

Here is the code running in Script Editor on Mojave 10.14.6

Any comment is welcome, but I'm not a expert smile


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

on run
   set theImagePath to POSIX path of (choose file of type {"public.image"} default location (path to pictures folder))
   set arguments to {imagePath:theImagePath}
   if current application's NSThread's isMainThread() as boolean then
       my performDialog:arguments
       my performSelectorOnMainThread:"performDialog:" withObject:arguments waitUntilDone:true
   end if
end run

on performDialog:arguments
   set theWindow to createWindowWithRect(0, 0, 800, 800)
   set theImage to current application's NSImage's alloc()'s initWithContentsOfFile:(arguments's valueForKey:"imagePath")
   set theImageView to current application's NSImageView's alloc()'s initWithFrame:{{50, 50}, {700, 700}}
   theWindow's contentView()'s addSubview:theImageView
   theImageView's setImage:theImage
   theWindow's makeKeyAndOrderFront:me
end performDialog:

on createWindowWithRect(xMin, yMin, xLen, yLen)
   set windowSize to {{xMin, yMin}, {xLen, yLen}}
   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
   return theWindow
end createWindowWithRect

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



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)