Highlighted

Best way to store document-specific script preferences?

Contributor ,
May 09, 2020

Copy link to clipboard

Copied

Hello all, I was wondering if there was a best-practice way of storing data in an Illustrator document.

 

My quick attempt was to simply create a "Settings" layer (locked and hidden) with a PageItem named after the script, and store JSON in the "note" property. This works mostly ok, but has problems:

  • it is intrusive, being so visible in the layers palette
  • it can be inadvertently destroyed by using Undo after creating or updating it.

 

Any ideas would be much appreciated!

Hi, you can save your datas in your doc's metadatas 🙂

You have to check documentation about XML and XMP metadatas.

TOPICS
Scripting

Views

456

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

Best way to store document-specific script preferences?

Contributor ,
May 09, 2020

Copy link to clipboard

Copied

Hello all, I was wondering if there was a best-practice way of storing data in an Illustrator document.

 

My quick attempt was to simply create a "Settings" layer (locked and hidden) with a PageItem named after the script, and store JSON in the "note" property. This works mostly ok, but has problems:

  • it is intrusive, being so visible in the layers palette
  • it can be inadvertently destroyed by using Undo after creating or updating it.

 

Any ideas would be much appreciated!

Hi, you can save your datas in your doc's metadatas 🙂

You have to check documentation about XML and XMP metadatas.

TOPICS
Scripting

Views

457

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
May 09, 2020 0
Participant ,
May 09, 2020

Copy link to clipboard

Copied

Hi, you can save your datas in your doc's metadatas 🙂

You have to check documentation about XML and XMP metadatas.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 09, 2020 2
Adobe Community Professional ,
May 09, 2020

Copy link to clipboard

Copied

Have a look at the following for detailed description on XMP scripting API as mentioned by Steven

https://estk.aenhancers.com/10%20-%20Scripting%20Access%20to%20XMP%20Metadata/accessing-the-xmp-scri...

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 09, 2020 1
Contributor ,
May 09, 2020

Copy link to clipboard

Copied

Thank you both, that is perfectly what I needed!

 

It did take me most of the morning to get it working though, so if it helps anyone, here's what I ended up with.

 

I used tenA's XMPtool.jsx. Thank you, tenA, if you're listening!

 

Here's tenA's code, with trivial modifications for my needs:

 

 XMPtool = {
    ns: "http://ns.adobe.com/xap/1.0/",
    prefix: "xmp:",
    f: new Object(),
    read: function (prop) {
        if (xmpLib == undefined) var xmpLib = new ExternalObject('lib:AdobeXMPScript');
        var xmpFile = new XMPFile(this.f.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_READ);
        var xmpPackets = xmpFile.getXMP();
        var xmp = new XMPMeta(xmpPackets.serialize());
        var val;
        try {
            val = xmp.getProperty(this.ns, prop).toString();
        } catch (error) {}
        return val;
    },
    write: function (prop, val) {
        if (xmpLib == undefined) var xmpLib = new ExternalObject('lib:AdobeXMPScript');
        var xmpFile = new XMPFile(this.f.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
        var xmp = xmpFile.getXMP();
        var mt = new XMPMeta(xmp.serialize());
        XMPMeta.registerNamespace(this.ns, this.prefix);
        mt.setProperty(this.ns, prop, val);
        if (xmpFile.canPutXMP(xmp)) xmpFile.putXMP(mt);
        xmpFile.closeFile(XMPConst.CLOSE_UPDATE_SAFELY);
    }
}

// set file reference to the active document
XMPtool.f = new File(app.activeDocument.fullName);

// example write
XMPtool.write('TestProperty1', 'TestValue1');

// example read
alert( XMPtool.read("TestProperty1") );

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 09, 2020 0
Adobe Community Professional ,
May 09, 2020

Copy link to clipboard

Copied

You can add tag to an object on the document. Tags can be used to store key/value data in them. Tags can be stored on pageitems and not document object, so you will have to identify an object that you will use to save your data in. See the following for examples and more description

https://illustrator-scripting-guide.readthedocs.io/jsobjref/Tag/#jsobjref-tag

https://illustrator-scripting-guide.readthedocs.io/jsobjref/Tags/

 

-Manan

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 09, 2020 2
m1b LATEST
Contributor ,
May 09, 2020

Copy link to clipboard

Copied

That's great too, Manan. I can think of a few uses for those! 😃

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
May 09, 2020 1