Hi @Thomas_Calvin I never use Cut, Copy and Paste when scripting unless absolutely necessary (because it changes the user's clipboard), but if you do, I strongly suggest you use app.copy() and app.paste().
This is the way I would approach the task of loading a graphic style from another document. See if it helps in your case.
- Mark
/**
* @file Load A Graphic Style.js
*
* Example showing one approach to loading a graphic style.
*
* @author m1b
* @version 2025-04-29
* @discussion https://community.adobe.com/t5/illustrator-discussions/why-doesn-t-cut-amp-paste-work-in-script/m-p/15294229
*/
(function () {
// NOTE: for this example I have my graphic style "DiagramArrow" in another
// document (not necessarily open) called "graphic-styles.ai" in the "lib" subfolder.
// See documentation for the parameters of `loadGraphicStyle` function: you could
// otherwise pass it a full path or a Document.
var styles = loadGraphicStyle('lib/graphic-styles.ai', app.activeDocument, ['DiagramArrow']);
if (!styles)
return alert('Failed to load graphic styles.');
var diagramArrowStyle = styles['DiagramArrow'];
if (!diagramArrowStyle)
return alert('Failed to load "DiagramArrow" graphic style.');
// do things, knowing that the "DiagramArrow" style is available
// ...
})();
/**
* Load graphic styles from one document into another.
*
* As a workaround for the limited methods
* of `ArtStyle`, we apply the target style
* to a temporary page item and then duplicate
* that item across to `destinationDoc`.
*
* Example usage:
* var styles = loadGraphicStyle(
* '/lib/graphic-styles.ai',
* app.activeDocument,
* ['Double-headed Arrow', 'Big Box']
* );
*
* How to use the returned object:
* var arrowStyle = styles['Double-headed Arrow'];
* var boxStyle = styles['Big Box'];
*
* @author m1b
* @version 2025-04-29
* @param {Document|String} sourceDoc - a document, or a path to a document, or the filename of a document in the same folder as this script.
* @param {Document} destinationDoc - the destination for the loaded style.
* @param {String|Array<String>} names - the name (or names, if array) of graphic styles to load from source document.
* @returns {Object} - an object containing reference to the loaded graphic styles, accessed using the style name.
*/
function loadGraphicStyle(sourceDoc, destinationDoc, names) {
var sourceIsDocument = 'Document' === sourceDoc.constructor.name;
if (
'String' === sourceDoc.constructor.name
&& File(File($.fileName).parent + '/' + sourceDoc).exists
)
// find file in same folder as this script
sourceDoc = File(File($.fileName).parent + '/' + sourceDoc);
if (
'String' === sourceDoc.constructor.name
&& File(sourceDoc).exists
)
// sourceDoc was a valid path
sourceDoc = File(sourceDoc);
if (
'File' === sourceDoc.constructor.name
&& sourceDoc.exists
)
// open the source document
sourceDoc = app.open(sourceDoc);
if (!sourceDoc.hasOwnProperty('graphicStyles'))
// couldn't derive a document
return;
if ('Array' !== names.constructor.name)
// we need to iterate over all name(s)
names = [names];
for (var i = 0, style, temp, dup; i < names.length; i++) {
style = getThing(sourceDoc.graphicStyles, 'name', names[i]);
if (!style)
// couldn't find style in sourceDoc
continue;
// create a temporary page item
temp = sourceDoc.pathItems.rectangle(0, 0, 1, 1);
style.applyTo(temp);
//duplicate styled item to destination
dup = temp.duplicate(destinationDoc, ElementPlacement.PLACEATEND);
// cleanup
dup.remove();
}
if (!sourceIsDocument)
// we don't need sourceDoc anymore
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
// return all the loaded styles
var loadedStyles = {},
counter = 0;
for (var i = 0, style; i < names.length; i++) {
style = getThing(destinationDoc.graphicStyles, 'name', names[i]);
if (!style)
continue;
loadedStyles[names[i]] = style;
counter++;
}
if (counter > 0)
return loadedStyles;
};
/**
* Returns a thing with matching property.
* If `key` is undefined, evaluate the object itself.
* @author m1b
* @version 2024-04-21
* @param {Array|Collection} things - the things to look through.
* @param {String} [key] - the property name (default: undefined).
* @param {*} value - the value to match.
* @returns {*?} - the thing, if found.
*/
function getThing(things, key, value) {
for (var i = 0, obj; i < things.length; i++)
if ((undefined == key ? things[i] : things[i][key]) == value)
return things[i];
};
Edit 2025-04-29: improved function so that it can accept an array of graphic style names or a single name. The returned object of the function contains references to the styles and the script now shows how to access it. I also show the example using a subfolder:
