処理対象ページをユーザーに文字入力させた場合に、
X1,X2,X3,Y-Z
のようなフォーマットを解釈して、処理対象ページ番号をすべて展開するAppleScriptです。
もともと16年前に作成したScriptを書き換えたものです。オリジナルもとくに現行環境で問題なく動いているのですが、ソートルーチンを高速なものに差し替えています。ただし、処理データの量がたいして多くないことを前提としているため(数万ページ指定しないとCocoaのソートルーチンを利用するメリットがない)、Cocoaのソートルーチンは使っていません。
AppleScript名:ページ範囲処理 v2 |
— – Created by: Takaaki Naganoya – Created on: 2005/11/04 – Modified on: 2021/02/14 — – Copyright © 2021 Piyomaru Software, All Rights Reserved — use AppleScript version "2.4" — Yosemite (10.10) or later use framework "Foundation" use scripting additions set parsedList to parsePageRangeString("1-10, 35, 37, 39, 100-110") of me –> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 35, 37, 39, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110} –ページ範囲指定のテキスト(多分)を展開して実際のページ数の一覧リストに on parsePageRangeString(pageText as string) set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to ("," as string) set aList to text items of pageText set AppleScript’s text item delimiters to curDelim set eList to {} repeat with i in aList set j to contents of i if ("-" as string) is in j then set eList to eList & parseByHyphen(j) of me else set the end of eList to j as number end if end repeat set eList to removeDuplicates(eList) of me set eList to shellSortAscending(eList) of me return eList end parsePageRangeString –重複部分を削除 on removeDuplicates(aList) set newList to {} repeat with i from 1 to (length of aList) set anItem to item 1 of aList set aList to rest of aList if {anItem} is not in aList then set end of newList to anItem end repeat return newList end removeDuplicates –ハイフンを実際の数値に変換 on parseByHyphen(aText) set aText to aText as string set curDelim to AppleScript’s text item delimiters set AppleScript’s text item delimiters to ("-" as string) set aList to text items of aText set AppleScript’s text item delimiters to curDelim set sList to {} repeat with i from ((item 1 of aList) as number) to ((item 2 of aList) as number) set the end of sList to i end repeat return sList end parseByHyphen –入れ子ではないリストの昇順ソート on shellSortAscending(aSortList) script oBj property list : aSortList end script set len to count oBj’s list’s items set gap to 1 repeat while (gap ≤ len) set gap to ((gap * 3) + 1) end repeat repeat while (gap > 0) set gap to (gap div 3) if (gap < len) then repeat with i from gap to (len – 1) set temp to oBj’s list’s item (i + 1) set j to i repeat while ((j ≥ gap) and (oBj’s list’s item (j – gap + 1) > temp)) set oBj’s list’s item (j + 1) to oBj’s list’s item (j – gap + 1) set j to j – gap end repeat set oBj’s list’s item (j + 1) to temp end repeat end if end repeat return oBj’s list end shellSortAscending |
More from my site
(Visited 46 times, 1 visits today)