Script to Create a Pattern Swatch from image, and name the swatch from image name
Copy link to clipboard
Copied
Hoping someone has an idea how to script this out.
I have the need to create pattern swatches from placed/embedded images, preferably in bulk. OOTB AI works fine to create the pattern, but i need to NAME the swatch the same name as the image (less the file extension).
For example.
1. Place image: cheetahprint.png
2. Embed image
3. Drag image to swatches palette to make pattern
4. By default, this just makes "new pattern swatch 1"
5. I want a script that will make the swatch name from the filename of the placed image
6. In this case, "cheetahprint" is the desired swatch name
I'm trying to prevent manual re-keying of 1000's of swatch names - which opens up risk of error. Accuracy is critical to downstream automation.
I would like the script to function like this:
1. Upon launch, open dialog, "select image files to make pattern swatches from"
2. Select one or more image files
3. Script performs the following procedurally:
- Places image
- Embeds image
- Makes new swatch
- Renames swatch with image filename (- .file extension)
- Repeats until last image file is processed
I'm not a coder, but would prefer javascript, and with Mac OS in mind (but happy if agnostic)
Hope to hear from you soon.
Explore related tutorials & articles
Copy link to clipboard
Copied
This would save hundreds of hours within our enterprise! Please help!
Copy link to clipboard
Copied
do both of you work on the same organization?
unfortunately, we don't have access at creating pattern swatches using scripting. I was researching a workaround last night but ran out of time.
what's your OS? both of you
Copy link to clipboard
Copied
Hi there,
Primarily Mac OS install base, but would be happy for a solution for either PC or Mac
Copy link to clipboard
Copied
And really, it's more about scripting the RENAMING of a swatch vs. creation. (although both would be awsome). Part of it could be an AI action, if necessary - then use scripting to rename. What were you thinking of doing?
Copy link to clipboard
Copied
oh ok, creation of the pattern swatches is the problem. Renaming swatches is not a problem, as long you still have the embedded images in the document.
so to use the script below,
- place your images
- embed them
- create your pattern swatches in the same order as the images stacking order in the layers panel
- deselect everything
- select your pattern swatches
- run the script
you'll go from this
to this
// rename selected pattern swatches
// carlos canto // 12-11-2019
// https://community.adobe.com/t5/illustrator/script-to-create-a-pattern-swatch-from-image-and-name-the-swatch-from-image-name/td-p/10776408
var idoc = app.activeDocument;
var rasters = idoc.rasterItems;
var selectedSwatches = idoc.swatches.getSelected();
var arr;
for (var a = 0; a < selectedSwatches.length; a++) {
arr = rasters[a].file.displayName.split(".");
arr.pop();
selectedSwatches[a].name = arr.join(".");
}
Copy link to clipboard
Copied
Hi! I'm trying to run this script and it keeps renaming my swatches numerically (like 1 through 8) rather than using the filenames. Any thoughts on why?
Copy link to clipboard
Copied
do you have the same set up as shown in my screenshots?
the systems adds 1, 2, etc, when there's already a swatch with the same name your trying to use.
Copy link to clipboard
Copied
Yep have the same set up as your screen shots. Is there a character limit? Because both of my swatches have long filenames and the first 21 characters of both filenames are the same.
Copy link to clipboard
Copied
I'm not sure if there's character limit, why don't you try with files with shorter names? just to make sure the script works.
Copy link to clipboard
Copied
I happened to have ran into an exercise in this for here (https://stackoverflow.com/questions/64245008/adobe-illustrator-scripting/64246542?noredirect=1).
With VBS it's possible to have a workaround:
Set AiApp = CreateObject("Illustrator.Application")
AppName = AiApp.Name
' MsgBox(AppName)
Set Doc = AiApp.ActiveDocument
Set MyGroup = Doc.GroupItems(1)
Doc.Selection = MyGroup
AiApp.DoJavaScript("app.executeMenuCommand('Adobe Make Pattern')")
Set WScriptShell = CreateObject("WScript.Shell")
WScriptShell.SendKeys("{Escape}")

