Sunday, February 23, 2020

#1 2020-02-11 02:48:19 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

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

Applescript:

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

Applescript:

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

Applescript:

visibleSwitchByName("Terminal")

Regards

Last edited by Fredrik71 (2020-02-11 05:51:56 pm)

Offline

 

#2 2020-02-12 04:19:52 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5168

Re: Handler, visible switch and manual...

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:

Applescript:

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:

Applescript:

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:

Applescript:

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:

Applescript:

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")


NG

Offline

 

#3 2020-02-12 06:24:31 am

Fredrik71
Member
Registered: 2019-10-23
Posts: 84

Re: Handler, visible switch and manual...

Thanks Nigel, I try your code. smile

To be honest I had to read your last code more and twice, but now I get it. 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. 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.

Applescript:

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

It didn't make Script Editor frontmost... smile

Regards.
Fredrik

Last edited by Fredrik71 (2020-02-12 06:41:57 am)

Offline

 

#4 2020-02-13 03:32:17 am

Nigel Garvey
Moderator
From:: Warwickshire, England
Registered: 2002-11-20
Posts: 5168

Re: Handler, visible switch and manual...

Fredrik71 wrote:

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


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


NG

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)