def asWebArchive(self): """ Convert the MHT archive to a webarchive. """ rootType, rootText = self.parts[self.root] pageResource = WebResource.alloc( ).initWithData_URL_MIMEType_textEncodingName_frameName_( NSData.dataWithBytes_length_(rootText.replace(b"\\", b"/"), len(rootText)), NSURL.URLWithString_(self.fixupURL(self.root)), NSString.stringWithString_(rootType), None, None, ) resources = [] for url in self.parts: if url == self.root: continue tp, data = self.parts[url] resources.append(WebResource.alloc( ).initWithData_URL_MIMEType_textEncodingName_frameName_( NSData.dataWithBytes_length_(data, len(data)), NSURL.URLWithString_(self.fixupURL(url)), NSString.stringWithString_(tp), None, None, )) return WebArchive.alloc( ).initWithMainResource_subresources_subframeArchives_( pageResource, resources, None)
def main(*args): if not args: args = ("dict", ) setup() try: for arg in args: url = NSURL.URLWithString_("pydoc:///%s" % (arg, )) print(NSString.stringWithContentsOfURL_(url)) finally: teardown()
def local_file_from_drop_url(url): """ Get the local file path associated with a drag/drop URL This is platform-dependent so put into it's own function :return: Local file path """ if sys.platform.startswith("darwin"): # OSx specific changes to allow drag and drop from Cocoa import NSURL return str(NSURL.URLWithString_(str(url.toString())).filePathURL().path()) else: return str(url.toLocalFile())
def local_file_from_drop_url(url): """ Get the local file path associated with a drag/drop URL This is platform-dependent so put into it's own function :return: Local file path """ if sys.platform.startswith("darwin"): # OSx specific changes to allow drag and drop from Cocoa import NSURL return str(NSURL.URLWithString_(str(url.toString())).filePathURL().path()) else: path = str(url.toLocalFile()) if sys.platform.startswith("win"): # QT bug with UNC paths. We can't fix this generally but we want # the WSL specific case to work, so detect that at least wslpath = "\\\\wsl$\\" + path if not os.path.exists(path) and os.path.exists(wslpath): return wslpath return path
def tableView_writeRows_toPasteboard_(self, tv, rows, pboard): # declare our own pasteboard types typesArray = [CopiedRowsType, MovedRowsType] # If the number of rows is not 1, then we only support our own types. # If there is just one row, then try to create an NSURL from the url # value in that row. If that's possible, add NSURLPboardType to the # list of supported types, and add the NSURL to the pasteboard. if len(rows) != 1: pboard.declareTypes_owner_(typesArray, self) else: # Try to create an URL # If we can, add NSURLPboardType to the declared types and write # the URL to the pasteboard; otherwise declare existing types row = rows[0] urlString = self.arrangedObjects()[row].valueForKey_(u'url') url = None if urlString: url = NSURL.URLWithString_(urlString) if urlString and url: typesArray.append(NSURLPboardType) pboard.declareTypes_owner_(typesArray, self) url.writeToPasteboard_(pboard) else: pboard.declareTypes_owner_(typesArray, self) # add rows array for local move pboard.setPropertyList_forType_(rows, MovedRowsType) # create new array of selected rows for remote drop # could do deferred provision, but keep it direct for clarity rowCopies = self.arrangedObjects()[:] # setPropertyList works here because we're using dictionaries, strings, # and dates; otherwise, archive collection to NSData... pboard.setPropertyList_forType_(rowCopies, CopiedRowsType) return True
def open_url(url): workspace = NSWorkspace.sharedWorkspace() workspace.openURL_(NSURL.URLWithString_(url))