import sys import time import objc from ScriptingBridge import SBApplication import Foundation import AppKit # Hide python rocket ship from popping up in Dock when run. import AppKit info = AppKit.NSBundle.mainBundle().infoDictionary() info['CFBundleIconFile'] = u'PythonApplet.icns' info['LSUIElement'] = True chrome = SBApplication.applicationWithBundleIdentifier_("com.google.Chrome") if chrome.isRunning(): windows = chrome.windows() frontmost_window = chrome.windows()[0] if chrome.windows() else None tab = frontmost_window.activeTab() # tab.setURL_("https://news.ycombinator.com/news") tab.setURL_("view-source:https://news.ycombinator.com/news") time.sleep(0.5) tab.viewSource() time.sleep(0.5) tab.selectAll() time.sleep(0.5) tab.copySelection() time.sleep(0.5) tab.close() # Get the general pasteboard pasteboard = AppKit.NSPasteboard.generalPasteboard() # Get the content of the clipboard clipboard_content = pasteboard.stringForType_(AppKit.NSStringPboardType) print(clipboard_content)