lohriialo / indesign-scripting-python

Scripting in InDesign is used to automate a wide variety of repetative task or as complex as an entire new feature
145 stars 26 forks source link

Link.Relink throws "Cannot create the link resource from the given URI." #12

Closed sanzoghenzo closed 1 year ago

sanzoghenzo commented 3 years ago

Hi, thanks a ton for your work!

With the information on this site I was able to put up a python-centric DTP workflow, using invoke, pandoc, lxml and pillow.

Usually the final product is a bulletin with most of the pages in B/W, so I convert and place the b/w images (converted using pillow) in the InDesign file, but recently we need to also have the color version for digital distribution.

I'm trying to automate the switch from B/W to color images (I put them into two separate folders, "images" and "images_bw", so I only need to change the parent folder):

def relink_images(indd_file, source_dir, dest_dir):
    app = win32com.client.Dispatch('InDesign.Application.CC.2017')
    ui_level =  app.scriptPreferences.userInteractionLevel
    # UserInteractionLevels.neverInteract
    app.scriptPreferences.userInteractionLevel = 1699640946
    cur_doc = app.Open(indd_file)
    for link in cur_doc.Links:
        link_path = pathlib.Path(link.filePath)
        if link_path.parent.name == source_dir:
            new_path = link_path.parent.parent / dest_dir / link_path.name
            if new_path.exists():
                print(f"relinking {link_path.name} to {new_path}...")
                try:
                    link.Relink(str(new_path))
                except Exception as err:
                    print(f"Error relinking {new_path}:", err)
            else:
                print(f"No file named {link_path.name} exitst in {dest_dir}")
    cur_doc.save()
    app.scriptPreferences.userInteractionLevel = ui_level

The Link.Relink method raises an exception:

(-2147352567, 'Exception occurred.', (35869, 'C:\\Program Files\\Adobe\\Adobe InDesign CC 2017\\InDesign.exe', 'Cannot create the link resource from the given URI.', None, 0, 0), None)

I saw that other have the same problem with javascript, and solved using the new File() js object (even though the docs states that you can use a string).

Since we don't have access to the File object, what are our options here?

Thanks for the attention!

Amaretto-Coke commented 1 year ago

@sanzoghenzo Did you ever figure this out? I'm dealing with a similar problem.

sanzoghenzo commented 1 year ago

Hi @Amaretto-Coke , Unfortunately I didn't find a solution for this, and I probably never will since I abandoned this project. Good luck!

Amaretto-Coke commented 1 year ago

@sanzoghenzo Thanks for the reply. I'll continue troubleshooting, maybe there's a workaround. I'll post here if I find anything.

Amaretto-Coke commented 1 year ago

@sanzoghenzo Putting "file:" in front of the path seemed to do the trick for me.

marklani commented 1 year ago

@sanzoghenzo Putting "file:" in front of the path seemed to do the trick for me.

This seemed to work for me as well. i.e file:C:\Path\Path\Path

sanzoghenzo commented 1 year ago

Good to know! Closing this, but this info could be made into an example if someone is willing to do that