02)AppleScriptの構文の最近のブログ記事

08)ちょっとした便利スクリプト

| コメント(0) | トラックバック(0)

アスキーナンバーで文字を指定したり逆にアスキーナンバーを調べます。

set myChar to ASCII character (34)
set myNum to ASCII number "A"
--ちなみに改行はreturnでタブはtabです。

下記はクリップボードを操作します。コピーした文字列を取り出したり、クリップボードにテキストを入れて、ペーストすることができます。

set the clipboard to "123"--クリップボードに文字列を入れる
set myTable to the clipboard--クリップボードから文字列を取り出す

下記はSORTです。早くありませんが、リストを並べ替えます。

on SORT(myList)
        set myCount to count items of myList
        repeat
                set myFLG to false
                repeat with N from 1 to myCount - 1
                        set buf1 to item N of myList
                        set buf2 to item (N + 1) of myList
                        if buf1 < buf2 then
                                set item N of myList to buf2
                                set item (N + 1) of myList to buf1
                                set myFLG to true
                        end if
                end repeat
                if myFLG is false then
                        exit repeat
                end if
        end repeat
        return myList
end SORT

07)エラー処理など

| コメント(0) | トラックバック(0)

try文でエラー処理
IllustratorにAppleScriptを送信する際テキストフレームにテキストをセットするスクリプトだったとします。そのときもしテキストフレームが無ければエラーになります。下記try〜on error〜end tryでエラー処理をすることができます。

with timeout of 1 second--タイムアウトを1秒に設定
	tell application "Adobe Illustrator"
		tell document 1
			try--tryスタート
				set contents of text frame 1 to "あいうえお"
			on error--もしエラーがあれば下を実行
				display dialog "失敗しました。"
			end try--try文終わり
		end tell
	end tell
end timeout--タイムアウト設定ここまで

タイムアウトの設定
IllustratorやInDesignなどは連続してAppleScriptを送信しつづけるとだんだん遅くなったりEPSを配置する時に「配置できません」とダイアログが表示されたりします。またスクリプトを送信しているのに応答しない事もしばしばあります。そういう時AppleScriptは応答待ちになってしばらく止まるのですがwith timeout of 1 secondをすると1秒だけしか待たないようになります。

with timeout of 1 second

AppleScript's text item delimitersはAppleScript内部の区切り文字の設定です。例えば区切り文字を","にして"あいう,かきく,さしす"という文字列をリストに型変換すると{"あいう","かきく","さしす"}というリストになります。コンマ区切りの文字を読み込みリストにするのに大変便利です。
区切り文字を"%"に変更してリストを文字列に型変換すると区切り文字をつかってリストがつながります。先ほどのリストは"あいう%かきく%さしす"になります。これを利用してやれば検索置換が出来ます。

set myStr to "あいう,かきく,さしす"
set OriginalDelimiters to AppleScript's text item delimiters
--元々の区切り文字を保存しておく。決まり事として必ず使う
set findStr to "," --検索文字set repStr to "%" --置換文字
set AppleScript's text item delimiters to {findStr}--区切り文字を","にする
set myStr to text items of myStr--","区切りでリストにする
set AppleScript's text item delimiters to {repStr}--区切り文字を"%"にする
set myStr to myStr as string--"%"区切りでテキストにする
set AppleScript's text item delimiters to OriginalDelimiters
--もともとの区切り文字に戻しておく。これも決まり事。
display dialog myStr
=>あいう%かきく%さしす

検索置換のハンドラ(関数)replaceAllです。これをスクリプトの一部に書いておいてset myStr to my replaceAll("元の文字", "検索文字", "置換文字")とすれば検索置換できます。大量に検索置換すると(もしくは特定の文字があると)文字化けする時があります。

set myStr to "あいう,かきく,さしす"
set myStr to my replaceAll(myStr, ",", "%")
display dialog myStr

on replaceAll(motoStr, findStr, repStr)
	set OriginalDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {findStr}
	set motoStr to text items of motoStr
	set AppleScript's text item delimiters to {repStr}
	set motoStr to motoStr as string
	set AppleScript's text item delimiters to OriginalDelimiters
	return motoStr
end replaceAll

下記ハンドラでset myCount to my countFields("元の文字", "区切り文字")で変数myCountには元の文字を 区切り文字で分割した数が入ります。
set myStr to my nthFields("元の文字", "区切り文字", アイテム数)で元の文字を 区切り文字で分割した2つめ等を取り出せます。

set myStr to "あいう,かきく,さしす"
set myCount to my countFields(myStr, ",")
set myStr to my nthFields(myStr, ",", 2)
display dialog "合計" & (myCount as string) & "個で2つめは:" & myStr

on countFields(myStr, sep)
	set OriginalDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {sep}
	set myStr to text items of myStr
	return count myStr
end countFields
on nthFields(myStr, sep, num)
	set OriginalDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to {sep}
	set myStr to text items of myStr
	set AppleScript's text item delimiters to OriginalDelimiters
	return item num of myStr
end nthFields

エントリー一覧

OpenID対応しています OpenIDについて
Powered by Movable Type 7.902.0
漢字イラストロジック-脳トレ京
FREE ONLINE SUDOKU