Skip to content

Instantly share code, notes, and snippets.

@UskeS
Created April 3, 2025 01:40
Show Gist options
  • Save UskeS/0e31500ca9b27a54c5c2aeaf39fcf0b5 to your computer and use it in GitHub Desktop.
Save UskeS/0e31500ca9b27a54c5c2aeaf39fcf0b5 to your computer and use it in GitHub Desktop.
[Illustrator] Script to toggle active document.
/**
* @fileoverview AI_changeDocuments
* @version v1.1.0 - created 2025-03-25, modified 2025-04-03
* @author Masahiro TAKANO & Yusuke SAEGUSA
* @description
* 開いているドキュメント数に応じて動作:
* - 0件 or 1件:スクリプト終了
* - 2件:アクティブでない方に即切り替え
* - 3件以上:アクティブドキュメントを除いたリスト表示、選択で即切り替え可能
*/
function main() {
var docCount = app.documents.length;
if (docCount === 0 || docCount === 1) {
return;
}
if (docCount === 2) {
app.activeDocument = app.documents[1];
return;
}
// 3件以上:ダイアログで切り替え先を選択(アクティブは除外)
var activeDoc = app.activeDocument;
var dialog = new Window("dialog", "ドキュメント切り替え");
dialog.orientation = "column";
dialog.alignChildren = "fill";
var selectPanel = dialog.add("panel", undefined, "切り替え先ドキュメント(現在のドキュメントは除外)");
selectPanel.orientation = "column";
selectPanel.alignChildren = "fill";
var docRefs = [];
for (var j = 0; j < app.documents.length; j++) {
var doc = app.documents[j];
if (doc !== activeDoc) {
docRefs.push(doc);
}
}
var docList = selectPanel.add("listbox", undefined, docRefs, { multiselect: false });
docList.preferredSize = [300, 150];
if (docList.items.length > 0) {
docList.selection = 0;
}
var activePanel = dialog.add("panel", undefined, "現在のドキュメント");
activePanel.orientation = "column";
activePanel.alignChildren = "left";
var currentDocText = activePanel.add("statictext", undefined, activeDoc.name);
dialog.add("button", undefined, "閉じる", { name: "OK" }).onClick = function() {
dialog.close();
}
// ✅ リスト項目を選択したら即切り替え
docList.onChange = function() {
var selectedIndex = docList.selection ? docList.selection.index : -1;
if (selectedIndex >= 0) {
app.activeDocument = docRefs[selectedIndex];
}
// フォーカスを戻す(連続操作可能に)
dialog.active = true;
};
dialog.addEventListener("show", function() {
docList.active = true;
});
docList.onChange(); // リスト表示のタイミングでも実行
dialog.show();
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment