IllustratorのJSXでレイヤーを取得・作成し、選択したオブジェクトを移動する処理例です。参照する階層を明示し、同じ階層の名前が重複している場合は処理を止めます。
2026年10月3日にコードと説明を改修しました。
2026年10月16日に、Windows 11 Pro上のIllustrator 30.8.2で、掲載した4つのコードを書き換えずに実行し、単純な正常例での動作を確かめました。
確かめた範囲と確かめていない範囲は「実機で確かめた結果(2026年10月16日)」にまとめています。
掲載例はコピーしたデータで試し、レイヤー構造、重なり順、外観、書き出し結果を確認してから使ってください。
アクティブなレイヤーとオブジェクト選択の違い
doc.activeLayerは新しいオブジェクトを置く現在のレイヤーです。この値を変更しても、レイヤー内のオブジェクトをすべて選択する処理にはなりません。doc.selectionはオブジェクトなどの選択状態で、何も選んでいないときはnull、文字編集中は文字範囲を返す場合があります。
名前の取得は階層ごとに行う
最上位はdoc.layers、子はparent.layersを調べます。下のuniqueLayerは、渡されたコレクションだけを検索します。同名が0件または2件以上ならエラーにするため、名前でどれか1件を暗黙に選ぶ動作を避けられます。
| 取得する階層 | コード内の参照 |
|---|---|
| 最上位 | uniqueLayer(doc.layers, "親") |
| 親の直下 | uniqueLayer(parent.layers, "子") |
| 子の直下 | uniqueLayer(child.layers, "孫") |
最上位のレイヤーをアクティブにする
実行前に、最上位に「親」という名前のレイヤーを1つ用意し、表示・ロック解除の状態にします。既存の別名レイヤーを使う場合は、コード内の「親」をその名前に変更してください。下の4つのコードは独立した例で、順番に実行するだけで必要なレイヤーがすべてそろう構成ではありません。
(function () {
try {
if (app.documents.length === 0) throw new Error("ドキュメントを開いてください。");
var doc = app.activeDocument;
var target = uniqueLayer(doc.layers, "親");
requireEditable(target);
doc.activeLayer = target;
} catch (e) { alert(String(e)); }
function uniqueLayer(layers, name) {
var found = null;
for (var i = 0; i < layers.length; i++) {
if (layers[i].name === name) {
if (found !== null) throw new Error("同名レイヤーが複数あります: " + name);
found = layers[i];
}
}
if (found === null) throw new Error("レイヤーがありません: " + name);
return found;
}
function requireEditable(node) {
while (node && node.typename !== "Document") {
if (node.locked || node.hidden || (node.typename === "Layer" && !node.visible))
throw new Error("ロック・非表示の対象があります。");
node = node.parent;
}
}
}());子・孫レイヤーをアクティブにする
最上位の「親」の中に「子」、その中に「孫」を用意します。同名のレイヤーを最上位に3つ並べても、このコードでは取得できません。「親」「子」「孫」をすべて表示し、ロックを解除してから実行してください。
(function () {
try {
if (app.documents.length === 0) throw new Error("ドキュメントを開いてください。");
var doc = app.activeDocument;
var parent = uniqueLayer(doc.layers, "親");
var child = uniqueLayer(parent.layers, "子");
var target = uniqueLayer(child.layers, "孫");
requireEditable(target);
doc.activeLayer = target;
} catch (e) { alert(String(e)); }
function uniqueLayer(layers, name) {
var found = null;
for (var i = 0; i < layers.length; i++) {
if (layers[i].name === name) {
if (found !== null) throw new Error("同名レイヤーが複数あります: " + name);
found = layers[i];
}
}
if (found === null) throw new Error("レイヤーがありません: " + name);
return found;
}
function requireEditable(node) {
while (node && node.typename !== "Document") {
if (node.locked || node.hidden || (node.typename === "Layer" && !node.visible))
throw new Error("ロック・非表示の対象があります。");
node = node.parent;
}
}
}());親レイヤーと子レイヤーを作成する
この例が作るのは「新しい親」と、その中の「新しい子」です。ほかの例で使う「親」「子」「孫」「まとめ」は作りません。最上位に「新しい親」がすでにある場合は、重複作成せずエラーで止まります。
(function () {
try {
if (app.documents.length === 0) throw new Error("ドキュメントを開いてください。");
var doc = app.activeDocument;
for (var i = 0; i < doc.layers.length; i++) {
if (doc.layers[i].name === "新しい親") throw new Error("同名の親が存在します。");
}
var parent = doc.layers.add();
parent.name = "新しい親";
var child = parent.layers.add();
child.name = "新しい子";
} catch (e) { alert(String(e)); }
function uniqueLayer(layers, name) {
var found = null;
for (var i = 0; i < layers.length; i++) {
if (layers[i].name === name) {
if (found !== null) throw new Error("同名レイヤーが複数あります: " + name);
found = layers[i];
}
}
if (found === null) throw new Error("レイヤーがありません: " + name);
return found;
}
function requireEditable(node) {
while (node && node.typename !== "Document") {
if (node.locked || node.hidden || (node.typename === "Layer" && !node.visible))
throw new Error("ロック・非表示の対象があります。");
node = node.parent;
}
}
}());選択したオブジェクトを「まとめ」へ移動する
実行前に、移動先となる最上位のレイヤー「まとめ」を用意し、表示・ロック解除の状態にします。初めて試すときは空の「まとめ」を使い、選択ツールで移動元のオブジェクト全体を選んでください。このコードは移動先レイヤーを自動作成しません。
移動前に全件を検査し、選択を配列へ控えてから移します。グループ内の一部を直接取り出すと、クリッピングや親の外観から外れるため、この例では部分選択を拒否します。移動対象のロック・非表示を勝手に解除しません。
(function () {
try {
if (app.documents.length === 0) throw new Error("ドキュメントを開いてください。");
var doc = app.activeDocument;
var selection = doc.selection;
if (!selection || !(selection instanceof Array) || selection.length === 0)
throw new Error("選択ツールでオブジェクト全体を選んでください。");
var target = uniqueLayer(doc.layers, "まとめ");
requireEditable(target);
var items = [];
for (var i = 0; i < selection.length; i++) {
var item = selection[i];
if (!item || typeof item.move !== "function" || item.typename === "TextRange" || item.typename === "InsertionPoint")
throw new Error("文字編集中など、対象外の選択です。");
if (item.parent.typename !== "Layer")
throw new Error("グループ内の部分選択は対象外です。グループ全体を選んでください。");
requireEditable(item);
items.push(item);
}
for (var j = items.length - 1; j >= 0; j--)
items[j].move(target, ElementPlacement.PLACEATBEGINNING);
} catch (e) { alert(String(e)); }
function uniqueLayer(layers, name) {
var found = null;
for (var i = 0; i < layers.length; i++) {
if (layers[i].name === name) {
if (found !== null) throw new Error("同名レイヤーが複数あります: " + name);
found = layers[i];
}
}
if (found === null) throw new Error("レイヤーがありません: " + name);
return found;
}
function requireEditable(node) {
while (node && node.typename !== "Document") {
if (node.locked || node.hidden || (node.typename === "Layer" && !node.visible))
throw new Error("ロック・非表示の対象があります。");
node = node.parent;
}
}
}());配列の末尾から移動先の先頭へ入れるため、配列内の順序を反転させずに配置します。ただし、selectionの順序が元のレイヤーパネル全体の重なり順と一致する保証はありません。異なるレイヤー間の前後関係や、親レイヤーの効果・透明度は移動後に比較してください。途中でmoveが失敗すると一部だけ移動した状態が残ります。保存せず結果を確認します。
実機で確かめた結果(2026年10月16日)
Windows 11 Pro(10.0.26200)のIllustrator 30.8.2日本語版で確かめました。
試したのは、400×260ptのRGBドキュメントです。
最上位に「親」「まとめ」「元」のレイヤーを置き、「親」の中に「子」、その中に「孫」を作りました。
「元」には青と赤の長方形を1つずつ置き、赤が青の手前で一部重なるようにしました。
ドキュメントの用意と2つの図形の選択は、自動処理で行いました。
掲載した4つのコードは書き換えずに別々のファイルへ保存し、文字コードはBOM付きのUTF-8にしました。
| コード | 起動の方法 | 結果 |
|---|---|---|
| 最上位のレイヤーをアクティブにする | 自動処理 | アクティブなレイヤーが「元」から「親」に変わった。2つの図形は選択されたまま |
| 子・孫レイヤーをアクティブにする | 自動処理 | アクティブなレイヤーが「孫」に変わった。選択はそのまま |
| 親レイヤーと子レイヤーを作成する | 自動処理 | 最上位に「新しい親」、その中に「新しい子」ができ、アクティブなレイヤーは「新しい子」になった。選択はそのまま |
| 選択したオブジェクトを「まとめ」へ移動する | 「ファイル」→「スクリプト」→「その他のスクリプト」 | 2つの図形が「元」から「まとめ」へ移った |
前の3つのコードでは、アクティブなレイヤーが変わっても、選んでいた2つの図形の選択、位置、色、重なり順は変わりませんでした。
アクティブなレイヤーを変えることと、オブジェクトを選ぶことが別の操作であることを、この条件で確かめられました。
前の3つは自動処理で起動したもので、メニューから起動したのは移動のコードだけです。
移動のコードは、2つの図形を選んだ状態で「ファイル」→「スクリプト」→「その他のスクリプト」から実行しました。


実行後、「元」の中身は2個から0個に、「まとめ」は0個から2個になりました。
2つの図形の位置、塗りの色、線なし、赤が手前という重なり順は、移動の前後で変わりませんでした。
移動の前後に自動処理で書き出したPNGを比べると、400×260pxの画素がすべて一致しました。
画面では選択枠の色が青からピンクに変わりますが、これはレイヤーの色に合わせて枠の色が変わっただけで、図形の色は変わっていません。
保存と開き直しは自動処理で行い、開き直したあとも2つの図形は「まとめ」の中にありました。
確かめていない範囲
確かめたのは、同じレイヤーにある単純な図形2つを、正常な条件で動かした場合だけです。
同じ階層の同名レイヤー、ロックや非表示の移動先、グループ内の部分選択、文字編集中の選択で、エラーで止まるかは試していません。
異なるレイヤーからの移動、クリッピンググループや効果・透明度がある対象、途中でmoveが失敗した場合、macOSも確かめていません。
JSXの保存と実行
コードをUTF-8のテキストで保存し、拡張子を.jsxにします。「ファイル」→「スクリプト」→「その他のスクリプト」から選びます。保存・実行の詳しい手順はJSX入門を参照してください。各コードは別々のファイルとして実行します。
確認用の小さな例
- 「親」の中に「子」、その中に「孫」を作り、各コードが指定した階層だけを参照するか確認する。
- 同じ階層に「親」を2つ置き、変更前にエラーで停止するか確認する。
- 「まとめ」をロック、非表示にし、解除せず停止するか確認する。
- 別レイヤーの重なる図形とクリッピンググループで、移動前後の外観を比較する。
親だけを外す用途は子レイヤーを残して親を削除する処理、参照の基礎はDOMの階層へ進んでください。旧GitHub版・既存画像は今回の改修版と一致するとは限りません。
参照資料
Adobe公式のレイヤーパネル概要と、Adobe資料をもとに維持されているLayerのAPI参照を参照しました。
よくある質問:JSXでレイヤーを操作する
この記事のJSXは、何をするものですか?
IllustratorのJSXでレイヤーを取得・作成し、選択したオブジェクトを移動する処理例です。参照する階層を明示し、同じ階層の名前が重複している場合は処理を止めます。
アクティブなレイヤーと、オブジェクトの選択は、何が違いますか?
doc.activeLayerは、新しいオブジェクトを置く現在のレイヤーです。この値を変更しても、レイヤー内のオブジェクトをすべて選択する処理にはなりません。doc.selectionはオブジェクトなどの選択状態で、何も選んでいないときはnull、文字編集中は文字範囲を返す場合があります。
レイヤーの名前は、どうやって取得しますか?
最上位はdoc.layers、子はparent.layersを調べるなど、階層ごとに行います。
記事のuniqueLayerは、渡されたコレクションだけを検索し、同名が0件または2件以上ならエラーにします。
そのため、名前でどれか1件を暗黙に選ぶ動作を避けられます。
参照先は、最上位がuniqueLayer(doc.layers, "親")、親の直下がuniqueLayer(parent.layers, "子")、子の直下がuniqueLayer(child.layers, "孫")です。
選択したオブジェクトを「まとめ」へ移動すると、何が起こりますか?
記事の例では、移動前に全件を検査し、選択を配列へ控えてから移します。グループ内の一部を直接取り出すと、クリッピングや親の外観から外れるため、この例では部分選択を拒否します。移動対象のロック・非表示は、勝手に解除しません。異なるレイヤー間の前後関係や、親レイヤーの効果・透明度は、移動後に比較してください。途中でmoveが失敗すると、一部だけ移動した状態が残るため、保存せず結果を確認します。
スクリプトは、どうやって実行しますか?
コードをUTF-8のテキストで保存し、拡張子を.jsxにします。「ファイル」→「スクリプト」→「その他のスクリプト」から選びます。各コードは別々のファイルとして実行します。保存・実行の詳しい手順は、関連記事のJSX入門を参照してください。
動作を確かめるには、どんな試し方がありますか?
記事では、確認用の小さな例として、「親」の中に「子」、その中に「孫」を作り、各コードが指定した階層だけを参照するか確認する、同じ階層に「親」を2つ置き、変更前にエラーで停止するか確認する、「まとめ」をロック・非表示にし、解除せず停止するか確認する、別レイヤーの重なる図形とクリッピンググループで、移動前後の外観を比較する、を挙げています。
親レイヤーだけを外したいときは、どうすればよいですか?
親だけを外す用途は、関連記事の「子レイヤーを残して親を削除する処理」を案内しています。参照の基礎は、DOMの階層の記事で確認できます。
掲載コードは、実際のIllustratorで動作確認されていますか?
2026年10月3日にコードと説明を改修したものです。
2026年10月16日に、Windows 11 Pro上のIllustrator 30.8.2で、4つのコードを書き換えずに実行し、単純な正常例で動作を確かめました。
移動のコードは「その他のスクリプト」から実行し、ほかの3つは自動処理で起動しました。
同名レイヤーやロック・非表示でエラーになるか、グループ内の部分選択、クリッピングや効果がある対象、macOSは確かめていません。
旧GitHub版・既存画像は、今回の改修版と一致するとは限りません。
掲載例はコピーしたデータで試し、レイヤー構造、重なり順、外観、書き出し結果を確認してから使ってください。
最新はAdobe公式のレイヤーパネルの説明などで確認してください。

コメント
コメント一覧 (47件)
Hi, Neat post. There is a problem with your website in internet explorer, would check this… IE still is the market leader and a huge portion of people will miss your wonderful writing due to this problem.
We’re a gaggle of volunteers and opening a brand new scheme in our community. Your web site offered us with helpful info to work on. You’ve performed an impressive process and our entire group shall be thankful to you.
Today, I went to the beach front with my children. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear. She never wants to go back! LoL I know this is entirely off topic but I had to tell someone!
I believe this internet site contains very fantastic pent articles blog posts.
강남구구단 방문을 준비하고 있다면 가격 구성, 이용 방식, 예약 절차와 위치 확인사항을 살펴보세요. 인원·시간에 따른 상담 항목과 자주 묻는 질문을 한곳에 정리했습니다.
Keep working ,impressive job!
I truly appreciate this post. I’ve been looking everywhere for this! Thank goodness I found it on Bing. You have made my day! Thx again
I’ve been exploring for a little bit for any high quality articles or blog posts on this sort of area . Exploring in Yahoo I at last stumbled upon this web site. Reading this information So i’m happy to convey that I have a very good uncanny feeling I discovered exactly what I needed. I most certainly will make certain to do not forget this web site and give it a look regularly.
Enjoyed reading through this, very good stuff, appreciate it. “Whenever you want to marry someone, go have lunch with his ex-wife.” by Francis William Bourdillon.
Hey would you mind stating which blog platform you’re using? I’m planning to start my own blog soon but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking for something unique. P.S My apologies for being off-topic but I had to ask!
Just wanna input on few general things, The website style and design is perfect, the written content is real fantastic. “The sun sets without thy assistance.” by The Talmud.
I got what you intend, thankyou for posting.Woh I am delighted to find this website through google.
I dugg some of you post as I cogitated they were extremely helpful handy
Sweet web site, super layout, rattling clean and utilise pleasant.
Very interesting info !Perfect just what I was searching for!
Hello.This post was extremely fascinating, especially because I was looking for thoughts on this topic last week.
Wow, marvelous blog format! How lengthy have you ever been running a blog for? you made running a blog glance easy. The entire look of your website is wonderful, as smartly as the content material!
Heya i’m for the first time here. I came across this board and I find It truly useful & it helped me out a lot. I hope to give something back and help others like you helped me.
I think this is among the most significant info for me. And i’m glad reading your article. But wanna remark on few general things, The web site style is perfect, the articles is really nice :D. Good job, cheers
Thankyou for all your efforts that you have put in this. very interesting information.
My wife and i ended up being absolutely lucky when Louis managed to finish up his analysis with the ideas he obtained through your web page. It’s not at all simplistic just to possibly be giving out solutions some others may have been selling. We grasp we need you to be grateful to for that. The most important explanations you made, the straightforward site navigation, the relationships you can assist to create – it is mostly amazing, and it’s facilitating our son and our family imagine that this article is amusing, and that is wonderfully essential. Many thanks for all!
Have you ever considered about adding a little bit more than just your articles? I mean, what you say is valuable and everything. But just imagine if you added some great graphics or video clips to give your posts more, “pop”! Your content is excellent but with images and video clips, this blog could certainly be one of the greatest in its niche. Great blog!
I want reading through and I believe this website got some truly utilitarian stuff on it! .
I think other site proprietors should take this site as an model, very clean and magnificent user genial style and design, let alone the content. You are an expert in this topic!
This really answered my drawback, thanks!
서울출장마사지 서울출장안마 셀럽은 고객이 머무는 장소에서 관리를 받을 수 있도록 방문 서비스를 안내합니다. 자택이나 숙소의 위치, 원하는 이용 시간, 코스를 알려주시면 예약 가능
I believe this website has got some rattling good info for everyone. “A man’s dreams are an index to his greatness.” by Zadok Rabinwitz.
Keep functioning ,splendid job!
Simply wish to say your article is as amazing. The clarity in your post is simply nice and i could assume you’re an expert on this subject. Well with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please continue the enjoyable work.
Merely wanna tell that this is very beneficial, Thanks for taking your time to write this.
The next time I read a blog, I hope that it doesnt disappoint me as much as this one. I mean, I know it was my choice to read, but I actually thought youd have something interesting to say. All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.
You made certain nice points there. I did a search on the topic and found a good number of people will go along with with your blog.
Heya! I’m at work browsing your blog from my new iphone 3gs! Just wanted to say I love reading your blog and look forward to all your posts! Keep up the fantastic work!
I keep listening to the news bulletin lecture about getting free online grant applications so I have been looking around for the finest site to get one. Could you tell me please, where could i get some?
We absolutely love your blog and find many of your post’s to be precisely what I’m looking for. Do you offer guest writers to write content available for you? I wouldn’t mind publishing a post or elaborating on a lot of the subjects you write about here. Again, awesome blog!
Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thank you for sharing!
There are certainly lots of particulars like that to take into consideration. That could be a great point to convey up. I offer the ideas above as general inspiration but clearly there are questions like the one you convey up where an important factor will probably be working in sincere good faith. I don?t know if finest practices have emerged around things like that, however I am positive that your job is clearly recognized as a good game. Each girls and boys really feel the influence of only a moment’s pleasure, for the remainder of their lives.
Hi! I’ve been reading your weblog for a while now and finally got the courage to go ahead and give you a shout out from Lubbock Tx! Just wanted to say keep up the great work!
Thanks – Enjoyed this update, how can I make is so that I get an update sent in an email whenever there is a fresh article?
늑대닷컴을 검색하며 궁금했던 웹툰 정보와 작품 찾는 방법을 정리했습니다. 연재·완결 구분, 무료 회차와 미리보기 차이, 주소 정보 확인 기준, 모바일 이용 팁과 자주 묻는 질문을 확인하세요.
밤토끼가 어떤 사이트였는지, 과거 이력과 현재 검색 정보를 구분하는 방법을 정리했습니다. 예전에 보던 웹툰 찾기, 공식 연재처 확인, 무료 회차와 대여·소장 차이, 자주 묻는 질문까지 한 번에 확인하세요.
포항오피 검색어의 맥락과 온라인 정보 확인 기준을 정리했습니다. 광고성 표현과 후기의 출처, 게시물의 최신성, 개인정보 보호 방법, 포항 공공정보와 자주 묻는 질문을 확인하세요.
Thank you for helping out, superb information. “The laws of probability, so true in general, so fallacious in particular.” by Edward Gibbon.
What’s Happening i’m new to this, I stumbled upon this I’ve found It positively useful and it has helped me out loads. I hope to contribute & assist other users like its aided me. Great job.
Really Appreciate this update, can I set it up so I get an update sent in an email every time you make a fresh update?
Im not positive the place you are getting your info, however great topic. I must spend a while finding out more or figuring out more. Thanks for magnificent information I used to be looking for this info for my mission.
부산여성전용마사지 부산토닥이 드디어 부산 상륙 NO.1 부산 최대 규모 관리사들 항시 대기중 24시 연중무휴