Last active
December 24, 2025 07:50
-
-
Save monokano/08c211a5cf03fcbb21a82a780ddb9903 to your computer and use it in GitHub Desktop.
選択テキストの数字とアルファベットのみを半角(欧字)にするInDesign用スクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 選択テキストを英数半角にする | |
| function toHalfWidth(theRange) { | |
| // 初期化 | |
| app.findTransliteratePreferences = NothingEnum.nothing; | |
| app.changeTransliteratePreferences = NothingEnum.nothing; | |
| // 文字種変換の設定 | |
| app.findTransliteratePreferences.findCharacterType = FindChangeTransliterateCharacterTypes.FULL_WIDTH_ROMAN_SYMBOLS; | |
| app.changeTransliteratePreferences.changeCharacterType = FindChangeTransliterateCharacterTypes.HALF_WIDTH_ROMAN_SYMBOLS; | |
| // 文字種変換を実行 | |
| for (var i = 0; i < theRange.length; i++) { | |
| theRange[i].changeTransliterate(); | |
| } | |
| // 初期化 | |
| app.findTransliteratePreferences = NothingEnum.nothing; | |
| app.changeTransliteratePreferences = NothingEnum.nothing; | |
| } | |
| // メイン処理関数 | |
| function main() { | |
| // 再描画を無効 | |
| app.scriptPreferences.enableRedraw = false; | |
| // 検索設定を保存(注:スタイルグループ内のスタイルが格納されないバグあり) | |
| var saveFindPrefs = app.findGrepPreferences.properties; | |
| var saveChangePrefs = app.changeGrepPreferences.properties; | |
| try { | |
| // 初期化 | |
| app.findGrepPreferences = NothingEnum.nothing; | |
| app.changeGrepPreferences = NothingEnum.nothing; | |
| // 現在の選択を取得 | |
| var currentSelection = app.selection; | |
| if (currentSelection.length > 0) { | |
| // 英数字のパターンを設定 欧字和字を区別しない | |
| app.findGrepPreferences.findWhat = "[\\l\\u\\d]+"; | |
| // 選択範囲で検索実行 | |
| var searchTarget = currentSelection[0]; | |
| var foundItems = searchTarget.findGrep(); | |
| if (foundItems.length > 0) { | |
| // 見つかったテキストを変換 | |
| toHalfWidth(foundItems); | |
| } | |
| } | |
| } finally { | |
| // 検索設定を復元 | |
| app.findGrepPreferences.properties = saveFindPrefs; | |
| app.changeGrepPreferences.properties = saveChangePrefs; | |
| // 再描画を有効に戻す | |
| app.scriptPreferences.enableRedraw = true; | |
| } | |
| } | |
| // doScriptを使用してUndo履歴をまとめる | |
| app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "英数字を半角に変換"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment