Skip to main content
Inspiring
April 2, 2015
Answered

Is there a way to rename slices through scripting?

  • April 2, 2015
  • 2 replies
  • 2715 views

I have a document with hundreds of icons, each organized into its own layer, and each layer containing it's slice.

Opening the Slice Options dialog to rename the slices to the same name as the layer is extremely slow, probably because I have too many slices.

So I wanted to make a script that goes through all my layer's and renames each slice it finds with the name of the parent layer.

Is that possible? I haven't found any examples that rename a slice.

Thanks!

Anthony

This topic has been closed for replies.
Correct answer Dr Franklin

We can ignore my script since it doesn't effectively do anything other than iterate through the document slices. I'm not using your script either as it doesn't fix my problem at all, it just renames the slice in the layers list, not in the Slice Options dialog which is what I need to change to export my slices with the layer names.

I'll look at the API to see if there's a way to export my PNGs manually from my slices.


I think I found my solution: a script that will export each of my layer as a png. This way I can completely eliminate slices from my file which was causing severe slowdowns so its great!

http://www.arcticmill.com/2012/08/export-layers-as-png-files-from-illustrator-in-multiple-resolutions.html

2 replies

Inspiring
April 3, 2015

Ok, however its rather confounding that you showed up here looking for a script regarding slices (read your first post), now you say you already had/have your own script.

Having said that good luck with "your script".

Inspiring
April 3, 2015

Dr Franklin wrote:

- rename the slices to the same name as the layer

- renames each slice it finds with the name of the parent layer

You mean slices from the Slice Tool? I have never messed with slices before but give the following a shot:

// Script name: renameSlicesToParentLayerName_W_J_T.js

// Purpose: Renames slices to the layer name they are found with in.

// Forum Post: https://forums.adobe.com/thread/1803810

// Note: This is a basic example snippet, with no fail-safes in place

// W_J_T 4/2/14

function renameSlicesToParentLayerName() {

    var doc = app.activeDocument;

    var itemsOnPage = doc.pageItems;

    for (var i = 0, l = itemsOnPage.length; i < l; i++) {

        var curItem = itemsOnPage;

        if (curItem.sliced) {

            curItem.name = curItem.parent.name

        }

    }

}

renameSlicesToParentLayerName();

Before:

After:


Not sure if thats what you wanted or if it will work with your setup, but hopefully it helps your efforts.

Inspiring
April 3, 2015

Almost, but your script renames the slice object. What I need is to rename the slice output file, which is set in the Slice Options... dialog. Basically what I want to do is rename my layers or my slices directly in the Layers window and not have to repeat this name in the Slice Options... dialog to export my icons. Because that dialog is really really slow to appear when you have a lot of slices in your document.

Inspiring
April 3, 2015

I'm using Adobe Illustrator CS6 (16.0.2)

Here's an example of a slice I need to rename:

The Sun layer contains a slice, by default when you create a slice, it's exported name will be something like User_939:

If I run your script, the Slice object in the layers panel will get correctly renamed to Sun, but that does not affect the actual name inside the Slice Options, so I still end up with my icon exported as User_939.png instead of Sun.png:


Ok, so you are talking about:

Object (menu) > Slice > Slice Options > Name

Are you then saving your slices via: (Or another method? If so please explain):

File (menu) > Save Selected Slices

Regardless, JS-scripting seems to not have access to that (to my knowledge), nor do I know why if named its not pulling the name from the named slice. Anyway, is your ultimate goal just getting the slices out as individual png files with the names as derived in my previous post? If so, a script could possibly be derived for that, as a custom workflow.