Tuesday, November 19, 2019

#1 2019-10-19 09:25:12 am

NMG
Member
Registered: 2019-09-29
Posts: 29

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  ?

Offline

 

#2 2019-10-19 12:04:09 pm

TedW
Member
Registered: 2005-11-02
Posts: 62

Re: Transparent window for AppleScript-Objc

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

Applescript:

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

Offline

 

#3 2019-10-20 09:16:38 am

NMG
Member
Registered: 2019-09-29
Posts: 29

Re: Transparent window for AppleScript-Objc

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

Offline

 

#4 2019-10-20 09:57:07 am

TedW
Member
Registered: 2005-11-02
Posts: 62

Re: Transparent window for AppleScript-Objc

errr... I usually point to this link for a primer: https://developer.apple.com/library/arc … Guide.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.

Offline

 

#5 2019-10-20 10:07:57 am

TedW
Member
Registered: 2005-11-02
Posts: 62

Re: Transparent window for AppleScript-Objc

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:

Applescript:

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...

Offline

 

#6 2019-10-21 03:06:12 am

NMG
Member
Registered: 2019-09-29
Posts: 29

Re: Transparent window for AppleScript-Objc

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)

Offline

 

#7 2019-10-21 10:32:56 am

TedW
Member
Registered: 2005-11-02
Posts: 62

Re: Transparent window for AppleScript-Objc

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.

Offline

 

#8 2019-10-21 07:35:54 pm

TheIaMonLyOneHeRe
Member
Registered: 2018-09-16
Posts: 29

Re: Transparent window for AppleScript-Objc

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:

Offline

 

#9 2019-10-22 03:40:59 am

NMG
Member
Registered: 2019-09-29
Posts: 29

Re: Transparent window for AppleScript-Objc

TheIaMonLyOneHeRe wrote:



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.

Last edited by NMG (2019-10-22 03:41:37 am)

Offline

 

#10 2019-10-22 09:55:27 am

TedW
Member
Registered: 2005-11-02
Posts: 62

Re: Transparent window for AppleScript-Objc

NMG wrote:
TheIaMonLyOneHeRe wrote:

theWindow's setAlphaValue_(0.80)


That line of code does not seem AppleScript nor Objective-c. Is this like a mix?



'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.

Offline

 

#11 2019-10-22 09:53:15 pm

TheIaMonLyOneHeRe
Member
Registered: 2018-09-16
Posts: 29

Re: Transparent window for AppleScript-Objc

NMG wrote:


@TheIaMonLyOneHeRe : This works well! Thank you.
That line of code does not seem AppleScript nor Objective-c.


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:

Applescript:

[self.window setAlphaValue:0.80];

Offline

 

#12 2019-10-23 09:56:24 am

TheIaMonLyOneHeRe
Member
Registered: 2018-09-16
Posts: 29

Re: Transparent window for AppleScript-Objc

TedW wrote:
NMG wrote:
TheIaMonLyOneHeRe wrote:

theWindow's setAlphaValue_(0.80)


That line of code does not seem AppleScript nor Objective-c. Is this like a mix?


Hey Ted, check you PM, please!

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)