Last active
June 6, 2024 01:14
-
-
Save monokano/82482732d2992c7b6982a8a904a7f8af to your computer and use it in GitHub Desktop.
Batch relinks the same linked file in an Illustrator document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Batch relinks the same linked file in an Illustrator document | |
// Targets | |
// Links that are selected within the document. | |
// Important: Even if not selected, links pointing to the same file as the selected ones are considered targets. | |
// Considerations | |
// Use the direct selection tool to select links within the document. | |
// Do not run in Isolation Mode as the process will not complete successfully. | |
// How to Use | |
// 1. Select link(s) within the document (multiple selections possible) | |
// 2. Run this script | |
// 3. Select the file(s) to relink when the file selection dialog appears | |
// 4. Completion message will be displayed | |
// Get the active document | |
var doc = app.activeDocument; | |
// Get all the placed items (links) | |
var links = doc.placedItems; | |
// Function to replace links with a new path | |
function relinkToNewPath(item, newPath) { | |
if (item.typename === "PlacedItem") { | |
item.file = new File(newPath); | |
} | |
} | |
// Check selected items | |
if (app.selection.length > 0) { | |
var selectedItems = app.selection; | |
var targetFiles = []; | |
// Collect links of selected items | |
for (var i = 0; i < selectedItems.length; i++) { | |
if (selectedItems[i].typename === "PlacedItem") { | |
targetFiles.push(selectedItems[i].file); | |
} | |
} | |
// If no links are found in the selection, display an alert and exit | |
if (targetFiles.length === 0) { | |
alert("No links found in the selection. Please select a link."); | |
} else { | |
// Show file selection dialog for the new link destination | |
var newLinkFile = File.openDialog("Select the new link destination file"); | |
// Check if selected file is valid | |
if (newLinkFile) { | |
var newLinkPath = newLinkFile.fsName; // Get file path | |
// Replace links including those using the same linked files | |
for (var j = 0; j < links.length; j++) { | |
for (var k = 0; k < targetFiles.length; k++) { | |
if (links[j].file && links[j].file.fullName === targetFiles[k].fullName) { | |
relinkToNewPath(links[j], newLinkPath); | |
} | |
} | |
} | |
alert("Links have been replaced with the new file."); | |
} else { | |
alert("No new link destination file was selected."); | |
} | |
} | |
} else { | |
alert("No items selected. Please select links."); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2024.6.6 "RasterItem" excluded.