{"id":628,"date":"2014-12-03T15:00:31","date_gmt":"2014-12-03T15:00:31","guid":{"rendered":"https:\/\/greladesign.co\/blog\/?p=628"},"modified":"2014-12-04T08:01:35","modified_gmt":"2014-12-04T08:01:35","slug":"add-artboard-with-javascript-in-illustrator-cs5-1","status":"publish","type":"post","link":"https:\/\/greladesign.co\/blog\/2014\/12\/03\/add-artboard-with-javascript-in-illustrator-cs5-1\/","title":{"rendered":"Add artboard with JavaScript in Illustrator CS5.1"},"content":{"rendered":"<p>I&#8217;m trying to pull together set of scripts to help with android\/ios graphic resources development. I can export artwork to PNG but results due to scaling are not satisfactory. This means that the scaling and adjusting graphic vectors needs to be done by the graphic designer. To help him in this I want to create a script that will produce set of <em>artboards<\/em> with given name and base dimensions.<br \/>\n<!--more--><\/p>\n<p>I&#8217;ve searched the documentation and found that one can add new <em>artboard<\/em> with the <code>add<\/code> method of the <code>Artboards<\/code> instance. Docs says that it receives the rect argument &#8211; and nothing more as to what this rect is.<\/p>\n<p>I&#8217;ve played with it and got frustrated by meaningless error messages that were not helpful.<\/p>\n<blockquote><p>Error 1242: Illegal argument &#8211; argument 1 &#8211; Rectangle value expected<\/p><\/blockquote>\n<p>or<\/p>\n<blockquote><p>Rectangle does not have a constructor<\/p><\/blockquote>\n<p>or<\/p>\n<blockquote><p>Error 1200: an Illustrator error coccurred: 1346458189(&#8216;PARAM&#8217;)<\/p><\/blockquote>\n<p>After searching here and there I&#8217;ve found that you should pass an array of 4 elements with following values (left, top, right, bottom).<br \/>\nPARAM errors means that some of the elements in the given list are invalid e.g. <code>[-300,-300,200,-300]<\/code><\/p>\n<p>to calculate them easily I&#8217;ve put following function:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar newRect = function(x, y, width, height) {\r\n    var l = 0;\r\n    var t = 1;\r\n    var r = 2;\r\n    var b = 3;\r\n\r\n    var rect = [];\r\n\r\n    rect[l] = x;\r\n    rect[t] = -y;\r\n    rect[r] = width + x;\r\n    rect[b] = -(height - rect[t]);\r\n\r\n    return rect;\r\n};\r\n<\/pre>\n<p>now it is much easier to add artboards at given size and position<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar artboards = app.activeDocument.artboards;\r\nvar artboard = artboards.add(newRect(0,-50,50,50));\r\n    artboard.name = &quot;test 1&quot;;\r\n    artboard = artboards.add(newRect(100,0,50,50));\r\n    artboard.name = &quot;test 2&quot;;\r\n    artboard = artboards.add(newRect(0,100,50,50));\r\n    artboard.name = &quot;test 3&quot;;\r\n    artboard = artboards.add(newRect(-50,0,50,50));\r\n    artboard.name = &quot;test 4&quot;;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to pull together set of scripts to help with android\/ios graphic resources development. I can export artwork to PNG but results due to scaling are not satisfactory. This means that the scaling and adjusting graphic vectors needs to &hellip; <a href=\"https:\/\/greladesign.co\/blog\/2014\/12\/03\/add-artboard-with-javascript-in-illustrator-cs5-1\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":""},"categories":[222,141],"tags":[226,224,225,223],"_links":{"self":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/628"}],"collection":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":7,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"predecessor-version":[{"id":647,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/posts\/628\/revisions\/647"}],"wp:attachment":[{"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/greladesign.co\/blog\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}