Copy link to clipboard
Copied
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:
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! 😊
Copy link to clipboard
Copied
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?