<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>InDesign Scripts &#8211; jaguchi log</title>
	<atom:link href="https://jaguchi.com/blog/category/indesign-scripts/feed/" rel="self" type="application/rss+xml" />
	<link>https://jaguchi.com</link>
	<description>Speeding on training wheels</description>
	<lastBuildDate>Sun, 01 Sep 2024 12:26:23 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.5</generator>

<image>
	<url>https://jaguchi.com/wp-content/uploads/2017/02/cropped-low_res-32x32.png</url>
	<title>InDesign Scripts &#8211; jaguchi log</title>
	<link>https://jaguchi.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">149953163</site>	<item>
		<title>フレームグリッドの文字数と行数をスクリプト制御</title>
		<link>https://jaguchi.com/blog/2012/03/indesign-grid-dimensions-script/</link>
		
		<dc:creator><![CDATA[mirka]]></dc:creator>
		<pubDate>Mon, 19 Mar 2012 16:25:37 +0000</pubDate>
				<category><![CDATA[InDesign Scripts]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://jaguchi.com/?p=259</guid>

					<description><![CDATA[InDesign ではすべての属性がスクリプト可能なのかと思いきや、フレームグリッドの基本情報である文字数／行数だけはなぜか簡単に読み書きする方法が用意されてない。日本語圏では頻繁に操作する箇所なので、自分でメソッドを作った。]]></description>
										<content:encoded><![CDATA[
<p>InDesign ではすべての属性がスクリプト可能なのかと思いきや、フレームグリッドの基本情報である文字数／行数だけはなぜか簡単に読み書きする方法が用意されてない。日本語圏では頻繁に操作する箇所なので、自分でメソッドを作った。</p>



<ul class="wp-block-list"><li>縦組み／横組み</li><li>テキストサイズ単位や組版単位などの違い</li><li>段数、段間</li><li>字間、行間</li><li>フレーム内マージン</li><li>フレームの線の太さ</li></ul>



<p>などの設定をすべて考慮して、テキストフレームの <code>geometricBounds</code> から文字数と行数を逆算する仕組み。</p>



<p><code>TextFrame.prototype</code> の拡張になっているので、外部ファイル扱い (.jsxinc) にしておいて <code>#import</code> で適宜読み込むのがおすすめ。</p>



<h2 class="wp-block-heading">使い方</h2>



<p>Getter と setter を兼ねた <code>gridDimensions()</code> というメソッドになっている。そのまま呼び出せば get、引数を渡して呼び出せば set として動作する。</p>



<p>フレームの種類が「フレームグリッド」に設定されているテキストフレームにのみ有効。フレームグリッドでない場合はエラーを返す。</p>



<span id="more-259"></span>



<h2 class="wp-block-heading">Get</h2>



<p>1段のサイズが 15文字 × 20行 のテキストフレーム <code>myTextFrame</code> があるとすると、</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">myTextFrame.gridDimensions();</code></pre>



<p>で <code>[15, 20]</code> という [文字数, 行数] 形式の array を返す。</p>



<h2 class="wp-block-heading">Set</h2>



<p>同じく [文字数, 行数] 形式の array を渡すことで、フレームグリッドのサイズを変更する。</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">myTextFrame.gridDimensions([16, 40]);</code></pre>



<p>これで、段数・字間などはそのままに、各段のサイズが 16文字 × 40行 に変更される。どちらか片方だけ変更したい場合は、<code>[0, 55]</code> のようにゼロを使えばそこだけ無変更。</p>



<h2 class="wp-block-heading">スクリプト</h2>



<p><a href="https://gist.github.com/2117706">InDesign: フレームグリッドの文字数と行数をget/set — Gist</a></p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">259</post-id>	</item>
		<item>
		<title>ScriptUI: Always honor Cancel/Esc in onClose validations</title>
		<link>https://jaguchi.com/blog/2011/09/scriptui-always-honor-cancelesc-in-onclose-validations/</link>
		
		<dc:creator><![CDATA[mirka]]></dc:creator>
		<pubDate>Fri, 30 Sep 2011 04:25:00 +0000</pubDate>
				<category><![CDATA[InDesign Scripts]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://jaguchi.com/2011/09/scriptui-always-honor-cancelesc-in-onclose-validations</guid>

					<description><![CDATA[When you need to validate user input in an edittext field, it’s not enough to do so in its onChange event — you must also validate it in the dialog’s onClose. It’s the only way to stop the dialog from closing after a click on the OK button, so that the user has a chance [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>When you need to validate user input in an <code>edittext</code> field, it’s not enough to do so in its <code>onChange</code> event — you must also validate it in the dialog’s <code>onClose</code>. It’s the only way to stop the dialog from closing after a click on the OK button, so that the user has a chance to correct their input. But what if that close event occurred because of an explicit user action to simply dismiss the dialog, like clicking the Cancel button?</p>



<span id="more-9"></span>



<h2 class="wp-block-heading">Please just let me Cancel</h2>



<p>By default, it seems that the close event does not distinguish between an “OK” close and a “Cancel” close. This can be problematic in input validations, because if your validator is designed to prevent the dialog from closing upon detection of faulty input, it should absolutely <em>not</em> do so in the event of an explicit “Cancel” (or Esc) close.<br/></p>



<h2 class="wp-block-heading">The Workaround</h2>



<p>Register event handlers for the Cancel button and Escape key, so that a <code>justExit</code> flag can be flipped when they occur. Then, the <code>onClose</code> handler can check the flag to determine whether it needs to validate and obstruct, or just get out of the way.</p>



<p>So first, define the boolean, and maybe a flagging function to flip it:<br/></p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">var justExitFlag = false;

function flipJustExit() {
	justExitFlag = true;
}</code></pre>



<p>Now add event listeners. Specifically, there are three events you’ll want to monitor:</p>



<p><strong>1. an Esc key press within the dialog window</strong></p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">myDialog.addEventListener("keydown", function (k) {
  if (k.keyIdentifier == "U+001B" /* Escape key */) {
    flipJustExit();
  }
});</code></pre>



<p><strong>2. a click on the Cancel button</strong></p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">myCancelBtn.addEventListener("click", flipJustExit);</code></pre>



<p><strong>3. a Spacebar key press on the Cancel button</strong></p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">myCancelBtn.addEventListener("keydown", function (k) {
  if (k.keyIdentifier == "U+0020" /* space key */) {
    flipJustExit();
  }
});</code></pre>



<p>Finally, your <code>onClose</code> handler might be something like this:</p>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">function validateInput() {
  if (justExitFlag) {
    return true; /* doesn’t stop dialog from closing */
  } else if (inputIsInvalid()) {
    alertUserWithCrazyPopup();
    return false; /* stops dialog from closing */
  }
}</code></pre>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">9</post-id>	</item>
		<item>
		<title>Randomize/reset measurementUnits</title>
		<link>https://jaguchi.com/blog/2011/09/indesign-randomizereset-measurementunits/</link>
		
		<dc:creator><![CDATA[mirka]]></dc:creator>
		<pubDate>Wed, 28 Sep 2011 08:34:00 +0000</pubDate>
				<category><![CDATA[InDesign Scripts]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://jaguchi.com/2011/09/indesign-randomizereset-measurementunits</guid>

					<description><![CDATA[Here is a set of three utility functions I use when debugging my InDesign scripts, to ensure that they work correctly in environments with different measurement units (points, millimeters, etc.) and ruler origins. If you haven’t already, read why managing measurement units is important. showCurrentSettings() Use this to display a quick alert of what the [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>Here is a set of three utility functions I use when debugging my InDesign scripts, to ensure that they work correctly in environments with different measurement units (points, millimeters, etc.) and ruler origins.</p>



<span id="more-10"></span>



<p>If you haven’t already, read <a href="https://jaguchi.com/blog/2011/09/indesign-controlling-measurementunits/">why managing measurement units is important</a>.</p>



<h2 class="wp-block-heading">showCurrentSettings()</h2>



<p>Use this to display a quick <code>alert</code> of what the current settings are.<br/></p>



<p>I often find that some of these settings get messed up at one point or another in a scripting session full of stepping, incomplete functions, or exiting mid-script. When things get weird, I check these settings.</p>



<h2 class="wp-block-heading">randomizeSettings()</h2>



<p>Randomize the measurementUnit settings with this function to create the most implausible environment ever. (Horizontal agates and vertical picas, anyone?) Then, check to see if your script works correctly. Randomize and check again. Two or three cycles of this is probably enough to see if your script fails to work around measurementUnit diversity.</p>



<h2 class="wp-block-heading">resetSettings()</h2>



<p>After your tampering is done, call this function to hit the reset button. The default values assigned in this function are mine (i.e., standard settings for a Japanese language user), so be sure to substitute in your own defaults.</p>



<h2 class="wp-block-heading">Try it:</h2>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">#target indesign
 
var sp = app.scriptPreferences;
var vp = app.documents[0].isValid ?
          document.viewPreferences : app.viewPreferences;
 
function showCurrentSettings() {
 
    alert(
        "Current settings:n" +
        "Script: " + sp.measurementUnit.toString() + "nn" +
 
        "Horizontal: " + vp.horizontalMeasurementUnits.toString() + "n" +
        "Vertical: " + vp.verticalMeasurementUnits.toString() + "n" +
        "Stroke: " + vp.strokeMeasurementUnits.toString() + "nn" +
 
        "Text: " + vp.textSizeMeasurementUnits.toString() + "n" +
        "Typo: " + vp.typographicMeasurementUnits.toString() + "nn" +
 
        "Ruler: " + vp.rulerOrigin.toString()
    );
}
 
function randomizeSettings() {
    var unitArray = [
        MeasurementUnits.POINTS,
        MeasurementUnits.PICAS,
        MeasurementUnits.INCHES,
        MeasurementUnits.INCHES_DECIMAL,
        MeasurementUnits.MILLIMETERS,
        MeasurementUnits.CENTIMETERS,
        MeasurementUnits.CICEROS,
        MeasurementUnits.CUSTOM,
        MeasurementUnits.AGATES,
        MeasurementUnits.U,
        MeasurementUnits.BAI,
        MeasurementUnits.MILS,
        MeasurementUnits.PIXELS,
        MeasurementUnits.HA,
        MeasurementUnits.AMERICAN_POINTS,
    ];
    var scriptUnitArray = unitArray.concat(AutoEnum.AUTO_VALUE);
    var strokeUnitArray = [
        MeasurementUnits.POINTS,
        MeasurementUnits.MILLIMETERS,
        MeasurementUnits.PIXELS,
    ];
    var textUnitArray = [
        MeasurementUnits.POINTS,
        MeasurementUnits.PIXELS,
        MeasurementUnits.Q,
        MeasurementUnits.AMERICAN_POINTS,
    ];
    var typoUnitArray = [
        MeasurementUnits.POINTS,
        MeasurementUnits.PIXELS,
        MeasurementUnits.HA,
        MeasurementUnits.AMERICAN_POINTS,
        MeasurementUnits.U,
        MeasurementUnits.BAI,
        MeasurementUnits.MILS,
    ];
    var rulerOriginArray = [
        RulerOrigin.SPREAD_ORIGIN,
        RulerOrigin.PAGE_ORIGIN,
        RulerOrigin.SPINE_ORIGIN,
    ];
 
    function getRandomItem(myArray) {
        var myIndex = Math.floor(Math.random() * myArray.length);
 
        return myArray[myIndex];
    }
 
    // Set random values to the preferences
    sp.measurementUnit = getRandomItem(scriptUnitArray);
 
    vp.horizontalMeasurementUnits = getRandomItem(unitArray);
    vp.verticalMeasurementUnits = getRandomItem(unitArray);
 
    vp.strokeMeasurementUnits = getRandomItem(strokeUnitArray);
    vp.textSizeMeasurementUnits = getRandomItem(textUnitArray);
    vp.typographicMeasurementUnits = getRandomItem(typoUnitArray);
 
    vp.rulerOrigin = getRandomItem(rulerOriginArray);
 
    showCurrentSettings();
}
 
function resetSettings() {
    var defaultUnits = MeasurementUnits.MILLIMETERS;
 
    sp.measurementUnit = AutoEnum.AUTO_VALUE;
 
    vp.horizontalMeasurementUnits = defaultUnits;
    vp.strokeMeasurementUnits = defaultUnits;
    vp.verticalMeasurementUnits = defaultUnits;
 
    vp.rulerOrigin = RulerOrigin.SPINE_ORIGIN;
 
    vp.textSizeMeasurementUnits = MeasurementUnits.Q;
    vp.typographicMeasurementUnits = MeasurementUnits.HA;
 
    showCurrentSettings();
}</code></pre>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">10</post-id>	</item>
		<item>
		<title>Controlling measurementUnits</title>
		<link>https://jaguchi.com/blog/2011/09/indesign-controlling-measurementunits/</link>
		
		<dc:creator><![CDATA[mirka]]></dc:creator>
		<pubDate>Wed, 28 Sep 2011 08:12:00 +0000</pubDate>
				<category><![CDATA[InDesign Scripts]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://jaguchi.com/2011/09/indesign-controlling-measurementunits</guid>

					<description><![CDATA[From pixels to points to ciceros, InDesign allows users to pick from 15 different measurement units to use in various places of their documents. Thanks to this versatility, it can sometimes be hard to predict how a script will act in a measurement unit environment even slightly different from yours. Where are these measurement units? [&#8230;]]]></description>
										<content:encoded><![CDATA[
<p>From pixels to points to ciceros, InDesign allows users to pick from 15 different measurement units to use in various places of their documents. Thanks to this versatility, it can sometimes be hard to predict how a script will act in a measurement unit environment even slightly different from yours.</p>



<h2 class="wp-block-heading">Where are these measurement units?</h2>



<p>There are three major places where measurement units could vary depending on the user environment:<br/></p>



<span id="more-11"></span>



<h2 class="wp-block-heading">1. document.viewPreferences</h2>



<p>Here you will find five important <code>measurementUnit</code> settings that may totally mess up your script if not accounted for.</p>



<p>The <code>measurementUnit</code> settings are:</p>



<ul class="wp-block-list"><li><code>horizontalMeasurementUnits</code></li><li><code>verticalMeasurementUnits</code></li><li><code>strokeMeasurementUnits</code></li><li><code>textSizeMeasurementUnits</code></li><li><code>typographicMeasurementUnits</code></li></ul>



<p>Although some documentation lists all 15 <code>MeasurementUnit</code> enums as possible values for these properties, the truth is that the <code>stroke</code>, <code>textSize</code>, and <code>typographic</code> properties only accept values from a specific subset of these enums. They are:</p>



<h3 class="wp-block-heading">Stroke measurement units</h3>



<ul class="wp-block-list"><li><code>MeasurementUnits.POINTS</code></li><li><code>MeasurementUnits.MILLIMETERS</code></li><li><code>MeasurementUnits.PIXELS</code></li></ul>



<h3 class="wp-block-heading">Text size measurement units</h3>



<ul class="wp-block-list"><li><code>MeasurementUnits.POINTS</code></li><li><code>MeasurementUnits.PIXELS</code></li><li><code>MeasurementUnits.Q</code></li><li><code>MeasurementUnits.AMERICAN_POINTS</code></li></ul>



<h3 class="wp-block-heading">Typographic measurement units</h3>



<ul class="wp-block-list"><li><code>MeasurementUnits.POINTS</code></li><li><code>MeasurementUnits.PIXELS</code></li><li><code>MeasurementUnits.HA</code></li><li><code>MeasurementUnits.AMERICAN_POINTS</code></li><li><code>MeasurementUnits.U</code></li><li><code>MeasurementUnits.BAI</code></li><li><code>MeasurementUnits.MILS</code></li></ul>



<h2 class="wp-block-heading">2. application.viewPreferences</h2>



<p>Same as <code>document.viewPreferences</code>, but like a default setting for new documents. The <code>document.viewPreferences</code> for each individual document will take precedence over these.</p>



<h2 class="wp-block-heading">3. application.scriptPreferences.measurementUnit</h2>



<p>This single property here affects some — but confusingly, not all — ways in which scripts handle units. In addition to all 15 <code>MeasurementUnit</code> enums, there is also a special context-dependent <code>AutoEnum.AUTO_VALUE</code> that uses whatever <code>MeasurementUnit</code> enum is assigned to a relevant property in the <code>viewPreferences</code>. <code>AutoEnum.AUTO_VALUE</code> is the default setting.</p>



<p>I consider this, the <code>scriptPreferences.measurementUnit</code>, to be the most dangerous measurementUnit pitfall. Why? Unlike the <code>viewPreferences</code>, there is no way a casual user of your script will be able to find or change this setting through the standard InDesign GUI. Thus, a reckless script that changes this setting and fails to revert it may alter the behavior of <em>all other scripts</em> in that user’s environment.</p>



<p>There are two things you should do to prevent this disaster. One is to be extremely serious about reverting any changes you make to this property. It’s a good idea to call the reverting routine in the <code>finally</code> of a <code>try</code> clause, so that an unexpected error will not abort the script without reverting.</p>



<p>The second is to script defensively, and make sure your script works regardless of the user’s <code>scriptPreferences.measurementUnit</code>. Do not assume it will always be set to <code>AutoEnum.AUTO_VALUE</code>. If the setting is going to affect any functionality in your script, alter it temporarily to something that is guaranteed to work as intended, and then revert to the original setting.</p>



<h2 class="wp-block-heading">Test your script</h2>



<p>Check to see how your script will work in <a href="/blog/2011/09/indesign-randomizereset-measurementunits">random measurement unit environments</a>.</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">11</post-id>	</item>
		<item>
		<title>scriptPreferencesをバッチ変更→復元</title>
		<link>https://jaguchi.com/blog/2011/04/indesign-alter-revert-scriptpreferences/</link>
		
		<dc:creator><![CDATA[mirka]]></dc:creator>
		<pubDate>Wed, 06 Apr 2011 00:02:00 +0000</pubDate>
				<category><![CDATA[InDesign Scripts]]></category>
		<category><![CDATA[JavaScript]]></category>
		<guid isPermaLink="false">https://jaguchi.com/2011/04/indesign-scriptpreferences%e3%82%92%e3%83%90%e3%83%83%e3%83%81%e5%a4%89%e6%9b%b4%e2%86%92%e5%be%a9%e5%85%83</guid>

					<description><![CDATA[スクリプト本体の実行前にユーザ設定値を変更しておき、本体実行後に元に戻す、という処理が頻出する。 特によくあるのが app.scriptPreferences の設定。 enableRedraw はほとんどの場合で disable したいだろうし、 measurementUnit は意図しない設定になっていると絶対値の組み込まれたアルゴリズムが不具合を起こす。 この辺の〈保持→変更→復元〉という処理を main() などに直接入れちゃうと煩雑なので、 scriptSettings.alter() と scriptSettings.revert() で本体ルーチンを挟むだけでいいようにメソッド化してみた。 以上を本体処理より前に declare しておくだけ。 メリット 変更不要のプロパティがあれば、 propStrs と tempVals の array から削るだけ エラーなどで途中 exit() する場面でも、その手前で scriptSettings.revert() を call するだけ scriptSettings の declare 時ではなく、 alter() を call した時点での設定値が保存される、という点にだけ注意。]]></description>
										<content:encoded><![CDATA[
<p>スクリプト本体の実行前にユーザ設定値を変更しておき、本体実行後に元に戻す、という処理が頻出する。</p>



<p>特によくあるのが <code>app.scriptPreferences</code> の設定。 <code>enableRedraw</code> はほとんどの場合で disable したいだろうし、 <code>measurementUnit</code> は意図しない設定になっていると絶対値の組み込まれたアルゴリズムが不具合を起こす。</p>



<p>この辺の〈保持→変更→復元〉という処理を <code>main()</code> などに直接入れちゃうと煩雑なので、 <code>scriptSettings.alter()</code> と <code>scriptSettings.revert()</code> で本体ルーチンを挟むだけでいいようにメソッド化してみた。</p>



<span id="more-12"></span>



<pre class="wp-block-code"><code lang="javascript" class="language-javascript">// Make alter and revert methods
var scriptSettings = {
  alter: function() {
    var prefs = app.scriptPreferences;
    var propStrs = ["enableRedraw", "measurementUnit", "userInteractionLevel"];
    var tempVals = [false, AutoEnum.AUTO_VALUE, UserInteractionLevels.INTERACT_WITH_ALL]
    var originalVals = [];

    for (var i = 0; i &lt; propStrs.length; i++) {
      originalVals.push(prefs[propStrs[i]]); // Retain original values
      prefs[propStrs[i]] = tempVals[i]; // Change to temporary values
    }

    // Make a function to revert to original values
    this.revert = function() {
      for (var i = 0; i &lt; propStrs.length; i++) {
        prefs[propStrs[i]] = originalVals[i];
      }
    };
  }
};</code></pre>



<p>以上を本体処理より前に declare しておくだけ。</p>



<h2 class="wp-block-heading">メリット</h2>



<ol class="wp-block-list"><li>変更不要のプロパティがあれば、 <code>propStrs</code> と <code>tempVals</code> の array から削るだけ</li><li>エラーなどで途中 <code>exit()</code> する場面でも、その手前で <code>scriptSettings.revert()</code> を call するだけ</li></ol>



<p><code>scriptSettings</code> の declare 時ではなく、 <code>alter()</code> を call した時点での設定値が保存される、という点にだけ注意。</p>
]]></content:encoded>
					
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">12</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: jaguchi.com @ 2026-06-13 05:05:20 by W3 Total Cache
-->