Skip to main content
Participant
November 26, 2024
Question

How to Get and Set Hyperlink Alt Text Programmatically in InDesign SDK

  • November 26, 2024
  • 1 reply
  • 265 views

Hello everyone,

I'm working on a project using the InDesign SDK, and I need to programmatically get and set the alt text for hyperlinks. I found some interfaces like IHyperlinkCmdData and IHyperlinkTable, but I'm having trouble figuring out how to retrieve or modify the alt text for a specific hyperlink.

Does anyone have an example snippet or guidance on how to achieve this?

Here's what I've tried so far

 

int32 hyperlinkCountint = hyperlinkTable->GetHyperlinkCount();

UID hyperlinkUID = hyperlinkTable->GetNthHyperlinkSource(n); // n = 1 

TI_CHECKED_NEW_CMD(myNewCmd, kNewHyperlinkCmdBoss); 
// custom macro defintion below
#define TI_CHECKED_NEW_CMD(theVar, theCmdBoss) \
    InterfacePtr<ICommand> theVar(CmdUtils::CreateCommand(theCmdBoss));\
    if (theVar == nil) {\
        break;\
    }

TI_CHECKED_INTERFACE(IHyperlinkCmdData, myNewCmdData, myNewCmd, IID_IHYPERLINKCMDDATA); 
// custom macro defintion below
#define TI_CHECKED_INTERFACE(theTypeSymbol, theVar, theBase, theInterface) \
    InterfacePtr<theTypeSymbol> theVar(theBase, theInterface);\
    if (theVar == nil) {\
        break;\
    }

InterfacePtr<IHyperlinkDestination> myDestination(db, hyperlinkUID,UseDefaultIID());

UIDRef myResult;
myResult = UIDRef(db, hyperlinkUID);

myNewCmdData->SetHyperlinkDestination(myResult);

PMString hyperlinkaltText = myNewCmdData->GetHyperlinkAltText();

 

 

My questions:

  1. How can I retrieve the alt text for a specific existing hyperlink?
  2. How can I set or update the alt text for that hyperlink?

 

If there's a better approach or additional steps I need to take, please let me know. A code snippet or reference would be greatly appreciated!

 

Thanks in advance for your help! 😊

1 reply

Inspiring
November 27, 2024

The following code can be used to set the alt text. But I am not sure how to read the existing alt text.

IActiveContext* acn = GetExecutionContextSession()->GetActiveContext();
IDocument* doc = acn->GetContextDocument();
IDataBase* myDB = ::GetDataBase(doc);


InterfacePtr<IHyperlinkTable> hyperlinkTable(myDB, myDB->GetRootUID(), IID_IHYPERLINKTABLE);

int32 numHyperlinks = hyperlinkTable->GetHyperlinkCount();
for (int32 i = 0; i < numHyperlinks; ++i) {
const UID myHyperlinkId(hyperlinkTable->GetNthHyperlink(i));
UIDRef itemRef = UIDRef(myDB, myHyperlinkId);
UIDList itemList(itemRef);
InterfacePtr<IHyperlink> myHyperlink(myDB, myHyperlinkId, UseDefaultIID());
if (!myHyperlink) continue;

InterfacePtr<ICommand> myNewCmd(CmdUtils::CreateCommand(kChangeHyperlinkCmdBoss));
if (myNewCmd == nil) {
break;
}
myNewCmd->SetItemList(itemList);
InterfacePtr<IHyperlinkCmdData> myNewCmdData(myNewCmd, IID_IHYPERLINKCMDDATA);
if (myNewCmdData == nil) {
break;
}
ErrorCode err = kSuccess;

PMString text = "abc";
myNewCmdData->SetHyperlinkAltText(text);
err = CmdUtils::ProcessCommand(myNewCmd);

}



Does anybody have an idea how to read the existing alt text?

Inspiring
November 8, 2025

It seems best to use IID_IHYPERLINKALTTEXTDATA.

do
{
    IActiveContext* acn = GetExecutionContextSession()->GetActiveContext();
    IDocument* doc = acn->GetContextDocument();
    IDataBase* myDB = ::GetDataBase(doc);

    InterfacePtr<IHyperlinkTable> hyperlinkTable(myDB, myDB->GetRootUID(), IID_IHYPERLINKTABLE);

    int32 numHyperlinks = hyperlinkTable->GetHyperlinkCount();
    for (int32 i = 0; i < numHyperlinks; ++i) {
        const UID myHyperlinkId(hyperlinkTable->GetNthHyperlink(i));
        UIDRef itemRef = UIDRef(myDB, myHyperlinkId);
        UIDList itemList(itemRef);
        InterfacePtr<IHyperlink> myHyperlink(myDB, myHyperlinkId, UseDefaultIID());
        if (!myHyperlink) continue;

        // IID_IHYPERLINKALTTEXTDATA
        InterfacePtr<IStringData> iStringData(myHyperlink, IID_IHYPERLINKALTTEXTDATA);
        if (iStringData == nil) continue;

        PMString name = iStringData->GetString();
        CAlert::InformationAlert(name);
    }
} while (false);
Inspiring
November 18, 2025

I created a plugin that allows you to retrieve and set the altText of hyperlinks from scripts.

KohakuNekotarou/KohakuExtendScriptHyperlinkAltText: InDesign C++ SDK Plug-In

 

Please give it a try if you like.