Problem Bringing a Given Window Frontmost with AppleScript from Python

When I run the following code from terminal the pop-up box will become frontmost but with KM it will not. I get no error message, it's just that the window will not become frontmost.

Don't worry about this code:
timerbox('hey','countdown', time=1)

This is the code that is not working but there is no error message:

os.system(f'''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')

Hey Kyle,

Is that the actual code you're running?

In an Execute a Shell Script action?

You haven't given it a proper shebang line.

You shouldn't be calling the Finder like that either. Use "System Events" instead of "Finder".

I don't understand why you're calling Python either when you can run AppleScript directly.

Is this line part of a larger script?

-Chris

Yes, it's part of a larger script. I can't run apple script first, then run python inside of apple script because I am 100 times more familiar with Python and because I hate Applescript. I tried learning Applescript but then I figured out that I could do almost everything I wanted with Python that I could with Applescript except for a very short number of things.

Hey Kyle,

Okay. When posting such things please post the minimum possible working code. This will make it more likely that someone will help you.

I know in this case the AppleScript part isn't working for you, but the rest of the script really should be turnkey.

This works (change the app-name as needed):

#!/usr/bin/env python
import os

cmd = """osascript -e '
tell application "System Events"
   tell application process \"Finder\"
      set frontmost to true
   end tell
end tell
'"""

def stupidtrick():
     os.system(cmd)
stupidtrick()

In general I recommend that people NOT try to compress scripts down to one-liners, as it makes them more difficult to read and to debug.

-Chris

Could System Events change the transparency of a window of an app?

It might cooperate with the function of setting frontmost.

Where to find the document about System Events ?

Nyet.

There’s very little official documentation. You have to look at the dictionary in the AppleScript editor.

-Chris

@az22c, to provide details on @ccstone's post:

  1. Open Script Editor
  2. Goto Menu File > Open Dictionary
  3. Choose "System Events" in the popup window
  4. Search on "window" to find all references to window.

You can also do a Google Search on "AppleScript system events" (no quotes) to see what's available on the 'net.

1 Like

I have known an app called Deskovery, which could listen global shortcut and then make current window toggle always-on-the-top or toggle semitransparent.

So maybe every window of MacOS can receive corresponding events, so that they could achieve those.

Something like Swift or Objective-C, which Deskovery may developed in, might have more abilities than Applescript.

This is very low-level and requires "hacking" the macOS, as there is no official API from Apple that allows a utility to access other applications' windows in this way.

-Chris

1 Like