Handler, visible switch and manual...

First handler use switch of visible status of true/false, also make application frontmost.

Edit:
To setup a keyboard shortcut is a very fast way switch application instead of using cmd + H

on visibleSwitchByName(appName)
	tell application "System Events"
		if (visible of process appName) is true then
			my visibleByName(appName, false)
		else
			my visibleByName(appName, true)
			set frontmost of process appName to true
		end if
	end tell
end visibleSwitchByName

Second handler use manual of visible status of true/false

on visibleByName(appName, Status) --> Status: true or false
	tell application "System Events" to set properties of process appName to {visible:Status}
end visibleByName

visibleSwitchByName("Terminal")

Regards

Hi Fredrik71.

‘if’ expects a boolean value which is usually the result of some test. But if the test is to check a value known to be a boolean itself, the result (depending on the test) will be the same as the value:

true is true --> true
false is true --> false

In such cases, it’s not necessary to do the test because you can use the value directly:

if (visible of process appName) then

However, it’s not wrong to do such a test if you prefer, or if you judge that it makes the code clearer. It is necessary to do a test if you’re not absolutely sure that the value will be a boolean!

With regard to setting a process’s ‘visible’ property, it’s more efficient to do this directly rather than use a separate handler and set the ‘properties’ to a record, but the difference isn’t noticeable in practice:

on visibleSwitchByName(appName)
	tell application "System Events"
		tell application process appName
			if (visible) then
				set visible to false
			else
				set visible to true
				set frontmost to true
			end if
		end tell
	end tell
end visibleSwitchByName

visibleSwitchByName("Terminal")

Not quite as efficient (it accesses the process’s properties four times!) but pleasingly brief is this:

on visibleSwitchByName(appName)
	tell application "System Events"
		tell application process appName
			set visible to (not visible)
			set frontmost to visible
		end tell
	end tell
end visibleSwitchByName

visibleSwitchByName("Terminal")

Thanks Nigel, I try your code. :slight_smile:

To be honest I had to read your last code more and twice, but now I get it. :slight_smile:

The way I try to code AppleScript is to think as logic I could, test, go back and change and see
what happens. Try to follow the rules to be able to read my code in later time to understand it.
Simple code maybe are not the best ones, I get that but everyone has to start somewhere. :slight_smile:

Nigel, this is a site for you. :wink:
https://www.rosettacode.org/wiki/Rosetta_Code

I have include your code in my Library so thanks for sharing.

Ps. I test your code with Script Editor like this.

set this_app to name of current application
visibleSwitchByName(this_app)
delay 3
visibleSwitchByName(this_app)

It didn’t make Script Editor frontmost… :slight_smile:

Regards.
Fredrik

Thanks for the link. I’ve learnt a new word there: “chrestomathy”. I must write it down somewhere. :wink:

I see the site has code examples in BBC Basic! That takes me back to my first computer and my early attempts at coding. :slight_smile: