MCPcopy Index your code
hub / github.com/python/cpython / MacOSXOSAScript

Class MacOSXOSAScript

Lib/webbrowser.py:616–665  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

614
615if sys.platform == 'darwin':
616 class MacOSXOSAScript(BaseBrowser):
617 def __init__(self, name='default'):
618 super().__init__(name)
619
620 def open(self, url, new=0, autoraise=True):
621 sys.audit("webbrowser.open", url)
622 self._check_url(url)
623 url = url.replace('"', '%22')
624 if self.name == 'default':
625 proto, _sep, _rest = url.partition(":")
626 if _sep and proto.lower() in {"http", "https"}:
627 # default web URL, don't need to lookup browser
628 script = f'open location "{url}"'
629 else:
630 # if not a web URL, need to lookup default browser to ensure a browser is launched
631 # this should always work, but is overkill to lookup http handler
632 # before launching http
633 script = f"""
634 use framework "AppKit"
635 use AppleScript version "2.4"
636 use scripting additions
637
638 property NSWorkspace : a reference to current application's NSWorkspace
639 property NSURL : a reference to current application's NSURL
640
641 set http_url to NSURL's URLWithString:"https://python.org"
642 set browser_url to (NSWorkspace's sharedWorkspace)'s ¬
643 URLForApplicationToOpenURL:http_url
644 set app_path to browser_url's relativePath as text -- NSURL to absolute path '/Applications/Safari.app'
645
646 tell application app_path
647 activate
648 open location "{url}"
649 end tell
650 """
651 else:
652 script = f'''
653 tell application "{self.name}"
654 activate
655 open location "{url}"
656 end
657 '''
658
659 osapipe = os.popen("/usr/bin/osascript", "w")
660 if osapipe is None:
661 return False
662
663 osapipe.write(script)
664 rc = osapipe.close()
665 return not rc
666
667#
668# Platform support for iOS

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…