indesign-scripting-python
indesign-scripting-python copied to clipboard
Link.Relink throws "Cannot create the link resource from the given URI."
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!