-
-
Save PaleNeutron/950dcf0a8bd376d901f2dcda47c185f4 to your computer and use it in GitHub Desktop.
Python + AppleScript to bring a window in foreground.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using applescript, sets the focus + foreground on a window by its title | |
# That works on OSX 12.0.1. | |
# @author Aurelien Scoubeau <aurelien.scoubeau@gmail.com> | |
# @edit John Lyu <paleneutron@outlook.com> | |
import argparse | |
import subprocess | |
# find the app or window back and activate it | |
def xfocus(title): | |
apple = """ | |
set the_title to "%s" | |
tell application "System Events" | |
repeat with p in every process whose background only is false | |
repeat with w in every window of p | |
if (name of w) is the_title then | |
tell p | |
set frontmost to true | |
perform action "AXRaise" of w | |
end tell | |
end if | |
end repeat | |
end repeat | |
end tell | |
""" % (title, ) | |
p = subprocess.Popen('osascript', | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT) | |
p.communicate(apple.encode())[0] | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
description='Utility to activate a window by title, for OSX') | |
parser.add_argument('title', help='Title of the window to activate') | |
args = vars(parser.parse_args()) | |
title = args['title'] | |
xfocus(title) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using AppleScript, lists the title of every window. | |
# That works on OSX 12.0.1. | |
# @author Aurelien Scoubeau <aurelien.scoubeau@gmail.com> | |
# @edit John Lyu <paleneutron@outlook.com> | |
import subprocess | |
# app name + window title farming | |
# it allows listing windows by just typing the app name | |
def xlist(): | |
apple = """ | |
set window_titles to {} | |
tell application "System Events" | |
repeat with p in every process whose background only is false | |
repeat with w in every window of p | |
set end of window_titles to (name of p) & "|>" & (name of w) | |
end repeat | |
end repeat | |
end tell | |
set AppleScript's text item delimiters to "\n" | |
window_titles as text | |
""" | |
p = subprocess.Popen('osascript', | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT) | |
windows = [ | |
c.split('|>') | |
for c in p.communicate(apple.encode())[0].decode().split('\n') | |
] | |
# raw style is 'Finder|>Downloads' 'Microsoft Outlook|>Inbox' | |
return windows | |
if __name__ == "__main__": | |
[print(i[-1]) for i in xlist()] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment