Transparent window for AppleScript-Objc

How to make a window transparent?

I have created an outlet for the window in the Delegate and made the connections. Then I suppose If I want that window 80% transparent white I should put Objective c code in the WillFinishLaunching, is that right?

property firstWindow : missing value

on applicationWillFinishLaunching_(aNotification)

end applicationWillFinishLaunching_

I know how to do that with Swift:
– firstWindow.isOpaque = false
– firstWindow.backgroundColor = NSColor(white: 1, alpha: 0.8)

How to make firstWindow 80% transparent for AppleScript-Objective c ?

This seems to do the trick (I’ve included the window creation to make testing easy):

use framework "AppKit"

property ca : current application
property NSWindow : class "NSWindow"
property NSColor : class "NSColor"

set win to NSWindow's alloc's initWithContentRect:(ca's NSMakeRect(50, 50, 400, 400)) styleMask:(((ca's NSWindowStyleMaskClosable) as integer) + ((ca's NSWindowStyleMaskTitled) as integer)) backing:(ca's NSBackingStoreBuffered) defer:false
set win's opaque to false
set c to NSColor's whiteColor()
set c to c's colorWithAlphaComponent:0.8
set win's backgroundColor to c
win's orderFront:me

Thank you for your answer. But I just do not understand the code. Can you advise where I can learn more about this?

errr… I usually point to this link for a primer: https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomationScriptingGuide/AppendixA-AppleScriptObjCQuickTranslationGuide.html. Not sure if that helps or not.

There’s nothing particularly obscure about this. The first line includes the AppKit framework, the next three set up some convenience variables, the fifth line creates a window to play with, and the remaining lines just translate your swift code into AppleScriptObjC (well, technically I translated your Swift to Obj-C first, then to ASObj-C). If you could be more specific about what you don’t understand, I can clarify.

Oh, I forgot to add… If you run this from the Script Editor, you need to make sure it’s running in the foreground or you’ll get an error message. Either use command-control-R, or hold down the control key when you select run from the Script menu.

Sorry, I still haven’t fully woken up yet, and I just realized you’re doing this in Xcode, not the script editor.

D’oh!

Forget most of what I wrote above, and use this instead:

on applicationWillFinishLaunching:aNotification
	set firstWindow's opaque to false
	set bColor to NSColor's whiteColor() -- create a white background color object
	set bColor to bColor's colorWithAlphaComponent:0.8 -- drop its alpha to .8
	set firstWindow's backgroundColor to bColor -- apply it
end applicationWillFinishLaunching:

This assumes the window is created and available by the time this method is called, obviously…

Thank you for your answer. I have copy - paste your code to the AppDelegate.applescript of Xcode and it does not make the window transparent. It gives me no error either.

I tried your link to Objective-C to AppleScript Quick Translation Guide. But I know nothing about Objective-C, I am learning Swift and Swift UI. I cannot find where I can learn about AppleScript App in Xcode. (I suppose it makes no sense to learn everything about Obective C in 2019. I just want the basic to deal with AppleScript)

Swift and objective-c are cognates: mostly it’s a simple job to translate one to the other, so learning the basics of objective-c shouldn’t be too difficult, and it’s more-or-less a requirement for using AppleScriptObjective-C or writing AppleScript code in Xcode. I picked up the basics of Swift in about a day, working from what I know of Obj-c, so don’t let it scare you.

Ans no, objective-c isn’t going away anytime soon…

Try shifting the code from the ‘applicationWillFinishLaunching:’ method to the ‘applicationDidFinishLaunching:’ method. The only reason I can think of that the code would not work as expected is that the ‘firstWindow’ property isn’t yet set (either because the Window hasn’t been created or the connection hasn’t been set). Everything internal should be set up correctly by the time the applicationDidFinishLaunching: method is called.

maybe this?

property theWindow : missing value

on applicationWillFinishLaunching:aNotification
– Insert code here to initialize your application before any files are opened
theWindow’s setAlphaValue_(0.80)
end applicationWillFinishLaunching:

@TheIaMonLyOneHeRe : This works well! Thank you.

That line of code does not seem AppleScript nor Objective-c. Is this like a mix? where can I learn that? I have intensely searched Internet and there is very few information and commonly old.

‘alphaValue’ is a property of the NSWindow class. The default setter and getter methods for a class property named ‘xxxx’ are constructed like so:
(getter) - xxxx;
(setter) - setXxxx:(id)val;

So, ‘setAlphaValue:’ is the standard setter method for the alphaValue property.

I agree with @TedW, thanks for your explanations.
but this is obj-c so the
What I did was translate to Objective-c for Applescript,
it would look like this in Objective-c:

[self.window setAlphaValue:0.80];

Hey Ted, check you PM, please!