• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Is it possible to find hyperlinks and apply a character style via GREP or script?

Contributor ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Hi -- Have a long document with an imported Word file many hyperlinks. Some of the hyperlinks lost their styling (mea culpa) after import. Ideally, I'd like to highlight all hyperlinks so I can scroll through and see which ones aren't styled correctly, but I'd setttle for a find/replace/GREP type search.

 

Found a script from from Peter Kharel that seems like it would highlight the links, but it didn't work for me in ID 2021 or ID 2022.

 

Alternately, is there a way to search via GREP? (Like an anchored object or table?)

 

Just wondering if something exists. TIA for any ideas.

TOPICS
How to , Scripting

Views

793

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 25, 2022 Sep 25, 2022

Hi Barbara, Would this work?:

 

var doc = app.activeDocument

//hyperlink character styles
var rcs = doc.characterStyles.itemByName("link-blue-regular");
var bcs = doc.characterStyles.itemByName("link-blue-bold");
var ics = doc.characterStyles.itemByName("link-blue-RegularItalic");

var hlts = doc.hyperlinkTextSources;
var t;
var i = hlts.length;
while(i--) {
    
    t = hlts[i].sourceText.characters[0].appliedFont
    if (t.name == "Rival	Regular") {
        hlts[i].sourceText.appliedCharacter
...

Votes

Translate

Translate
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

A quick jsx would do it: 

var doc = app.activeDocument;
var hlts = doc.hyperlinkTextSources;
var cs = doc.characterStyles.itemByName("Your Style Name");
//note that if the style is in a group, this won't work
//you'd need a longer statement to get the correct style
var i = hlts.length;
while(i--) {
    hlts[i].appliedCharacterStyle = cs;
}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Thanks for this! Do I need to change some of the names above to fit my documents?

ie: doc.hyperlinkTextSources? and "Your Style Name"? 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Just "Your Style Name". Keep the quotes. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

The links are on different paragraph styles, nested a couple levels deep. Lead with something like, "../name/name/styleName " ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

The applied paragraph style and their groupings don't matter. What does matter is where the Character Style you want to apply is. Let's say the Character Style you want to apply is called "Links". If that style is in a top level Character Style Group called "Hypers", you'd change the var cs line to: 

var cs = doc.characterStyleGroups.itemByName("Hypers").characterStyles.itemByName("Links");

 If you the grouping is nested, then you nest the group call like so: 

var cs = doc.characterStyleGroups.itemByName("HypersLevel1").characterStyleGroups.itemByName("HypersLevel2").characterStyles.itemByName("Links");

Or, you could just temporarily copy your character style to the root panel and run my code. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Sorry, in case I wasn't clear, originally, do all hyperlinks share the same style? Or can their style differ based on the paragraph they're in? 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

There are two different character styles used on several different paragraph styles. Some links might have the wrong character style applied. Too bad we can't layer character styles. I'm trying to think of something they would all have in common to target with the script... 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Theoretically, you could check the paragraph style and apply the correct character style based on the pstyle applied. But that's bordering on custom development. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Hey Brian,

Thanks for this. I'm working on a project right now that can use this and it should save me a bunch of time.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Feb 19, 2024 Feb 19, 2024

Copy link to clipboard

Copied

LATEST

This is brillian, thank you!!!

Could you kindly modify it in a way, that it will only apply to 

(1) selected text frame with connections

(2) selected text

Thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Hi @~BarbaraT , Did the hyperlinks’ Character Styles get broken (set to [None]), or were they just overridden?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 22, 2022 Sep 22, 2022

Copy link to clipboard

Copied

Could be some of both. I think I inadvertantly deleted a style that didn't appear to be used, but was. (The Word doc came in with a few different character styles that turned out to be for hyperlinks.) I also had import-mapped some character states to styles -- so maybe that overwrote some of them?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2022 Sep 23, 2022

Copy link to clipboard

Copied

Could be some of both.

 

If it was just one style you deleted, then maybe clear the overrides for all of the hyperlink’s applied styles and only apply your style to hyperlinks with "[None]" applied?:

 

var doc = app.activeDocument;
var hlts = doc.hyperlinkTextSources;
var cs = doc.characterStyles.itemByName("Your Style Name");
var i = hlts.length;
while(i--) {
    if (hlts[i].appliedCharacterStyle.name != "None") {
        hlts[i].clearOverrides(OverrideType.CHARACTER_ONLY)
    } else {
        hlts[i].appliedCharacterStyle = cs;
    }
}

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 23, 2022 Sep 23, 2022

Copy link to clipboard

Copied

Unfortunately, didn't work for me. Made several kinds of overrides on a test paragraph, including one set to "none".  Would be difficult to implement on the actual document, since there are different type weights/italics involved. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 23, 2022 Sep 23, 2022

Copy link to clipboard

Copied

Could you share the document or some sample pages?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 25, 2022 Sep 25, 2022

Copy link to clipboard

Copied

Sample page here. If I understand correctly, all the links would change to the style in the jsx file (link-blue-bold).  Which wouldn't really work for this current project, but could be handy for something in the future.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 25, 2022 Sep 25, 2022

Copy link to clipboard

Copied

Hi Barbara, Would this work?:

 

var doc = app.activeDocument

//hyperlink character styles
var rcs = doc.characterStyles.itemByName("link-blue-regular");
var bcs = doc.characterStyles.itemByName("link-blue-bold");
var ics = doc.characterStyles.itemByName("link-blue-RegularItalic");

var hlts = doc.hyperlinkTextSources;
var t;
var i = hlts.length;
while(i--) {
    
    t = hlts[i].sourceText.characters[0].appliedFont
    if (t.name == "Rival	Regular") {
        hlts[i].sourceText.appliedCharacterStyle = rcs
    } 
    if (t.name == "Rival	Bold") {
        hlts[i].sourceText.appliedCharacterStyle = bcs
    }
    if (t.name == "Rival	Regular Italic") {
        hlts[i].sourceText.appliedCharacterStyle = ics
    }
}

 

 

 

Before:

 

Screen Shot.png

 

After:

 

Screen Shot 1.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 27, 2022 Sep 27, 2022

Copy link to clipboard

Copied

Brilliant! Yes this works even after throwing in a nested style and other variations at it. Many thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines