指定フォルダ以下のすべてのAppleScript書類(通常書類、バンドル書類)をSpotlightで検出して、各書類の冒頭に書いてあるヘッダーコメントをすべて書き換えて保存するAppleScriptです。
–> Download scriptHeaderReplacer.zip (Code-Signed executable AppleScript applet with Libraries in its bundle)
AppleScriptの自動組み立て&自動書き出しといった処理は、macOS 10.4ぐらいの時期、あるいはScript DebuggerをコントロールしてClassic MacOSの時代からやっていた記憶があります。AppleScriptをAppleScriptで書くとか書き換えるといった処理は、割と重箱の隅をつつくような(メーカー側も想定していないわけではないだろうけれど、そんなに検証してなさそう)ものなので、未知の問題に遭遇することの多いものです。ただし、安定して動作させられれば得るものも大きいです。
CotEditorの配布用サンプルScriptをまとめる際に、ヘッダーコメント部分を統一する作業がなにげに大変でした。そこで、本Scriptを作成して一括してAppleScript書類のヘッダーコメント統一を行えるようにしました。60ファイル程度あるので、すべて手作業で書き換えるのは非効率的です(サンプルを書くよりも本Scriptを書くほうが時間がかかりました。でも、一度書けば2度目以降は自動で処理できます)。
本AppleScriptでは、Script EditorをコントロールしてAppleScript書類の書き換えを行っています。ただし、AppleScript書類自体の詳細な書式情報はCocoaの機能を用いて抽出しています。Script Editorで書き換えを行っているのは「目で見て動作を確認できる」からで、それ以外にはScript Editorをコントロールするメリットはありません(Cocoa側の機能を用いてScriptの修正と保存ができれば、すぐにでも本ワークフローから外したい)。
AppleScriptの構文色分け書式にアクセスして構文要素を文字色で区分けしているため、本Scriptの実行環境で構文色分け書式がデフォルトのままとか、各構文要素の指定色でごくごく似たものがあったような場合には正常に動作しません。
Script名称が「AppleScript書類冒頭のコメントを削除する v4」になっているのは、コメントを追加するプログラムとコメントを削除するプログラムの2系統のものを別々に作って、最終的に「コメントを削除するプログラム」に機能を統合したためです。
メインのScriptの内容のみ掲載していますが、(巨大な)ライブラリ入りのScript掲載は頭が痛いところです。とりあえず、実行して試せるということを重視してみました。
AppleScript名:AppleScript書類冒頭のコメントを削除する v4.scptd |
— – Created by: Takaaki Naganoya – Created on: 2019/10/03 — – Copyright © 2019 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions use asAttr : script "asAttrRecLib" –AppleScript書類をattribute runs的に書式データ化するライブラリ use asCom : script "asCommentLib" –AppleScript書類から構文色分け情報にもとづいてコメントを抽出するライブラリ use dsipTV : script "displayTextView" –NSTextViewで比較的長いテキスト入力(元テキストの修正程度を行うライブラリ use script "Metadata Lib" version "2.0.0" –https://macosxautomation.com/applescript/apps/Script_Libs.html –新しいヘッダーコメント(ダイアログ上でさらに変更可能) set hRes to "– – Created by: Takaaki Naganoya – Created on: 2019/10/06 — – Copyright © 2019 Piyomaru Software, All Rights Reserved – http://www.piyocast.com " –ヘッダーの入力・修正を促すset aHeader to (input text view hRes main message "Input New Header" sub message "Input AppleScript header text to replace" with properties {font name:"HiraMinProN-W6", size:20.0, width:800, height:200}) –AppleScriptの構文色分け設定から、コメントのカラーを取得する(色書式検索用 R、G、B値が1つの文字列にまとまっている形式) set comColStr to (getCommentColStr() of asCom) as string –指定フォルダ以下のAppleScript書類をSpotlight検索ですべてリストアップ set aFol to choose folder with prompt "Select Root Folder" set resList to perform search in folders {aFol} predicate string "kMDItemContentType IN %@" search arguments {"com.apple.applescript.script", "com.apple.applescript.script-bundle"} –各AppleScript書類でループ repeat with i in resList –ファイルをScript Editorでオープン set j to POSIX file (contents of i) tell application "Script Editor" try open j on error display dialog "Error in opening" return end try –ヘッダーコメントの書き換え処理 tell front document set aCon to contents if aCon does not start with "–" then –ヘッダーコメントが存在しなかった場合 set aCon to aHeader & aCon set contents to aCon else –ヘッダーコメントが存在した場合 –既存のヘッダーを外してヘッダーコメントを付加 –AppleScript書類の冒頭のヘッダーコメントの範囲を書式情報から取得する set tmpLoc to retHeaderCommentRange(comColStr, j as alias, aHeader) of me delay 0.5 —ないとうまく動作しない –既存のヘッダーコメントのテキストを取得する set aText to (text 1 thru tmpLoc of aCon) –ヘッダーコメントの削除 set pStr to repChar(aCon, aText, "") of me set contents to aHeader & pStr end if check syntax delay 0.5 —ないとうまく動作しない end tell close every document with saving delay 0.5 —ないとうまく動作しない end tell end repeat –上記のclose命令をすり抜ける書類があるので、最後にまとめてクローズ tell application "Script Editor" close every document with saving end tell –パスで与えられたAppleScript書類の冒頭からのヘッダーコメントの範囲を求める on retHeaderCommentRange(comColStr as string, aFile, aHeader as string) set aList to (getAttrRecFromASPath(aFile) of asAttr) as list set aCount to 1 set prevRec to {} set iRange to missing value set delRange to 0 repeat with i in aList set tmpCol to (colorStr of i) as string set tmpStr to (stringVal of i) as string set tmpRange to (location of itemRange of i) as integer if (tmpCol is not equal to comColStr) and (tmpStr does not end with (return & return)) and ((location of itemRange of i) > ((length of aHeader) – 3)) then –改行コードが2つ連続した場合に同じ色情報になるのでこのように記述(重要) set iRange to itemRange of prevRec exit repeat else set aLoc to (location of itemRange of i) as integer set aLen to (length of itemRange of i) as integer set delRange to delRange + aLoc + aLen end if set aCount to aCount + 1 copy (contents of i) to prevRec end repeat if iRange = missing value then error "Comment not found " set tmpLoc to (location of iRange) + (|length| of iRange) return tmpLoc end retHeaderCommentRange –文字置換 on repChar(origText as string, targChar as string, repChar as string) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to targChar set tmpList to text items of origText set AppleScript’s text item delimiters to repChar set retText to tmpList as string set AppleScript’s text item delimiters to curDelim return retText end repChar |