comet.hyperlink¶
Methods¶
- comet.hyperlink.createPageDestination(document, target, name='', shared=False)¶
Create a destination for hyperlinks pointing to a document page
- Parameters:
document (CDocument | None) –
The document to create the destination in.
The parameter type can be:
target (tuple[CDocument, int] | tuple[str, int] | CPage | int) –
The target page.
The parameter type can be:
name (str) –
The name of the destination.
Empty str: Use the default ‘Anchor’
- Returns:
The newly created destination
- Return type:
- Raises:
TypeError – When parameter types are invalid
CometError – When parameter target is of type
CPage
from a different document which has never been saved
- Available:
InDesign® comet_pdf®
- CScript:
- Examples:
Create a destination for page index 2 in the current script document.
destination = comet.hyperlink.createPageDestination(comet.gDocument, comet.gDocument.getPage(2))
- comet.hyperlink.createTextDestination(document, target, name='', shared=False)¶
Create a destination for hyperlinks pointing to a piece of text
- Parameters:
document (CDocument | None) –
The document to create the destination in.
The parameter type can be:
target (tuple[CFrame, int] | tuple[CTextModel, int]) –
The target text to link to.
The parameter type can be:
name (str) –
The name of the destination
Empty str: Use the default ‘Anchor’
shared (bool) – Create a shared destination inside the document?
- Returns:
The newly created destination
- Return type:
- Raises:
When parameter types are invalid
When parameter document is
None
and there is no script document
ValueError – When parameter target is empty
- Available:
InDesign® comet_pdf®
- CScript:
- Examples:
Create a destination for the first textindex in frame with UID 123 in the current document.
frame = comet.gDocument.getFrame(123) destination = comet.hyperlink.createTextDestination(comet.gDocument, (frame, 0))
- comet.hyperlink.createURLDestination(document, target, name='', shared=False)¶
Create a destination for hyperlinks pointing to a URL
- Parameters:
document (CDocument | None) –
The document to create the destination in
The parameter type can be:
target (str) –
The full target URL for the destination.
E-Mail URLs like mailto:name@…subject=…. as possible aswell, where the subject part is optional.
name (str) –
The name of the destination
Empty str: Use the default ‘Anchor’
shared (bool) – Create a shared destination inside the document?
- Returns:
The newly created destination
- Return type:
- Raises:
When parameter types are invalid
When parameter document is
None
and there is no script document
ValueError – When parameter target is empty
- Available:
InDesign® comet_pdf®
- CScript:
- Examples:
Create a destination for ‘priint.com’
destination = comet.hyperlink.createURLDestination(comet.gDocument, 'priint.com')
- comet.hyperlink.create(source, target, name='')¶
Create a hyperlink by providing a source and an already created destination
- Parameters:
source (CFrame | tuple[CFrame, int, int]) –
The source for this link.
The parameter type can be:
target (CHyperlinkDestination) – The defined target which must be defined in the same document as the source
name (str) –
The name of the hyperlink
Empty str: Use up to 32 characters of the source text if any, else use ‘Hyperlink’
- Returns:
The newly created link
- Return type:
- Raises:
- Available:
InDesign® comet_pdf®
- CScript:
- Examples:
Create a hyperlink from the script frame from to the URL ‘priint.com’
#!py #pragma plain import comet def main(): destination = comet.hyperlink.createURLDestination(comet.gDocument, 'priint.com') comet.hyperlink.create(comet.gFrame, destination) return 0
Create a hyperlink from the script frame to the page with index 2 in the same document.
#!py #pragma plain import comet def main(): destination = comet.hyperlink.createPageDestination(comet.gDocument, comet.gDocument.getPage(2)) comet.hyperlink.create(comet.gFrame, destination) return 0
Create a hyperlink from the script from to the first textindex in frame with UID 123 in the current document.
#!py #pragma plain import comet def main(): frame = comet.gDocument.getFrame(123) destination = comet.hyperlink.createTextDestination(comet.gDocument, (frame, 0)) comet.hyperlink.create(comet.gFrame, destination) return 0
- comet.hyperlink.find(scope)¶
Find all existing hyperlinks in a certain scope.
- Parameters:
scope –
The scope to search in.
The parameter type can be:
- Returns:
A list of found hyperlinks
- Return type:
- Raises:
TypeError – When parameter types are invalid
- Available:
InDesign®
- Examples:
Find all hyperlinks in the current document and log detailed information about their sources and destinations.
#!py #pragma plain import comet def main(): #Gather all links links: list[comet.CHyperlink] = comet.hyperlink.find(comet.gDocument) for link in links: source: comet.CHyperlinkSource = link.getSource() sourceType = source.getType() if sourceType == 0: #Frame source sourceFrame = source.getFrame() comet.wlog(f'Link Source (Frame): {sourceFrame}') elif sourceType == 1: #Textmodel source sourceTextModel = source.getTextModel() sourceStart = source.getStart() sourceLength = source.getLength() comet.wlog(f'Link Source (TextModel): {sourceTextModel}, Start: {sourceStart}, Length: {sourceLength}') linkType = link.getType() destination = link.getDestination() if linkType == 1: #URL URL = destination.getURL() comet.wlog(f'Link target (URL): "{URL}"') elif linkType == 4: #Page pageIndex = destination.getPageIndex() document = destination.getDocument() page = document.getPage(pageIndex) comet.wlog(f'Link target (Page): {page}') elif linkType == 5: #Text anchor textModel, index = destination.getTextAnchor() frame = textModel.getFrame() comet.wlog(f'Link target (Text anchor) Frame: {frame}, Index: {index}') return 0
Remove all hyperlinks pointing to ‘priint.com’ from the script document.
#!py #pragma plain import comet def main(): #Gather all links links: list[comet.CHyperlink] = comet.hyperlink.find(comet.gDocument) for link in links: linkType = link.getType() destination = link.getDestination() if linkType == 1: #URL URL = destination.getURL() if URL == 'priint.com': link.remove() return 0
- comet.hyperlink.findCrossRefs(scope, name='', classID=0, recordID=None, findInBook=False, calcTextCoords=False)¶
Find all cross reference destinations of a given name and a given Comet ID.
Cross reference destinations are created using
CTextModel.insertCrossRef()
, with the <w2cross> tag or using the product pool panel submenu Insert Cross Reference.A detailed description about Comet cross references can be found here
While creating cross reference destinations, InDesign® takes care of unique names by appending a counter to the names.
This is the reason why you may have more than one cross reference destination with the same name and ID in the document.
The find function ignores this counter and will find all cross reference destinations beginning with the given name and/or Comet ID string containing of classid, ID, ID2, ID3 and StringID of the given Comet-ID.
You may wish to search all documents of the book containing the given document.
In this case all documents of the book will be opened and closed in the background automatically.
The Comet document watch mechanism is turned off in this situation.
Cross reference destinations in overset will not be found!
- Parameters:
scope (CDocument) – The document to search in
name (str) –
Name of the destination to search for.
Find all destinations beginning with the given name.
May be empty.
recordID (tuple[int, int, int, str] | CIDType) –
Comet object ID.
The parameter type can be:
findInBook (bool) – Search in all documents of the book the document belongs to?
calcTextCoords (bool) –
Calculation of text coordinates is expensive and must therefore be enabled manually.
- Returns:
A list of found crossrefs
- Return type:
- Raises:
TypeError – When parameter types are invalid
- Available:
InDesign®
- CScript:
- comet.hyperlink.getNextUniqueKey(document=None)¶
Get next available unique hyperlink key of a document.
The function is useful if you want to create hyperlinks using TaggedText.
See here for more information
- Parameters:
document (None | CDocument) –
The document to get the next key for
None
: Active script document
- Returns:
The next unique hyperlink key
- Return type:
- Raises:
When parameter types are invalid
When parameter document is
None
and there is no script document
- Available:
InDesign® comet_pdf®
- CScript: