Archive for the 'ユーザー操作(user interact)' Category

02/10 フォルダの選択(内容確認つき)v2

ちょっと気の利いた複数folder選択」を発展させ、選択したフォルダの一覧を確認表示するようにしたAppleScriptです。

最大の見どころは、フォルダパスを一覧表示するさいに、どのパスかを示す文字の長さをそろえて、フォルダパスの内容確認を行いやすくした「umekusaKit」です。

dialog1.jpg

これで、選択した複数パスの一覧を表示し、処理前に確認するように書いておけば、ささいなミスもなくなるに違いありません。

ちなみに、この一覧表示で「Cancel」ボタンをクリックすると……

dialog2.jpg

のように、処理を中断します。

どうも、プログラムを見直してみると……もっと汎用的なサブルーチンに仕立てて、もっと簡単に利用できるようにできそうな気がします。まだ、手直しできる余地が大きそうですが、使用頻度はあまり高くなさそうなので……このままかもしれません。

スクリプト名:フォルダの選択(内容確認つき)v2
property aFol : missing value
property bFol : missing value
property cFol : missing value

set a1Mes to “コマの書類の親フォルダ”
set b1Mes to “ページアップ先のフォルダ”
set c1Mes to “ページ台紙が入っているフォルダ”

set a2Mes to “(komaFolderServerPath)を選択”
set b2Mes to “(pageUpOutputTargetPath)を選択”
set c2Mes to “(pageDaishiFolder)を選択”

if aFol = missing value then
  set aFol to choose folder with prompt (a1Mes & a2Mes)
else
  set aFol to choose folder default location aFol with prompt (a1Mes & a2Mes)
end if

if bFol = missing value then
  set bFol to choose folder with prompt (b1Mes & b2Mes)
else
  set bFol to choose folder default location bFol with prompt (b1Mes & b2Mes)
end if

if cFol = missing value then
  set cFol to choose folder with prompt (c1Mes & c2Mes)
else
  set cFol to choose folder default location cFol with prompt (c1Mes & c2Mes)
end if

–設定内容の確認表示
set aaList to {a1Mes, b1Mes, c1Mes}
set {r1, r2, r3} to makeSameString(aaList, “ ”) of umekusaKit –ウメクサ文字には全角スペースを指定

set aList to {r1 & “ = ” & aFol as string, r2 & “ = ” & bFol as string, r3 & “ = ” & cFol as string}
set defaultItem to contents of item 1 of aList

–最終確認
set aRes to choose from list aList with prompt “この設定でよいですか?” default items defaultItem with title “指定内容の最終確認”
if aRes = false then
  display dialog “処理を中止します” buttons {“OK”} default button 1 with icon 1 with title “処理中止”
  
return
end if

–このあとにメインの処理が入る

–文字リストをchoose from listで表示させる際に長さを揃えるために作った
script umekusaKit
  –最大の長さの文字列に合わせて、ウメクサ文字を後ろに追加する
  
–fillCharは1文字であることを期待している
  
–全角文字と半角文字が「混在しない」ことを期待している。あるいは、半角文字の文字長が一緒とか
  
on makeSameString(aList, fillChar)
    –リスト中の文字列長の最大値を求める
    
set lenList to {}
    
repeat with i in aList
      set the end of lenList to length of (contents of i)
    end repeat
    
set maxLen to item (maximumItemNoFromList(lenList) of me) of lenList
    
    
–最大長に合わせて各項目の末尾にfillCharを追加する
    
set retList to {}
    
repeat with i in aList
      set newStr to contents of i
      
set aLen to length of i
      
repeat with ii from 1 to (maxLen - aLen)
        set newStr to newStr & fillChar
      end repeat
      
set the end of retList to newStr
    end repeat
    
    
return retList
  end makeSameString
  
  
–最大値の項目番号を取得する
  
on maximumItemNoFromList(nList)
    script o
      property nl : nList
      
property itemNo : 1
    end script
    
    
set max to item 1 of o’s nl
    
repeat with i from 2 to (count nList)
      set n to item i of o’s nl
      
if n > max then
        set max to n
        
set o’s itemNo to i
      end if
    end repeat
    
return o’s itemNo
  end maximumItemNoFromList
end script

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

01/30 ちょっと気の利いた複数folder選択

仕事用にかなり凝ったAppleScriptのプログラムを作成したような場合には、処理対象のデータを自由に指定できるよう、処理先をchoose folderやchoose fileで指定することがあります。プログラム内にパスを固定で記述するよりも自由度が高く、ユーザーフレンドリーな処理になります。

ただ……1つのAppleScript中でいくつもフォルダを選択する必要があったりすると、そのたびにフォルダ選択ダイアログで処理対象のフォルダを指定しなければなりません。しかも、Folder Aを選択したあとでFolder Bを選択しようとすると、Folder Aの場所からスタートしなくてはなりません。

これはけっこう面倒です。

dialog1.jpeg

dialog2.jpeg

そこで、choose folderで一度フォルダを選択したら、propertyの値に保持しておいて、各フォルダ選択ダイアログで個別に記録。二度目からはそれぞれ前回指定したフォルダを勝手に指定するよう、些細な改善を行ってみました。

その結果、大幅な操作性の改善を行うことができ、いくつフォルダ選択ダイアログが出てきても、うっとおしくなくなりました(たいていにおいて、前回と同じ場所を指定することのほうが多く、目視で確認してリターンキーを押していくだけ)。

スクリプト名:ちょっと気の利いた複数folder選択
property folderA : missing value
property folderB : missing value

if folderA is not equal to missing value then
  –前回の選択内容を使用
  
set folderA to choose folder default location folderA with prompt “folder Aを選択”
else
  –はじめて実行する場合など
  
set folderA to choose folder with prompt “folder Aを選択”
end if

if folderB is not equal to missing value then
  –前回の選択内容を使用
  
set folderB to choose folder default location folderB with prompt “folder Bを選択”
else
  –はじめて実行する場合など
  
set folderB to choose folder with prompt “folder Bを選択”
end if

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

01/17 リストから選択してアイテム番号を返す〜複数選択対応

choose from listで複数選択を行いつつ、選択した項目がそれぞれリストの何アイテム目かの情報を返すAppleScriptです。

以前に、複数選択ではないバージョンは作ってあったのですが、Omni Outlinerから選択部分を取得するAppleScriptを作成する際に、即興で作ったものです。

スクリプト名:リストから選択してアイテム番号を返す〜複数選択対応
set aList to {“red”, “blue”, “green”, “white”}
set aMes to “項目を選択してください(Command-クリックで複数選択可能)”
set aRes to retMultipleItemFromListByItemNo(aList, aMes) of me

–リストから選択してアイテム番号を返す(複数項目選択対応)
on retMultipleItemFromListByItemNo(aList, aMes)
  set aRes to choose from list aList with prompt aMes with multiple selections allowed
  
if aRes = false then return 0
  
  
set hitList to {}
  
repeat with i1 in aRes
    set aRes to contents of i1
    
set hitNum to 1
    
repeat with i in aList
      set j to contents of i
      
if j is equal to aRes then
        exit repeat
      end if
      
set hitNum to hitNum + 1
    end repeat
    
set the end of hitList to hitNum
  end repeat
  
return hitList
end retMultipleItemFromListByItemNo

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

05/08 リストの中に指定のタグ付きテキスト要素によるトリミングを行う。結果はリストで返す

リスト形式で存在しているデータで、実はその中にInfo.plistのような構造のタグ付きテキストが入っているような場合の処理で作成したサブルーチンです。

つまり……AppleScriptで作ったアプリケーションがタグ付きテキスト形式でデータを保存していて、その保存先がテキストファイルではないために、取得するとリストで返ってくるようなケースに対応するためのものです。

リストをまるごとテキストに変換して、テキストとして処理してもよかったのですが……リストのまま扱えると便利なケースが多いので、リストのまま処理してみた次第です。応用範囲がなさそうでいて、その実、便利に使えています。

スクリプト名:リストの中に指定のタグ付きテキスト要素によるトリミングを行う。結果はリストで返す
set aList to {"<index>", "script1", "script2", "script3", "<a>", "test script G1", "</a>", "</index>"}

set {startTagText, endTagText} to {"<a>", "</a>"}
set sList to trimListItemByTaggedText(aList, startTagText, endTagText) of me
if sList = false then
  display dialog "Error Message"
end if
sList
> {"test script G1"}

リストの中に指定のタグ付きテキスト要素によるトリミングを行う。結果はリストで返す
on trimListItemByTaggedText(aList, startTagText, endTagText)
  –そもそも、指定のタグ付きテキストが、開始/終了テキストともに与えられたリストの中に入っていなかった場合、処理終了
  
if startTagText is not in aList or endTagText is not in aList then return false
  
  –
  
set sPos to retItemNoInList(aList, startTagText) of me
  
set ePos to retItemNoInList(aList, endTagText) of me
  
  –
startTagTextとendTagTextの間に何も存在しなかった場合はエラー
  
if sPos + 1 = ePos then return false
  
  –
結果を取り出す
  
set resList to items (sPos + 1) thru (ePos - 1) of aList
  
return resList
end trimListItemByTaggedText

指定項目のリスト中におけるシリアルサーチ
on retItemNoInList(aList, a)
  set aCount to 1
  
set hitF to false
  
repeat with i in aList
    if a = contents of i then
      set hitF to true
      
exit repeat
    end if
    
set aCount to aCount + 1
  end repeat
  
  
if hitF = true then
    return aCount
  else
    return false
  end if
end retItemNoInList

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に

03/09 chooseFromListでアイテム番号を返す

リストに入れておいたアイテムを一覧表示させて選択する「choose from item」、選択肢として与える内容はリストなので、選択した後に何番目のアイテムが選択されたかを知りたい場合が多いわけですが、返ってくるのは選択された項目の内容そのもの………。というわけで、リストの何番目の項目が選択されたのかを求めるサブルーチンを作成。これも、かなり使用頻度の高いものです。

スクリプト名:chooseFromListでアイテム番号を返す
set aList to {”あか“, “あお“, “きいろ“}
set aMes to以下の中から選択してください。
set a to retNumberFromChooseFromList(aList, aMes) of me
a

リストからの選択ダイアログで、選択したアイテムのアイテムナンバーを返す
on retNumberFromChooseFromList(aList, aMes)
  set aRes to choose from list aList with prompt aMes
  
if aRes = false then return false
  
set aRes to (contents of aRes) as string
  
set itemCount to 1
  
repeat with i in aList
    set j to contents of i
    
if j is equal to aRes then
      return itemCount
    end if
    
set itemCount to itemCount + 1
  end repeat
end retNumberFromChooseFromList

▼新規書類に ▼カーソル位置に ▼ドキュメント末尾に