指定した位置にガイドを作成

実行するとダイアログが表示されます。
そのダイアログに数値を入力(単位:%)してOKボタンを押すとガイドが作成されます。

// https://dtpscriptin.com/gui_edittext/
// http://www.openspc2.org/book/PhotoshopCC2014/GUI/parts/006/index.html

// ダイアログ作成
var objDlg = new Window("dialog", "ガイドを作成", [0, 0, 400, 200]);
// ボタンを作成
objDlg.okBtn = objDlg.add("button", [210, 150, 280, 80 + 25], "OK", {name: "ok"});
objDlg.cancelBtn = objDlg.add("button", [120, 150, 190, 80 + 25], "Cancel", {name: "cancel"});
// テキストを追加
var objStText01 = objDlg.add("statictext", [20, 15, 380, 35], "垂直ガイド:");
var objStText02 = objDlg.add("statictext", [20, 75, 380, 95], "水平ガイド:");
var objStText03 = objDlg.add("statictext", [330, 123, 380, 135], "単位:%");
// テキストボックスを追加
var objTxtbox01 = objDlg.add("edittext", [20, 35, 380, 55], "5,10,90,95");
var objTxtbox02 = objDlg.add("edittext", [20, 95, 380, 115], "5,10,90,95");
// チェックボックス
var objChkbox01 = objDlg.add("checkbox", [21, 123, 140, 138], "既存のガイドを消去");
// ダイアログ表示
objDlg.center();
var n = objDlg.show();

if (n === 1) {
if (objChkbox01.value === true) {
if (activeDocument.guides.length > 0) {
executeAction(stringIDToTypeID("clearAllGuides"), undefined, DialogModes.NO);
}
}
var vArray = objTxtbox01.text.replace(/\s+/g, '').split(',');
var hArray = objTxtbox02.text.replace(/\s+/g, '').split(',');
try {
verticalGuide(vArray);
horizontalGuide(hArray);
} catch (e) {
alert(e.massege);
}

}

function verticalGuide(vArray) {
var default_rulerUnits = app.preferences.rulerUnits; //既定の定規単位を取得

// http://www.openspc2.org/book/PhotoshopCC2014/easy/document/004/index.html
app.preferences.rulerUnits = Units.PIXELS;
var w = activeDocument.width.value;

for (var i = 0; i < vArray.length; i++) {
if (vArray[i] !== '') {
var desc1 = new ActionDescriptor();
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(stringIDToTypeID("position"), stringIDToTypeID("pixelsUnit"), w * (vArray[i] / 100));
desc2.putEnumerated(stringIDToTypeID("orientation"), stringIDToTypeID("orientation"), stringIDToTypeID("vertical"));
desc2.putEnumerated(stringIDToTypeID("kind"), stringIDToTypeID("kind"), stringIDToTypeID("document"));
var ref1 = new ActionReference();
ref1.putIdentifier(stringIDToTypeID("document"), 1481);
ref1.putIndex(stringIDToTypeID("good"), 1);
desc2.putReference(stringIDToTypeID("null"), ref1);
desc2.putInteger(charIDToTypeID("GdCA"), 0);
desc2.putInteger(charIDToTypeID("GdCR"), 74);
desc2.putInteger(charIDToTypeID("GdCG"), 255);
desc2.putInteger(charIDToTypeID("GdCB"), 255);
desc1.putObject(stringIDToTypeID("new"), stringIDToTypeID("good"), desc2);
var ref2 = new ActionReference();
ref2.putClass(stringIDToTypeID("good"));
desc1.putReference(stringIDToTypeID("null"), ref2);
desc1.putEnumerated(stringIDToTypeID("guideTarget"), stringIDToTypeID("guideTarget"), stringIDToTypeID("guideTargetCanvas"));
executeAction(stringIDToTypeID("make"), desc1, DialogModes.NO);
}
}
app.preferences.rulerUnits = default_rulerUnits;
}

function horizontalGuide(hArray) {
var default_rulerUnits = app.preferences.rulerUnits; //既定の定規単位を取得

// http://www.openspc2.org/book/PhotoshopCC2014/easy/document/004/index.html
app.preferences.rulerUnits = Units.PIXELS;
var h = activeDocument.height.value;

for (var j = 0; j < hArray.length; j++) {
if (hArray[j] !== '') {
var desc1 = new ActionDescriptor();
var desc2 = new ActionDescriptor();
desc2.putUnitDouble(stringIDToTypeID("position"), stringIDToTypeID("pixelsUnit"), h * (hArray[j] / 100));
desc2.putEnumerated(stringIDToTypeID("orientation"), stringIDToTypeID("orientation"), stringIDToTypeID("horizontal"));
desc2.putEnumerated(stringIDToTypeID("kind"), stringIDToTypeID("kind"), stringIDToTypeID("document"));
var ref1 = new ActionReference();
ref1.putIdentifier(stringIDToTypeID("document"), 1481);
ref1.putIndex(stringIDToTypeID("good"), 1);
desc2.putReference(stringIDToTypeID("null"), ref1);
desc2.putInteger(charIDToTypeID("GdCA"), 0);
desc2.putInteger(charIDToTypeID("GdCR"), 74);
desc2.putInteger(charIDToTypeID("GdCG"), 255);
desc2.putInteger(charIDToTypeID("GdCB"), 255);
desc1.putObject(stringIDToTypeID("new"), stringIDToTypeID("good"), desc2);
var ref2 = new ActionReference();
ref2.putClass(stringIDToTypeID("good"));
desc1.putReference(stringIDToTypeID("null"), ref2);
desc1.putEnumerated(stringIDToTypeID("guideTarget"), stringIDToTypeID("guideTarget"), stringIDToTypeID("guideTargetCanvas"));
executeAction(stringIDToTypeID("make"), desc1, DialogModes.NO);
}
}
app.preferences.rulerUnits = default_rulerUnits;
}

更新履歴

inserted by FC2 system