Fading windows snippet safe?

Hi, I’m wondering if this snippet is safe for both panels & windows. I does seem to work fine but just to be sure, I’m checking here.

on FadeIn(theWindow)
	if visible of window theWindow = false then
		set alpha value of window theWindow to 0
		set visible of window theWindow to true
		repeat with theAlpha from 0 to 100 by 2
			set alpha value of window theWindow to (theAlpha / 100)
			if theAlpha ≥ 100 then
				set alpha value of window theWindow to 1.0
				exit repeat
			end if
		end repeat
	end if
end FadeIn


on FadeOut(theWindow)
	if visible of window theWindow then
		repeat with theAlpha from 100 to 0 by -2
			set alpha value of window theWindow to (theAlpha / 100)
			if theAlpha ≤ 2 then
				set visible of window theWindow to false
				set alpha value of window theWindow to 1.0
				exit repeat
			end if
		end repeat
	end if
end FadeOut

Thanks in advance

Your code looks good to me. The handler name confused me at first though. To explain: at first I though “FadeIn(theWindow)” meant I had to pass the window to the handler, meaning FadeIn(window “myWindow”), because the handler’s variable is theWindow. But then I realized you needed to pass only the window name to the handler, not the window itself. Considering this you may want to change the variable to windowName or something similar so that it’s more clear what you need to pass to the handler.

In case you’re interested, here’s what I use to do this. Go to the following website and download “NSWindow+Fade 0.1”. Add NSWindow+Fade.h and NSWindow+Fade.m to your project. This allows you to set a time duration for how long you want it to take to fade the window in and out. In my examples below the fade time is set to 0.3 seconds.
http://www.zathras.de/programming/sourcecode.htm

To fade a window in:

call method "fadeInWithDuration:" of window "myWindow" with parameter 0.3

To fade a window out:

call method "fadeOutWithDuration:" of window "myWindow" with parameter 0.3

Thanks :slight_smile: I asked it because show window isn’t the correct syntax to show panels but it does seem to work. So I was just asking someones opinion.

You’re right about the handler stuff… I should have named it different and maybe give some details in my post…

About the objective-c part. Are there any advantages/disadvantages ? In the long run I’d like to move away from AppleScript and write objective-c but that’s gonna take some time…

It’s (almost) always an advantage to use the native language :wink:

I showed you that method only because it allows you to set a time span. Your code works as fast as it can so you can’t control how long it takes.

In general I don’t see any disadvantages to using call methods. Objective-c methods have been optimized for speed where applescript has not. Plus “call method” is part of the native language in applescript studio. As such their advantages are 1) the code executes faster so the more call methods you use the faster your program will run, and 2) you have access to more commands. Apple has only implemented a limited set of commands in native applescript language, so using call methods you basically have access to everything cocoa offers.

Okay, thank you both :slight_smile: