2009年3月アーカイブ

30)部分角版処理

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

InDesignCS3で2つのオブジェクトの位置とサイズを合わせます。
付属の画像を見ていただけるとわかりやすいと思います。
チラシなどの部分角判処理用に作りました。

回転していたらだめです。

配置画像を背面に合わす.jsx
配置画像を前面に合わす.jsx

たぶん他のバーションのInDesignでも動くと思います。

ダウンロードはこちらbubun.zip

InDesign13

07)部分角判処理

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

IllustratorCS3で2つのオブジェクトの位置とサイズを合わせます。
付属の画像を見ていただけるとわかりやすいと思います。
チラシなどの部分角判処理用に作りました。

回転してたらだめです。

IllustratorCS3で2つの画像を選択します。
このときクリッピングマスクは選択しないでください。
「2つを前面に合わせる.jsx」を実行すると前面の画像の位置とサイズに背面の画像が移動します。「2つを背面に合わせる.jsx」はその逆です。

ダウンロードはこちらbubun.zip

Illustrator7

06)配置画像・埋込画像を選択

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

IllustratorCS3で配置画像か埋め込み画像を選択します。
おそらくCS2かCS4でも、他にも10やCSでも動くと思います。

Mac専用です。

ダウンロードはこちらselectimage.zip

Illustrator6

30)図とキャプ間隔調整

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

InDesignCS3で選択された2つのオブジェクトの
間隔を1mmとか入力された値に調整します。
CS2、CS4でも動くと思います。
図とキャプションの調整用に作りました。

数値を入力するとその値分の間隔になります。
ptでもmmでも環境依存なんでしょうね。

ダウンロードはこちらfigurecap.zip

InDesign12

12)表の中の罫線を操作

| コメント(3) | トラックバック(1)

InDesignCS2、CS3で表の中の罫線を操作します。
(CS4も動くかもしれません。)
コントロールパレットから線幅を変更するのが面倒なので
1クリックで動くものを作りました。
適当にカスタマイズして使っていただければと思います。
連結したセルが混じるとうまく動かない場合があります。

MacはAppleScript
WinはVBScriptです。

0.0_中_タテ 選択セルの中のタテ罫を0mmにします。
0.1_中_タテ 選択セルの中のタテ罫を0.1mmにします。
0.2_中_タテ 選択セルの中のタテ罫を0.2mmにします。

0.0_中_ヨコ 選択セルの中のヨコ罫を0mmにします。
0.1_中_ヨコ 選択セルの中のヨコ罫を0.1mmにします。
0.2_中_ヨコ 選択セルの中のヨコ罫を0.2mmにします。

0.0_左右   選択セルの左右を0mmにします。
0.1_左右   選択セルの左右を0.1mmにします。
0.2_左右   選択セルの左右を0.2mmにします。

0.0_上下   選択セルの上下を0mmにします。
0.1_上下   選択セルの上下を0.1mmにします。
0.2_上下   選択セルの上下を0.2mmにします。

ダウンロードはこちら

JS版MacWin両方用TableTools_JS.zip

上のJS版が動かなければ下のを試してください。

Mac用TableTools_Mac.zip

Windows用TableTools_WIN.zip

なぜかJavaScriptで選択範囲のセルに対してinnerColumneStrokeWeightがうまくうごかなかったのです。

InDesign11

文字化けなおしました。

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

Windows環境でDownloadしたzipファイルを解凍すると日本語が文字化け。
すいません。だいじょうぶかなぁと思ってたのですが。ついつい面倒でMacで圧縮してアップしていました。本日訂正しておきました。

拡張子を変更

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

選択フォルダ内のファイル、またはドロップされたファイルの拡張子を変更します。

拡張子を表示->拡張子を表示します。
拡張子をトル->拡張子をとります。元々のファイル名はコメントに行きます。
拡張子をつける->入力された拡張子にします。元々のファイル名はコメントに行きます。
拡張子をもとに戻す->コメントに入っていたファイル名に戻します。

AppleScriptなのでMac専用です。

OS9時代にせうぞーさんが便利なリネームツールを作られていたのですが、
OSXでは無かった(と思う)ので簡易版をつくりました。

■■■使い方■■■
2つのファイルを比べる(OS X)を起動して
古いファイル、新しいファイルをドロップします。
比べるボタンをクリックすると新旧ファイルで内容が違う行番号と行を表示します。

ダウンロードはこちらextchange.zip

else2

2つのファイルを比べる

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

2つのテキストファイルを調べて内容が違う行を表示します。
ファイルはShift-JIS専用です。
AppleScriptでプロパティを取って調べたりする時にたまーに使っています。
ただそれだけのソフトです。

■■■使い方■■■
2つのファイルを比べる(OS X)を起動して
古いファイル、新しいファイルをドロップします。
比べるボタンをクリックすると新旧ファイルで内容が違う行番号と行を表示します。

ダウンロードはこちらhikaku.zip

else1

InDesignCS3で選択範囲、ストーリー、ドキュメント全て
のいずれかに検索置換を行います。

検索置換大文字Aをaに.jsx

検索置換小文字aをAに.jsx

検索置換全角数字を半角に.jsx

ダウンロードはこちらfindrep2.zip

InDesign10

InDesignCS3で選択範囲、ストーリー、ドキュメント全て
のいずれかに検索置換を行います。
(1)〜(20)まで
(日)〜(土)までを1文字に置換します。

ダウンロードはこちらfindrep1.zip

InDesign09

せうぞーさんの名もないテクノ手のほうに
100倍便利なスクリプトがありました。こちらをお使いください。
なんのために作ったんだ。。。>自分
勉強のためです。

http://d.hatena.ne.jp/seuzo/20080919/1221832112

大阪DTPの勉強部屋オープン

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

2009/3/7に記念すべき第一回勉強会を終えた
大阪DTPの勉強部屋のサイトがついにオープンしました。

http://www.osakadtp.com/

ばばさんが作られたそうです。ありがとうございました。
わたしがこのブログをオープンしたのは2009/3/9
勉強会に参加して決意したのです!

大阪DTPの勉強部屋は私も次回お話しさせていただく予定になっています。
スクリプトのリクエストも受け付けていますのでお気軽にどうぞ。

InDesignCS3で(たぶんCS2でもCS4でも動く)
選択された文字列の中でルビが①②〜⑩を
「圏点①」などの文字スタイルを当ててルビを解除します。
カスタマイズすればルビが●のを圏点にしたりできますよ。

なんで、こんなものが必要かというと縦組の時です。
くわしくはサンプルドキュメントをごらんください。

また、なんで圏点設定せずに文字スタイルを当てるのかというと
そういう仕事で使ったからです。

処理したい文字列をIビームカーソルで選択して実行してください。

ダウンロードはこちらKenten.zip

EXCELの行をコピーしIllustratorの選択した小組に流し込みます。テキストフレームがエリアテキストの場合はオーバーフローすれば収まるように長体がかかります。改行を含む文字の場合の長体処理はしていません。
小組のテキストフレーム1つ1つにIllustratorのレイヤーパレットで"A","B","C","D"・・・と名前をあらかじめつけておきます。

ex-ai01.jpg

※レイヤーに名前を付けるようにレイヤーパレット内のテキストをダブルクリックして名前を付ける事が出来ます。

このテキストフレームがEXCELの列の"A","B","C","D"・・・と関連づきます。

ex-ai02.jpg

※EXCELのA列の文字がIllustratorの"A"と名前を付けたテキストフレームに流し込まれます。

準備が終わったらIllustratorの小組を選択し、EXCELの行をコピーしてからスクリプトを実行してみてください。

global textFlameList
set myRec to the clipboard
set textFlameList to {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

set OriginalDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {tab}
set myRec to text items of myRec

tell application "Adobe Illustrator"
	tell document 1
		set mySele to selection
		repeat with selObj in mySele
			my getTextFrameIndex(selObj)
		end repeat
		repeat with N from 1 to count myRec
			my NAGASIKOMI(item N of textFlameList, item N of myRec)
		end repeat
	end tell
end tell
set AppleScript's text item delimiters to OriginalDelimiters

on getTextFrameIndex(selObj)
	tell application "Adobe Illustrator"
		if class of selObj is group item then
			set myCount to count page items of selObj
			repeat with N from 1 to myCount
				my getTextFrameIndex(page item N of selObj)
			end repeat
		else if class of selObj is text frame then
			set myName to name of selObj
			set itemNum to my getItemNum(myName)
			if itemNum > 0 then
				set item itemNum of textFlameList to selObj
			end if
		end if
	end tell
end getTextFrameIndex

on getItemNum(myName)
	return offset of myName in "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
end getItemNum

on NAGASIKOMI(targetTextFrame, inputStr)
	if targetTextFrame is 0 then
		return
	end if
	tell application "Adobe Illustrator"
		set contents of targetTextFrame to inputStr
		if kind of targetTextFrame is area text then
			if my overflow(targetTextFrame) is true then
				set mySCL to horizontal scale of paragraph 1 of targetTextFrame
				my choutai(targetTextFrame, mySCL)
			end if
		end if
	end tell
end NAGASIKOMI

on choutai(targetTextFrame, mySCL)
	set mySCL to mySCL - 3
	tell application "Adobe Illustrator"
		set horizontal scale of paragraph 1 of targetTextFrame to mySCL
	end tell
	if my overflow(targetTextFrame) is true then
		my choutai(targetTextFrame, mySCL)
	end if
end choutai

on overflow(tFrame)
	set OriginalDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	tell application "Adobe Illustrator"
		set myLastP to (contents of every paragraph of tFrame) as string
		set myLastL to (contents of every line of tFrame) as string
		set AppleScript's text item delimiters to OriginalDelimiters
		if myLastP is not myLastL then
			set AppleScript's text item delimiters to OriginalDelimiters
			return true
		else
			set AppleScript's text item delimiters to OriginalDelimiters
			return false
		end if
	end tell
end overflow

3/9オープンして3/12には144人訪問

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

2009/3/9にスクリプト置き場としてこっそりオープンしたのですが

2009/3/12にInDesign勉強部屋さんからリンクいただき
http://study-room.info/id/index.html

2009/3/13にはせうぞーさんの名もないテクノ手で
ご紹介いただきました。そこで194人のご訪問
http://d.hatena.ne.jp/seuzo/20090313/

2つのビックサイトからの参照で奇跡のアクセスアップです。
現在3/19で120人/日程度になってきました。ふー

いやほんと感謝です。
ずっと資料をアップしていたのですが、
ちょっと一段落したので日記も書いてみました。

InDesignCS4の新機能

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

・正規表現スタイル
・ライブプリフライト
・リンクパレットの強化(拡張子を変えて再リンクとか)
・欧文なきわかれ
・全角スペースの行末吸収
・SWFやフラッシュ書き出し
・相互参照

このサイトについて

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

東京のPAGE2009でたくさんの著名な方とお会いして、さらに大阪DTP勉強会でも刺激を受けそして、名古屋、大阪の勉強会でスクリプト系のお話をさせていただくことになりました。
ありがたいと同時にプレッシャーも感じています。がんばらないとね。
話をするときに「だれだこりゃ」ってなるのも悲しいので、もうちょっとWEBを充実させることにしました。
で、いままで溜め込んでたスクリプトとかせめて勉強会まではアップしていこうと思っています。よろしくお付き合いください。

BridgeCS4の新機能

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

ブリッジ
・カラマネの同期

PhotoshopCS4新機能

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

PhotoshopCS4
・画像を変形しても人物は変形されないで保護する機能(マスクも可能)
(サーファーのデモ画面)
・回転がすごく速くなった(グラフィックボードによる)
・画像補正系の作業がすごく便利になった。

PhotoshopCS4Extended
・3D Studioなど3Dソフトからデータを持ってくるマッピングする

IllustratorCS4の新機能

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

・複数アートボード100個まで
 PDF保存すると複数ページになる。EPS保存するとバラバラになる。
 FreeHandとの互換のためらしい
・グラデーションが使いやすくなりました。
・グラデーションに楕円が使えるようになった
・アピアランスパレットから直接色や線幅を指定できるようになった。
・ブラシツールに塗りブラシが追加
・グループ内のオブジェクトを選択するにはダブルクリック
・トンボが効果に移動
・分版プレビューができるようになった
・カラーユニバーサルプレビューができるようになった

41)PDFハイパーリンク作成

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

こちらはハイパーリンク作成のサンプル。けっこう難しいです。最初にブックマークと同じくハイパーリンクオブジェクトを作成し、次にリンクテキストのリンク部分の領域を設定します。最後にリンクオブジェクトを作成し、その時、先ほど作成した2つの設定と下線の設定などを行って完成です。こんなところを読んでいるあなたはかなりのマニア。

tell application "Adobe InDesign CS2_J"
	tell document 1
		get properties of hyperlink 1
		--★ここから下でテキストをクリックしてページへのリンクを作成できる
		set theDest to make hyperlink page destination with properties ¬
		{destination page:page 3, view setting:fit window} 
		set myhyperlink to make hyperlink text source with properties ¬
			{source text:text from character 1 to character -2 of paragraph 2 of ¬
			text frame 1 of page 1}
		make hyperlink with properties {border color:black, highlight:invert, ¬
			border style:solid, hidden:false, destination:theDest, ¬
			source:myhyperlink, name:"456789", visible:false, width:thin}
	end tell
end tell

40)PDFのブックマーク作成

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

ウィンドウ=>インタラクティブのブックマークパレットにブックマークを作成します。スクリプトを実行すると下図のような状態になり、PDFを作成するとこのブックマークは生きてきます。

as-indd.jpg

プログラムとしてはhyperlinkオブジェクトをまず作成し、そこにリンク先のページやそのページの開き方の設定をしておきます。出来上がったhyperlinkオブジェクトの情報は見えないのですが、2回目に同じ名前のものを作ろうとするとエラーが出てしまいます。
次にブックマークを作成。それのプロパティに先ほど作成したhyperlinkオブジェクトを入れておくと出来上がりです。
作ったブックマークの中にもう一つブックマークを作ると上図のように入れ子になったブックマークが出来上がります。

tell application "Adobe InDesign CS2_J"
	tell document 1
		--★ここから下でページへのリンクの親メニューを作成できる
		set theDest to make hyperlink page destination with properties ¬
			{destination page:page 5, view setting:fit window}
		set oyaBookMark to make bookmark with properties ¬
			{destination:theDest, name:"567"}
		--★ここから下でページへのリンクを作成できる
		set theDest to make hyperlink page destination with properties ¬
			{destination page:page 5, view setting:fit window}
		make bookmark at end of oyaBookMark with properties ¬
			{destination:theDest, name:"5"}
		--★ここから下でページへのリンクを作成できる
		set theDest to make hyperlink page destination with properties ¬
			{destination page:page 6, view setting:fit window}
		make bookmark at end of oyaBookMark with properties ¬
			{destination:theDest, name:"6"}
		--★ここから下でページへのリンクを作成できる
		set theDest to make hyperlink page destination with properties ¬
			{destination page:page 7, view setting:fit window}
		make bookmark at end of oyaBookMark with properties ¬
			{destination:theDest, name:"7"}
	end tell
end tell

表の中の選択部分の値段を取り出し入力した値をプラスします。値段の","のつけかたがベタです。。。

set ANS to display dialog "値段を入力してください。" default answer "1000"
set plusNum to text returned of ANS
set plusNum to my replaceAll(plusNum, "円", "")
set plusNum to my replaceAll(plusNum, ",", "")
set plusNum to plusNum as integer

set OriginalDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {""}

tell application "Adobe InDesign CS2_J"
	tell active document
		tell selection
			set cellCount to count cells
			repeat with N from 1 to cellCount
				set myData to contents of cell N
				set myData to my replaceAll(myData, "円", "")
				set myData to my replaceAll(myData, ",", "")
				set myData to myData as integer
				set myData to myData + plusNum
				set myData to myData as string
				if length of myData = 4 then
					set myData to (character 1 of myData & "," & ¬
						characters 2 thru 4 of myData & "円") as string
				else if length of myData = 5 then
					set myData to (characters 1 thru 2 of myData & "," & ¬
						characters 3 thru 5 of myData & "円") as string
				else if length of myData = 6 then
					set myData to (characters 1 thru 3 of myData & "," & ¬
						characters 4 thru 6 of myData & "円") as string
				end if
				set contents of cell N to myData
			end repeat
		end tell
	end tell
end tell
set AppleScript's text item delimiters to OriginalDelimiters

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

38)表の行を追加する

| コメント(0) | トラックバック(0)
tell application "Adobe InDesign CS2_J"
	tell document 1
		tell text frame 1
			make row of table 1
			set rowCount to count rows of table 1
		end tell
	end tell
end tell

37)疑似囲み罫

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

ページ内の数段落分をコラムのように囲む事はInDesignではできません。かわりに1セルだけの表を使うのですが、めんどうなのでスクリプトで選択範囲を表にします。それでもページをまたぐ囲みはできないのですが。。。

tell application "Adobe InDesign CS2_J"
	tell document 1
		set PS to paragraph style "本文囲み内"
		set newTable to convert to table selection column separator "☆" ¬
			row separator "☆"
		--行区切り列区切りを(ありえなさそうな文字)"☆"にしています。
		set applied paragraph style of paragraphs 1 thru -1 of ¬
			cell 1 of newTable to PS
		set bottom edge stroke weight of cell 1 of newTable to 0.1
		set right edge stroke weight of cell 1 of newTable to 0.1
		set left edge stroke weight of cell 1 of newTable to 0.1
		set top edge stroke weight of cell 1 of newTable to 0.1
		set bottom inset of cell 1 of newTable to 3.25
		set top inset of cell 1 of newTable to 3.25
		set left inset of cell 1 of newTable to 0
		set right inset of cell 1 of newTable to 0
		set width of cell 1 of newTable to 130
	end tell
end tell

36)表内のあふれた文字に長体

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

表内の文字のオーバーフローに長体をかけます。オーバーフローのチェックにはoverflowsというプロパティを調べます。これはtext frameでも同じです。

tell application "Adobe InDesign CS2_J"
	activate
	set mySele to class of selection --選択されたアイテムが
	if mySele is cell or mySele is table then --もしセルかテーブルなら
		set selecount to count cells of selection --セルの数を数える
		repeat with C from 1 to selecount --セルの数分くりかえす
			if overflows of cell C of selection then --セルのテキストがあふれていたら
				repeat with myPer from 100 to 30 by -2 --100%から30%まで2%刻みで
					set horizontal scale of every paragraph of cell C of ¬
						selection to myPer --長体をかけていく
					recompose cell C of selection
					if not overflows of cell C of selection then 
						--セルのテキストが収まったら
						exit repeat --くりかえしを抜ける
					end if
				end repeat
			end if
		end repeat
	else
		display dialog "表を選択してください"
	end if
end tell

※09/04/17
長体が30%になるということでInDesignがアクティブになっていないとだめなのでactivateを入れました。でもまだ誤差が出るようです。overflowのチェックに時差が出ているようです。なので、ちょっと変更してみました。下記は段落1と行1を比べて同じかどうかチェックします。こっちのほうが確実ですが、複数行だと失敗しますのでご注意下さい。

tell application "Adobe InDesign CS2_J"
	activate
	set mySele to class of selection --選択されたアイテムが
	if mySele is cell or mySele is table then --もしセルかテーブルなら
		set selecount to count cells of selection --セルの数を数える
		repeat with C from 1 to selecount --セルの数分くりかえす
			if overflows of cell C of selection then --セルのテキストがあふれていたら
				set mypara to paragraph 1 of cell C of selection
				repeat with myPer from 100 to 30 by -2 --100%から30%まで2%刻みで
					set horizontal scale of every paragraph of cell C of ¬
						selection to myPer --長体をかけていく
					--if not overflows of cell C of selection then
					set myline to line 1 of cell C of selection
					if not my getoverflow(mypara, myline) then
						--セルのテキストが収まったら
						exit repeat --くりかえしを抜ける
					end if
				end repeat
			end if
		end repeat
	else
		display dialog "表を選択してください"
	end if
end tell

on getoverflow(mypara, myline)
	if mypara = myline then
		return false
	else
		return true
	end if
end getoverflow

35)表の罫線を変更する

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

表の罫線を変更します。選択したセルの下罫線だけを変える設定でカスタマイズすればいろんなバリエーションを作れます。

set OriginalDelimiters to AppleScript's text item delimiters
set myWaight to "0.0 mm"
tell application "Adobe InDesign CS2_J"
	tell document 1
		set selectedItems to selection
		if selectedItems is {} then
			return
		end if
		set selItem to item 1 of selectedItems
		set startCell to name of selItem
		set AppleScript's text item delimiters to {":"}
		set startCell to text items of startCell
		set startX to item 1 of startCell
		set startY to item 2 of startCell
		set indRowCount to count rows of selItem
		set indColumnCount to count columns of selItem
		--set left edge stroke weight of column (startX) of selItem to myWaight
		--set right edge stroke weight of ¬
			column (startX + indColumnCount - 1) of selItem to myWaight
		set bottom edge stroke weight of row (startY + indRowCount - 1) of selItem¬
			 to myWaight	
	end tell
end tell
set AppleScript's text item delimiters to OriginalDelimiters

34)選択された表を調べる

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

表の選択部分は表全体の何行目で何列目かを調べたい時、下記スクリプトで選択された表の中の1つめのセルの名前を取り出せます。"1:3"といった感じで返ってくるので":"区切りで分割します。もっと良い方法がありそうだけどわからなかった。。。。

tell application "Adobe InDesign CS2_J"
        --選択した表のcell 1は表全体の何行目か調べる
        set myRow to index of parent row of cell 1 of selection as integer
        --選択した表のcell 1の名前を調べる("1:3")とかで返ってくる
        get name of cell 2 of selection
end tell

33)テキストを表にペースト

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

クリップボードのテキストをInDesignCSの表の選択部分にペーストします。行や列の概念は無く3行4列の表なら単純に1番目から12番目までのセルにテキストを入れます。もしエクセルの表が2行6列でもセルの数は同じなので内容は入れ替わります。

CS3から直接コピペできますね。

set myTable to the clipboard
set OriginalDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return}
set myTable to text items of myTable
set AppleScript's text item delimiters to {tab}
set myTable to myTable as string
set myTable to text items of myTable
set AppleScript's text item delimiters to OriginalDelimiters

tell application "InDesign CS_J"
        set contents of selection to myTable
		--表組の内容を入れ替える。
end tell

32)ルビ

| コメント(0) | トラックバック(0)
tell application "Adobe InDesign CS2_J"
	tell document 1
		tell text frame 1
			set contents to "竹内 亨"
			set ruby flag of character 1 to yes
			set ruby string of character 1 to "たけ"
			set ruby flag of character 2 to yes
			set ruby string of character 2 to "うち"
			set ruby flag of character 4 to yes
			set ruby string of character 4 to "とおる"
		end tell
	end tell
end tell

スクリプトラベルで"L01spec"と名前をつけたtext frameにファイルtempL01.txtを流し込みます。

my test("L01spec", "TAKEUCHI:作業中:temp:tempL01.txt")

on test {myLabel, FilePath}
	set myLabel to myLabel as string
	set FilePath to FilePath as string
	tell application "Adobe InDesign CS2_J"
		tell text frame myLabel of document 1
			tell text 1
				place FilePath
			end tell
		end tell
	end tell
	return "true"
end run

30)字形変更

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

選択されたテキストフレーム内の文字を1文字づつあたっていき連続数字なら等幅半角字形に1ケタ数字なら等幅半角字形で前後に4分アキを入れる。
本当に1文字づつあたっていく超ベタなスクリプト。ほんとはずかしい。serchを使った方が早いですね。興味ある方はチャレンジしてみてください。

CS3なら正規表現
CS4なら正規表現スタイルを使った方が良いです。

set findList to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
set findListCount to count findList

tell application "Adobe InDesign CS2_J"
	tell document 1
		set selectedItems to selection
		if selectedItems is {} then
			display dialog "テキストフレームを選択ツール(黒矢印)で¬
				選択してください。"
			return
		end if
		repeat with selItem in selectedItems
			if class of selItem is not text frame then
				display dialog "テキストフレームを選択ツール(黒矢印)で¬
					選択してください。"
				return
			else
				set Pnum to count paragraphs of selItem
				repeat with P from 1 to Pnum
					set myPara to paragraph P of selItem
					set FLG to false
					repeat with FL from 1 to findListCount
						if item FL of findList is in myPara then
							set FLG to true
							exit repeat
						end if
					end repeat
					if FLG is true then
						set Cnum to count characters of paragraph P of selItem
						repeat with N from 1 to Cnum
							if N = 1 then
								set TXTpre to "★"
							else
								set TXTpre to contents of character (N - 1) of ¬
								paragraph P of selItem
							end if
							set TXT to contents of character N of paragraph P of selItem
							if N = Cnum then
								set TXTaf to "★"
							else
								set TXTaf to contents of character (N + 1) of ¬
								paragraph P of selItem
							end if
							if TXTpre is in "01234567890123456789,." and ¬
							TXT is in "01234567890123456789,." then
								set glyph form of character (N - 1) of paragraph P of ¬
								selItem to monospaced half width form --等幅半角字形
								set glyph form of character N of paragraph P of ¬
								selItem to monospaced half width form --等幅半角字形
							else if TXTpre is not in "01234567890123456789"¬
							 and TXT is in "01234567890123456789" ¬
							and TXTaf is not in "01234567890123456789" then
								set properties of character N of paragraph P ¬
								of selItem to {glyph form:monospaced half width form,¬
								 trailing aki:0.25, leading aki:0.25}
							end if
						end repeat
					end if
				end repeat
			end if
		end repeat
	end tell
end tell
display dialog "終了しました"

29)字取り変更

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

Iビームカーソルでテキストを選択し、今9字取りの文字列を7字取りに変更といったことができるスクリプト

tell application "Adobe InDesign CS2_J"
	tell document 1
		--最小3文字
		set ANS to display dialog "何字取りを何字取りにしますか?¬
			" default answer "9/7" buttons {"OK"}
		set myStr to text returned of ANS
		set motoJIDORI to 9
		set newJidori to 7
		set motoJIDORI to my nthFields(myStr, "/", 1)
		set newJidori to my nthFields(myStr, "/", 2)
		set selectedItems to selection
		if selectedItems is {} then
			return
		end if
		set selItem to item 1 of selectedItems
		tell selItem
			set jidori of every character whose jidori is motoJIDORI to newJidori
		end tell
	end tell
end tell

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

29)字取り変更

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

Iビームカーソルでテキストを選択し、今9字取りの文字列を7字取りに変更といったことができるスクリプト

tell application "Adobe InDesign CS2_J"
	tell document 1
		--最小3文字
		set ANS to display dialog "何字取りを何字取りにしますか?¬
			" default answer "9/7" buttons {"OK"}
		set myStr to text returned of ANS
		set motoJIDORI to 9
		set newJidori to 7
		set motoJIDORI to my nthFields(myStr, "/", 1)
		set newJidori to my nthFields(myStr, "/", 2)
		set selectedItems to selection
		if selectedItems is {} then
			return
		end if
		set selItem to item 1 of selectedItems
		tell selItem
			set jidori of every character whose jidori is motoJIDORI to newJidori
		end tell
	end tell
end tell

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

文字スタイルに設定されているフォントを変更します。

tell application "Adobe InDesign CS2_J"
	tell document 1
		tell character style "英語"
			set applied font to "Times New Roman PS MT"
			set point size to "6 Q"
		end tell
	end tell
end tell

27)文字スタイル

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

下記は文字スタイルのサンプルです。

tell application "Adobe InDesign CS2_J"
	tell active document
		set CS1 to character style "文字スタイル 1"
		tell text frame 1 of page 1
			set applied character style of characters 1 thru 2 to CS1
		end tell
	end tell
end tell

26)段落スタイル

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

同じくsearchを使って検索しているのですが、下記は"■数字."の文字列を検索し見つかれば"■見出し"という段落スタイルを付けています。

searchはCS3以降変更されているので、あくまで段落スタイルを適応するための参考程度にしてください。

tell application "Adobe InDesign CS2_J"
	set oldFindPreferences to properties of find preferences
	set oldChangePreferences to properties of change preferences
	tell active document
		tell page 1
			my setPstyle(text frame 1, "■^9.", "■見出")
		end tell
	end tell
	set properties of find preferences to oldFindPreferences
	set properties of change preferences to oldChangePreferences
end tell

on setPstyle(TF serchString, styleName)
	tell application "Adobe InDesign CS2_J"
		set find preferences to nothing
		set change preferences to nothing
		tell active document
			set PS1 to paragraph style styleName
			search TF with find attributes {find text:serchString} ¬
				with change attributes {applied paragraph style:PS1}
		end tell
	end tell
end setPstyle

25)searchをつかった検索置換

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

searchはInDesignの検索置換です。かなりいろいろできるのではまります。下記スクリプトでは(1)をOpenTypeの(1)にしています。必ず検索置換設定を記録して最後に復帰しておく事と、検索前に検索置換設定を空にしておくことが必要です。

CS2とCS3で大きく変わったのがここです。
このスクリプトはCS3では動きません。

tell application "Adobe InDesign CS2_J"
	set oldFindPreferences to properties of find preferences
	--検索設定を変数oldFindPreferencesに入れておく
	set oldChangePreferences to properties of change preferences
	--置換設定を変数oldFindPreferencesに入れておく
	tell active document
		tell page 1
			my FindRep1(text frame 1, "(1)", "<2474>")
			--オリジナル関数を呼び出す
		end tell
	end tell
	set properties of find preferences to oldFindPreferences
	--検索設定を元に戻す
	set properties of change preferences to oldChangePreferences
	--置換設定を元に戻す
end tell

on FindRep1(TF, findStr, repStr)
	tell application "Adobe InDesign CS2_J"
		set find preferences to nothing
		--検索設定をなしにする。
		set change preferences to nothing
		--置換設定をなしにする。
		tell active document
			search TF for findStr replacing with repStr
		end tell
	end tell
end FindRep1

24)改ページ文字を入れる

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

ページの最後に「改頁」という文字を挿入します。なんでこんなことするかというとこのテキストをマイクロソフトのワードにペーストし改頁を本物の改ページに置き換えます。するとワードの索引作成機能が使えるというわけです。ワードの方が便利なときもあるというサンプルですね。ちなみにワードの索引作成機能は私は良く知らないので質問しないでください。
このスクリプトのポイントはページの最初から処理を行うと「改頁」を入れた瞬間テキストが次ページにあふれどこが改ページなのかわからなくなるのでページの後ろから処理していってる事。
その後WORDの索引がそうとう使えない事が判明。WPRDで索引はやめといたほうがいいです。

tell application "Adobe InDesign CS2_J"
	tell document 1
		set PageCount to count pages
		repeat with N from 0 to PageCount - 1
			set P to PageCount - N
			tell page P
				set Tcount to count text frames
				--テキストフレームの数を数える
				repeat with T from 1 to Tcount
					set myB to visible bounds of text frame T
					set W to (item 4 of myB) - (item 2 of myB)
					set H to (item 3 of myB) - (item 1 of myB)
					--テキストフレームの大きさを調べて
					if 113 < W and 162 < H then
						--大きければ本文のテキストフレームとする。
						set myCha to character -1 of text frame T
						--最後の文字を取り出す。
						set character -1 of text frame 1 to myCha & "<改頁>"
						--最後の文字を最後の文字+<改頁>にする。
					end if
				end repeat
			end tell
		end repeat
	end tell
end tell

23)段落1行に長体

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

下記は段落(paragraph)の文字列と見せかけの行(line)の文字列を比べ、同じになるまで3%きざみで長体をかけます。

tell application "Adobe InDesign CS2_J"
	tell document 1
		set selectedItems to selection
		if selectedItems is {} then
			return
		end if
		set selItem to item 1 of selectedItems
		set myPer to 97
		--まずスタートパーセントを97にする。
		repeat
			--繰り返し
			set pStr to every paragraph of selItem as string
			--段落のテキストを取り出す
			set Lstr to line 1 of selItem as string
			--ライン(みかけの行)のテキストを取り出す
			if pStr is not Lstr then--もし段落と見かけの行が違うなら
				--変倍をかける
				set horizontal scale of paragraph 1 of selItem to myPer
				--パーセントから3を引いておく
				set myPer to myPer - 3
			else--段落と見かけの行が同じなら
				exit repeat--繰り返しを抜ける
			end if
		end repeat
	end tell
end tell

22)特殊なノンブル

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

WORDのPDFが元原稿で、それにあわせて全角数字でどうしてもノンブルを入れなくては行けない時のサンプルです。これは単ページのドキュメントのセンター下にテキストフレームを作りそこに全角数字のノンブルを入れます。

tell application "Adobe InDesign CS2_J"
	tell active document
		set PS to paragraph style "ノンブル"
		set countPage to count pages
		repeat with N from 1 to countPage
			tell page N
				set myStr to my HanToZen((N) as string)
				--ページ番号をオリジナル関数HanToZenを使って全角にする。
				set Tobj to make text frames at beginning with properties ¬
					{contents:myStr, visible bounds:¬
					{297 - 15, 210 / 2 - 20, 297 - 10 + 5, 210 / 2 + 20}}
					--テキストフレームを作成し文字を入れる
				set applied paragraph style of paragraph 1 of Tobj to PS
				--スタイルを当てる。
			end tell
		end repeat
	end tell
end tell

--オリシナル関数HanToZen
on HanToZen(myStr)
	set findList to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
	--半角文字リストを作成
	set repList to {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0"}
	--全角文字リストを作成
	repeat with N from 1 to count findList
		set myStr to my FindAndRep(item N of findList, item N of repList, myStr)
		--リスト分検索置換
	end repeat
	return myStr
end HanToZen

--オリシナル関数FindAndRep(検索置換)
on FindAndRep(findStr, repStr, motoStr)
	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 FindAndRep

21)縦組にする

| コメント(0) | トラックバック(0)
tell application "Adobe InDesign CS2_J"
        tell active document
                tell page 1
                        set story orientation of story preferences of ¬
						parent story of text frame 1 to vertical
                end tell
        end tell
end tell

20)名刺の10枚面付け

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

名刺を10枚分A4の用紙に面付けします。X,Yの数字を変更すれば位置を変える事ができます。

set inputFile to choose file with prompt "ファイルを選択してください"
tell application "Adobe InDesign CS2_J"
	tell page 1 of document 1
		set X to 14
		set Y to 11
		set W to 91
		set H to 55
		repeat with N1 from 1 to 2
			set X2 to X + W * (N1 - 1)
			repeat with N2 from 1 to 5
				set Y2 to Y + H * (N2 - 1)
				make new rectangle at beginning with properties ¬
					{geometric bounds:{Y2, X2, Y2 + H, X2 + W}, ¬
					stroke color:"None", fill color:"None"}
				set placeFile to place inputFile on rectangle 1
				fit rectangle 1 given center content
			end repeat
		end repeat
	end tell
end tell

名刺の10枚面付け2
上と同じスクリプトですが一生懸命考えると6行になりました。

set inputFile to choose file with prompt "ファイルを選択してください"
repeat with N1 from 1 to 10
	tell application "Adobe InDesign CS2_J" to make new rectangle at beginning¬
	 	of page 1 of document 1 with properties ¬
		{geometric bounds:{(10 + ((N1 - 1) div 2) * 55), (15 + (N1 mod 2) * 91), ¬
		(10 + ((N1 - 1) div 2) * 55) + 55, (15 + (N1 mod 2) * 91) + 91}, ¬
		stroke color:"None", fill color:"None"}
	tell application "Adobe InDesign CS2_J" to place inputFile on rectangle 1 ¬
		of page 1 of document 1
	tell application "Adobe InDesign CS2_J" to fit rectangle 1 of page 1 ¬
		of document 1 given center content
end repeat

19)PDFのページ配置

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

まずAcrobatでPDFを開きページ数を数えます。そのあとでInDesignのドキュメントにPDFを1ページづつ配置していきます。さきほどのEPS配置と良く似ていますがPDFの処理が付け加えられています。

set myPer to 100
set NURI to 3 --塗り足し(0にすると塗り足しなし)
set ANS to display dialog "ドキュメントの何ページ目から配置しますか?" ¬
		default answer "1"
set inddPage to text returned of ANS
set inddPage to inddPage as integer
set pdfPage to 1
set inputFile to choose file with prompt "pdfファイルを選択"
set Fkind to kind of (info for inputFile)
if Fkind is "PDF 書類" or Fkind is "Adobe PDF document" then
	--AcrobatでPDFを開きページ数を数える
	tell application "Adobe Acrobat 7.0 Professional"
		open inputFile
		set AllPage to count page of document 1
		close document 1
	end tell
	repeat AllPage times --AllPage分繰り返す。
		set myError to my setEPS(pdfPage, inddPage, inputFile, NURI, myPer)
		set pdfPage to pdfPage + 1
		set inddPage to inddPage + 1
		if myError is "ERROR" then
			exit repeat
		end if
	end repeat
end if

--オリジナル関数setEPSの宣言
on setEPS(pdfPage, inddPage, inputFile, NURI, myPer)
	--ここより下でend tryより上を試す(エラーが出ても止まらないようにするため)
	try
		--"Adobe InDesign CS2_J"を呼ぶ
		tell application "Adobe InDesign CS2_J"
			set page number of PDF place preferences to pdfPage
			set PDF crop of PDF place preferences to crop media
			--"最前面のドキュメント"を呼ぶ
			tell active document
				--もし変数myPageのページが存在しないなら
				if not (exists of page inddPage) then
					--最後にページをつくる
					make page at end
				end if
				--ページの領域を変数myBに入れる{Y1,X1,Y2,X2}で返ってくる
				set myB to bounds of page inddPage
				--Y1より変数NURIを引いた分だけ変数Yに入れる
				set Y to (item 1 of myB) - NURI
				--X1より変数NURIを引いた分だけ変数Xに入れる
				set X to (item 2 of myB) - NURI
				--Y2よりY1を引き変数NURI*2を足した分だけ変数Hに入れる
				set H to (item 3 of myB) - (item 1 of myB) + NURI * 2
				--X2よりX1を引き変数NURI*2を足した分だけ変数Wに入れる
				set W to (item 4 of myB) - (item 2 of myB) + NURI * 2
				--"ページ"変数myPageを呼ぶ
				tell page (inddPage)
					--画像ボックスを作り変数myRectに入れる
					--(画像ボックスのプロパティは領域{Y, X, Y + H, X + W}、
					--線幅0mm、ぬりなし、線の色もなし)
					set myRect to make rectangle at beginning with properties¬
					 {geometric bounds:{Y, X, Y + H, X + W}, stroke weight:0, ¬
					fill color:"None", stroke color:"None"}
					--変数myRectの中にinputFileを配置し結果を変数EPSobjに入れる
					set PDFobj to place inputFile on myRect
					--EPSobjを呼ぶ
					tell PDFobj
						--水平方向の比率と垂直方向の比率を変数myPerにする
						set properties to {vertical scale:myPer, horizontal scale:myPer}
					end tell
					--myRectにセンター揃えの命令を送る
					fit myRect given center content
				end tell
			end tell
		end tell
		return ""
	on error
		return "ERROR"
	end try
end setEPS

18)EPSのページ配置

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

フォルダ内のEPSファイルを次々にページに配置していきます。InDesignでrectangleを作る場合、座標をスプレッドで指定するため、(横組で右ページX座標が0ならX=210で指定する。)まずページ自体の座標を調べその座標を基準にrectangleを作成しています。

set myPer to 100
set NURI to 3 --塗り足し(0にすると塗り足しなし)

tell application "Adobe InDesign CS2_J"
	set myVersion to version
	if "4.0." is not in myVersion then
		--InDesignのバーション確認
		display dialog "Adobe InDesign CS2で使用してください。¬
			InDesign2.0が起動している場合は一旦終了し、スクリプトも¬
			再起動してください。"
		return
	end if
end tell

set myFol to choose folder with prompt "フォルダを選択"
set myFol to myFol as string
set myPage to 1--変数myPageを1にしておく。
repeat with myFile in list folder myFol without invisibles
	--フォルダ内のアイテム分繰り返す
	--以降のif文までは選択ファイル配置&保存を参照
	set this_item to (myFol & myFile) as alias
	set FType to file type of (info for this_item)
	set Fkind to kind of (info for this_item)
	if FType is "EPSF" or FType is "EPSP" or FType is "PDF " or ¬
		FType is "8BPS" or FType is "TEXT" or ¬
		Fkind is "Adobe Photoshop ファイル" or FType is "TIFF" or ¬
			Fkind is "TIFF 書類" or Fkind is "EPS ファイル" then
		my setEPS(myPage, myFol & myFile, NURI, myPer)
		--オリジナル関数setEPSを呼び出す
		set myPage to myPage + 1
		--変数myPageに1をプラスする。
	end if
end repeat

on setEPS(myPage, inputFile, NURI, myPer)
	try
		tell application "Adobe InDesign CS2_J"
			tell active document
				if not (exists of page myPage) then
					make page at end
				end if
				set myB to bounds of page myPage
				set Y to (item 1 of myB) - NURI
				set X to (item 2 of myB) - NURI
				set H to (item 3 of myB) - (item 1 of myB) + NURI * 2
				set W to (item 4 of myB) - (item 2 of myB) + NURI * 2
				tell page (myPage)
					set myRect to make rectangle at beginning with properties ¬
						{geometric bounds:{Y, X, Y + H, X + W}, stroke weight:0, ¬
						fill color:"None", stroke color:"None"}
					set EPSobj to place inputFile on myRect
					tell EPSobj
						set properties to ¬
							{vertical scale:myPer, horizontal scale:myPer}
					end tell
					fit myRect given center content
				end tell
			end tell
		end tell
	end try
end setEPS

17)選択ファイルを配置&保存

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

ファイルを選択しそのファイルを新規ドキュメントに配置し適切なサイズにドキュメントサイズを変更した後選択ファイルのフォルダに保存する。

set this_item to choose file with prompt "配置ファイルを選択"
set Fpath to this_item as string
--変数Fpathにフルパスを入れる
set Fpath to Fpath & ".indd"
--フルパスに拡張子.inddを付けておく
set Finfo to info for this_item
--ファイルインフォメーションを調べる
set FType to file type of Finfo
--ファイルインフォメーションのファイルタイプを変数Ftypeに入れる
set Fkind to kind of Finfo
--ファイルインフォメーションのファイルカインドを変数Fkindに入れる
--↓下記のような条件があえば。。。意外と条件にあわないEPSが多い。
if FType is "EPSF" or FType is "EPSP" or FType is "PDF " or FType is "8BPS" ¬
	or FType is "TEXT" or Fkind is "Adobe Photoshop ファイル" or ¬
	FType is "TIFF" or Fkind is "TIFF 書類" or Fkind is "EPS ファイル" then
	--オリジナル関数setEPS2を呼び出す
	my setEPS2(this_item, Fpath)
end if

--↓オリジナル関数setEPS2
on setEPS2(this_item, Fpath)
	tell application "Adobe InDesign CS2_J"
		--ドキュメントを作成
		make document
		tell active document
			set myRect to make rectangle at beginning with properties ¬
				{geometric bounds:{0, 0, 100, 100}, ¬
				stroke weight:0, fill color:"None", stroke color:"None"}
			--とりあえずrectangle(ボックス)を作成100×100サイズ
			--線や塗りはなし
			set EPSobj to place this_item on myRect
			--ファイルを作成したボックスに配置する
			--(配置画像を変数EPSobjに入れる)
			set myB to geometric bounds of EPSobj
			--配置画像(変数EPSobj)の領域を調べる
			--geometric bounds は{Y1,X1,Y2,X2}で返ってくる
			set H to (item 3 of myB) - (item 1 of myB)--高さを取り出す
			set W to (item 4 of myB) - (item 2 of myB)--幅を取り出す
			if H < 297 and W < 210 then --A4縦より小さいなら
				set PW to 210
				set PH to 297
			else if H < 210 and W < 297 then --A4横より小さいなら
				set PW to 297
				set PH to 210
			else if H < 364 and W < 257 then --B4縦より小さいなら
				set PW to 257
				set PH to 364
			else if H < 257 and W < 364 then --B4横より小さいなら
				set PW to 364
				set PH to 257
			else --それ以外
				if H > W then --A3縦
					set PW to 297
					set PH to 420
				else --A3横
					set PW to 420
					set PH to 297
				end if
			end if
			set properties of document preferences to {page height:PH, page width:PW}
			--ドキュメントのサイズを変更する。
			set geometric bounds of myRect to {0, 0, PH, PW}
			--rectのサイズをページサイズにする
			fit myRect given center content
			--画像をセンターあわせにする。
			save to Fpath
			--Fpathに保存する。
		end tell
	end tell
end setEPS2

16)画像の配置

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

単純に配置するだけなら下記で十分

set inputFile to "Macintosh HD:test.jpg"
tell application "Adobe InDesign CS2_J"
	tell active document
		tell page 1
			set EPSobj to place inputFile
		end tell
	end tell
end tell

領域を作っておきその領域に配置した後縮小する。

set inputFile to "Macintosh HD:test.jpg"
set X to 10
set Y to 20
set W to 50
set H to 60
set myPer to 70
tell application "Adobe InDesign CS2_J"
	tell active document
		tell page 1
			set myRect to make rectangle at beginning with properties ¬
				{geometric bounds:{Y, X, Y + H, X + W}, stroke weight:0, ¬
				fill color:"None", stroke color:"None"}
				--まずはボックスを作る
			set EPSobj to place inputFile on myRect
			--そのボックスに画像を入れる
			tell EPSobj
				set properties to {vertical scale:myPer, horizontal scale:myPer}
				--画像のサイズを変更する
			end tell
			fit myRect given center content
			--画像をセンターに入れる
		end tell
	end tell
end tell

15)選択画像の回転

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

こういう細かいスクリプトはアプリケーションのPresetsのScriptsフォルダに入れておくとウィンドウメニューの自動化=>スクリプトパレットに表示されるのでかなり便利。ショートカットも割り当てる事ができる。

tell application "Adobe InDesign CS2_J"
	tell active document
		tell selection
			set myAngle to rotation angle as real
			--現在の回転を取り出す
			set rotation angle to myAngle - 0.1
			--現在の回転から-0.1ひいたものにする。
		end tell
	end tell
end tell

14)画像のフィット

| コメント(0) | トラックバック(0)
tell application "Adobe InDesign CS2_J"
        tell active document
                fit rectangle 1 given content to frame
                -- fit rectangle 1 given content to frame/内容をフレームにあわせる
                -- fit rectangle 1 given center content/内容を中央に
                -- fit rectangle 1 given proportionally/短辺合わせ
                -- fit rectangle 1 given frame to content/フレームをあわせる
                -- fit rectangle 1 given fill proportionally/長辺合わせ
        end tell
end tell

13)スニペットの配置と書き出し

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

スニペットとはInDesignでオブジェクトを選択しDesktopにドラッグ&ドロップした時にできるオブジェクトです。これを別ドキュメントにドロップすれば同じ場所に同じオブジェクトが再現できます。ライブラリのようなものですが1つのオブジェクトごとにファイルで管理できるので自動組版システムを作るにはマスターページよりも使いやすい場合があります。
配置するのは下記のとおりで簡単です。

set mySnipet to "Macintosh HD:日程.inds" as string
tell application "Adobe InDesign CS2_J"
	tell page 1 of document 1
		place mySnipet
	end tell
end tell

こちらは出力です。出力も簡単なのですが書き出す時にレイヤーが非表示だと非表示のスニペットになってしまいます。要注意です。

tell application "Adobe InDesign CS2_J"
	tell document 1
		tell layer 2 of page 1
			export text frame 1 format InDesign snippet to mySnipet
		end tell
	end tell
end tell

12)ライブラリの配置

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

ライブラリの配置はあっけないほど簡単です。下記では"Nomi01"という名前のライブラリを配置しています。

tell application "Adobe InDesign CS2_J"
	place asset asset "Nomi01" of library 1 on document 1
end tell

11)トンボ作成

| コメント(0) | トラックバック(0)
Illustratorとけっこう違う部分があります。ラインの作成、カラーの指定、グループの作り方などさまざまなスクリプト勉強になります。
global lineW, mypt, myCol
--グローバル変数の宣言。線幅とポイント数、色はグローバルにした
set lineW to 0.1
--線幅0.1
set mypt to 2.83
--ポイントは2.83
tell application "Adobe InDesign CS2_J"
tell active document
set selectedItems to selection
--選択アイテムを調べる
if selectedItems is {} then
--選択アイテムが無ければ
display dialog "フレームを1つだけ選択してください。"
return
end if
if not (exists (layers whose name is "TOMBO")) then
--トンボレイヤーがなければ
make layer with properties {name:"TOMBO"}--レイヤーを作る
end if
set myCol to color "Registration"
-- 変数myColはレジストレーションにしておく
if number of selectedItems is 1 then
--選択アイテムが1つなら
set selItem to item 1 of selectedItems
--選択アイテムの1つめを取り出す。(変数selectedItemsはリストだから)
set myParent to parent of selItem
--選択アイテムの親オブジェクトを調べる。
--(ページ番号が入ると思われる。)
--※こまかいエラー処理をしていないのはご容赦
set myBounds to geometric bounds of selItem
--選択アイテムの領域
set X to item 2 of myBounds--X座標
set Y to item 1 of myBounds--Y座標
set W to (item 4 of myBounds) - (item 2 of myBounds)--幅
set H to (item 3 of myBounds) - (item 1 of myBounds)--高さ
set gItm to {} as list--変数gItmをリストとして初期化しておく。
--左上
set gItm to gItm & my drowLine(X - 13, X - 3, Y, Y, myParent)
--オリジナル関数dorwLineを呼び出し、
--結果(ラインオブジェクトが返ってくる)を変数gItemに入れる
set gItm to gItm & my drowLine(X - 13, X, Y - 3, Y - 3, myParent)
set gItm to gItm & my drowLine(X - 3, X - 3, Y - 13, Y, myParent)
set gItm to gItm & my drowLine(X, X, Y - 13, Y - 3, myParent)
--上センター
set gItm to gItm & my drowLine(X + W / 2, X + W / 2, Y - 13, Y - 3, myParent)
set gItm to gItm & my drowLine(X + W / 2 - 5, X + W / 2 + 5, Y - 7, Y - 7, myParent)
set gItm to gItm & my drowEllipse(X + W / 2 - 2, Y - 7 - 2, 4, 4, myParent)
--右上
set gItm to gItm & my drowLine(X + W + 3, X + W + 13, Y, Y, myParent)
set gItm to gItm & my drowLine(X + W, X + W + 13, Y - 3, Y - 3, myParent)
set gItm to gItm & my drowLine(X + W + 3, X + W + 3, Y - 13, Y, myParent)
set gItm to gItm & my drowLine(X + W, X + W, Y - 13, Y - 3, myParent)
--左センター
set gItm to gItm & my drowLine(X - 13, X - 3, Y + H / 2, Y + H / 2, myParent)
set gItm to gItm & my drowLine(X - 7, X - 7, Y + H / 2 - 5, Y + H / 2 + 5, myParent)
set gItm to gItm & my drowEllipse(X - 7 - 2, Y + H / 2 - 2, 4, 4, myParent)
--右センター
set gItm to gItm & my drowLine(X + W + 3, X + W + 13, Y + H / 2, Y + H / 2, myParent)
set gItm to gItm & my drowLine(X + W + 7, X + W + 7, Y + H / 2 - 5, Y + H / 2 + 5, myParent)
set gItm to gItm & my drowEllipse(X + W + 7 - 2, Y + H / 2 - 2, 4, 4, myParent)
--左下
set gItm to gItm & my drowLine(X - 13, X - 3, Y + H, Y + H, myParent)
set gItm to gItm & my drowLine(X - 13, X, Y + H + 3, Y + H + 3, myParent)
set gItm to gItm & my drowLine(X - 3, X - 3, Y + H + 13, Y + H, myParent)
set gItm to gItm & my drowLine(X, X, Y + H + 13, Y + H + 3, myParent)
--下センター
set gItm to gItm & my drowLine(X + W / 2, X + W / 2, Y + H + 13, Y + H + 3, myParent)
set gItm to gItm & my drowLine(X + W / 2 - 5, X + W / 2 + 5, Y + H + 7, Y + H + 7, myParent)
set gItm to gItm & my drowEllipse(X + W / 2 - 2, Y + H + 7 - 2, 4, 4, myParent)
--右上
set gItm to gItm & my drowLine(X + W + 3, X + W + 13, Y + H, Y + H, myParent)
set gItm to gItm & my drowLine(X + W, X + W + 13, Y + H + 3, Y + H + 3, myParent)
set gItm to gItm & my drowLine(X + W + 3, X + W + 3, Y + H + 13, Y + H, myParent)
set gItm to gItm & my drowLine(X + W, X + W, Y + H + 13, Y + H + 3, myParent)
make group with properties {group items:gItm}
--変数gItemをグループ化する。
end if
end tell
end tell

--オリジナル関数dorwLine
on drowLine(X1, X2, Y1, Y2, myParent)
set buf to {Y1, X1, Y2, X2}
tell application "Adobe InDesign CS2_J"
tell myParent--変数myParentにはおそらくページが入っている。
make graphic line with properties {geometric bounds:buf, ¬
fill color:"None", stroke color:myCol, stroke weight:lineW * mypt, ¬
item layer:"TOMBO"}
--"TOMBO"レイヤーに線を引く
end tell
end tell
end drowLine

on drowEllipse(X1, Y1, W1, H1, myParent)
set buf to {Y1, X1, Y1 + H1, X1 + W1}
tell application "Adobe InDesign CS2_J"
tell myParent
make ovals with properties {geometric bounds:buf, ¬
fill color:"None", stroke color:myCol, stroke weight:lineW * mypt, ¬
item layer:"TOMBO"}
--マルを作成する
end tell
end tell
end drowEllipse
        

マスターページを上書き可能な状態にするのはスクリプトの定番手法。

tell application "Adobe InDesign CS2_J"
        tell document 1
                override item 1 of master page items of page 1 destination page page 1
        end tell
end tell

09)スクリプトラベル

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

ウィンドウメニューの自動化=>スクリプトラベルパレットを表示しオブジェクトを選択した状態でスクリプトラベルにオブジェクトの名前を入力する事ができる。例えばtext frameを選択し"Sname"とスクリプトラベルに名前をつけると下記のように名前で指定できる。

tell application "Adobe InDesign CS2_J"
	tell document 1
		tell page 1
			tell text frame "Sname"
				set contents to "商品名"
			end tell
		end tell
	end tell
end tell

08)スプレッド1は何ページあるか

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

スプレッド1が見開きか単ページか調べる事は非常に重要です。それによってページにアイテムを配置する配置方法が変わってくるからです。

tell application "Adobe InDesign CS2_J"
	tell active document
		tell spread 1--スプレッド1の
			set sp1Count to count pages--ページ数を調べる
		end tell
	end tell
end tell
display dialog "スプレッド1は" & sp1Count & "ページです"

07)ページ数を調べる

| コメント(0) | トラックバック(0)
tell application "Adobe InDesign CS2_J"
	tell active document
		set PageCount to count page
	end tell
end tell

06)線の作成

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

"Y100M100"のカラーがあるか調べ、なければカラーを作成、そのあとで線を作ります。塗りなしはfill color:"None"で指定できます。(InDesign2.0ではfill color:none)

tell application "Adobe InDesign CS2_J"
	set LayObj to layer "LINE" of active document
	set Ws to 0.2
	set colName to "M100Y100"
	set C to 0
	set M to 100
	set Y to 100
	set K to 0
	tell active document
		try
			set myCol to color colName
			--変数myColにカラーオブジェクトを入れる
		on error--失敗したら
			set myCol to make color with properties ¬
				{name:colName, color value:{C, M, Y, K}}
			--変数myColにカラーオブジェクトを作る
		end try
	end tell
	tell page 1 of active document
		make graphic line with properties ¬
			{geometric bounds:{"12 mm", "10 mm", "109 mm", "10 mm"}, ¬
			fill color:"None", stroke color:myCol, stroke weight:Ws, ¬
			item layer:LayObj}
		--ライン作成。塗りがなしはfill color:"None"
		--2.0ではfill color:none
	end tell
end tell

05)レイヤーを作成する

| コメント(0) | トラックバック(0)
IMGレイヤーが存在するか調べ、なければ作成します。
tell application "Adobe InDesign CS2_J"
tell active document
if not (exists (layers whose name is "IMG")) then
--同じ名前のレイヤーがあればエラーになるのでまず調べる
make layer with properties {name:"IMG"}
--なければレイヤーをつくる
end if
end tell
end tell

04)ガイドを作成する

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

写真レイヤーにガイドを作成します。

tell application "Adobe InDesign CS2_J"
	set LayObj to layer "写真" of active document
	--写真レイヤーを変数LayObjへ入れる
	tell active document
		tell page 1
			set myGuide to make guide with properties ¬
				{orientation:horizontal, location:40, item layer:LayObj}
				--写真レイヤーに横ガイドをひく
		end tell
	end tell
end tell
tell application "Adobe InDesign CS2_J"
	tell active document
		set spCount to count master spread
		--マスターページのスプレッド数を調べる
		repeat with N from 1 to spCount
		--マスターページ分繰り返す
			tell master spread N
				delete (every guide whose orientation is horizontal)
				--横ガイドを全て消す
			end tell
		end repeat
	end tell
end tell
tell application "Adobe InDesign CS2_J"
	set Dcount to count document
	repeat with N from 1 to Dcount
		set myName to name of document N as string
		set myPath to (file path of document N) as string
		set myPDFpreset to PDF export preset "[PDF/X-1a]"
		set newFile to myPath & myName & ".pdf"
		export document N format PDF type to newFile using myPDFpreset
	end repeat
end tell

01)Document

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

作成する(ミリ単位で指定できる。)

tell application "Adobe InDesign CS2_J"
	make document with properties {page height:297, page width:210}
end tell

作成する2プロパティも同時設定

tell application "Adobe InDesign CS2_J"
	set myDocument to make document
	--ドキュメントを作成(作成されたドキュメントは変数myDocumentに入る)
	set properties of document preferences of myDocument to ¬
		{pages per document:10, pages per spread:1, ¬
		page width:"210 mm", page height:"297 mm"}
	--プロパティの設定。document preferencesを使う。
	--pages per document	ドキュメントのページ数
	--pages per spread			スプレッド当たりのページ数(2なら見開き)
end tell

保存せずに閉じる(Illustratorと同じ)

tell application "Adobe InDesign CS2_J"
	close document 1 saving no
end tell

保存する

set Fpath to "macintosh HD:test.indd"
tell application "Adobe InDesign CS2_J"
	save document 1 to Fpath
end tell

保存.jsx
IllustratorCS3で現在開いているドキュメントを
指定されたタイプ(AI、PDF、EPS)
指定されたバージョンで次々に保存していきます。
そして閉じていきます。

保存しないで閉じる.jsx
IllustratorCS3で現在開いているドキュメントを
ただ保存しないで閉じます。昔のドキュメントをいっぱい開いてしまって
閉じるたびに保存しますかと聞かれてうっとおしいときに便利です。

■■■使い方■■■
Illustratorドキュメントを開いている状態で使用してください。

実行すると
タイプ(AI、PDF、EPS)
バージョン(8、9、10、CS、CS2、CS3、CS4)
を聞いてきますので選択してください。

次に保存フォルダを聞いてきますので選択します。

次々に保存されて閉じていきます。

PDF保存ではバージョンは無視されます。
上位バーションで保存するとエラーになります。
フォントのエンベットなどはデフォルト値が使われます。

ダウンロードはこちらAIsave.zip

下記はまず選択されたアイテムを調べる。アイテムはたとえ1つだけ選択していてもリストで返ってくるので、リスト内のアイテム分繰り返す。(それが1つめのrepeat)そこから選択オブジェクトのクラスがtext columnならテキストが選択されていると見て処理を続ける。

tell application "Adobe InDesign CS2_J"
	tell document 1
		--文字スタイルを変数CSに入れておく
		set CS to character style "文字スタイル 1"
		--選択オブジェクとを変数selectedItemListに入れる
		set selectedItemList to selection
		if selectedItemList is {} then --もしなければ
			return --処理終了
		end if --選択されたオブジェクトを調べる(listで返ってくる)
		repeat with selectedItem in selectedItemList
			--選択オブジェクト分繰り返す。
			set myClass to class of selectedItem
			--もし選択オブジェクトが文字列なら
			if myClass is text column then
				--文字数分繰り返す
				repeat with C from 1 to count characters of selectedItem
					--もし文字が "0123456789"の中のどれかなら
					if contents of character C of selectedItem is in "0123456789" then
						--文字スタイルをつける
						set applied character style of character C of selectedItem to CS
					end if
				end repeat
			end if
		end repeat
	end tell
end tell

選択オブジェクトのクラスはよく使うものとして

if myClass is rectangle then--画像ボックス等四角です。
if myClass is text farme then--テキストフレーム
if myClass is text column then--テキスト
if myClass is table then--表

ほかにもわからないアイテムは page item で調べられます。ドキュメントの1ページ目に1つのオブジェクトを作り下記のようなスクリプトをつかうとクラスが何か調べる事が出来ます。

--ページアイテムのクラスを調べる時に使います。
tell application "Adobe InDesign CS2_J"
	tell page 1 of document 1
		get class of page item 1
	end tell
end tell

22)処理が止まってしまうとき

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

ダイアログがでてAppleScriptの応答待ちで処理が止まってしまうときは、第2章のエラー処理などで紹介したtime outの設定をつかい1秒応答がなければIllustratorを落とす方法で逃げる。しかしその場合もAppleScriptからIllustratorにQuitコマンドを送っても応答しないのでshellから強制的に落とす。興味ある方は調べてみてください。

文字列の検索置換や段落・文字スタイルの適応(おそらくInDesignと同じ)タブの設定やシンボルの配置。さらに詳しく見ていくとグラデーションやブラシなどクラスはいっぱいあるので、AppleScriptでコントロールできるはず、だが省略。。。
したくてもできない事は

Illustratorでドキュメントを開くときリンクが見つからないとかエラーダイアログが出てしまうときがある。
ダイアログを出さずにオープンする事ができない。
お〜まちさんの指摘より下記でダイアログを出さずに開く事ができました。

tell application "Adobe Illustrator"
	open file "Macintosh HD:TEST10.ai" without dialogs
end tell

㎝など特殊な文字は流し込みに失敗する。
Unicodeはいけます。

レイヤー名やオブジェクト名に日本語を使うとAppleScriptで値を取り出すとき文字化けする。
CS2で解決している

アピアランスがコントロールできない。などなど。わたしの思い違いも多々あると思うのでおかしな点はご指摘ください。

20)線幅を変える

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

全てのpath itemの線の太さ調べ、特定の太さの線の線幅を変更します。

tell application "Adobe Illustrator"
	tell document 1
		set pathCount to count path items
		repeat with pathNum from 1 to pathCount
			set pathStrokeWidth to stroke width of path item pathNum
			if 0.6 < pathStrokeWidth and pathStrokeWidth < 0.7 then
				set stroke width of path item pathNum to 0.283
			else if 0.7 < pathStrokeWidth then
				set stroke width of path item pathNum to pathStrokeWidth / 2
			else
				set stroke width of path item pathNum to 0.283
			end if
		end repeat
	end tell
end tell

19)トンボ作成

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

下記でトンボのカラーや線幅をカスタマイズすることができます。

global lineW, C, M, Y, K, pt
set lineW to 0.05
--色を入力単位%
set C to 100
set M to 100
set Y to 100
set K to 100
set pt to 2.83466796875

tell application "Adobe Illustrator"
	tell document 1
		set selectedItems to selection
		if selectedItems is {} then
			display dialog "四角をを選択ツールで選択してください。"
			return
		end if
		repeat with selItem in selectedItems
			set myB to geometric bounds of selItem
			my makeTombo(myB)
		end repeat
	end tell
end tell

on makeTombo(myB)
	set X1 to (item 1 of myB) / pt
	set Y1 to (item 2 of myB) / pt
	set X2 to (item 3 of myB) / pt
	set y2 to (item 4 of myB) / pt
	set myGroup to my makeGroup()
	my makeLine(X1 - 13, Y1, X1 - 3, Y1, myGroup) --横罫左上
	my makeLine(X1 - 13, Y1 + 3, X1, Y1 + 3, myGroup) --横罫左上
	my makeLine(X1, Y1 + 13, X1, Y1 + 3, myGroup) --縦罫左上
	my makeLine(X1 - 3, Y1 + 13, X1 - 3, Y1, myGroup) --縦罫左上
	my makeLine(X1 - 13, y2, X1 - 3, y2, myGroup) --横罫左下
	my makeLine(X1 - 13, y2 - 3, X1, y2 - 3, myGroup) --横罫左下
	my makeLine(X1, y2 - 13, X1, y2 - 3, myGroup) --縦罫左下
	my makeLine(X1 - 3, y2 - 13, X1 - 3, y2, myGroup) --縦罫左下
	my makeLine(X2 + 13, Y1, X2 + 3, Y1, myGroup) --横罫右上
	my makeLine(X2 + 13, Y1 + 3, X2, Y1 + 3, myGroup) --横罫右上
	my makeLine(X2, Y1 + 13, X2, Y1 + 3, myGroup) --縦罫右上
	my makeLine(X2 + 3, Y1 + 13, X2 + 3, Y1, myGroup) --縦罫右上
	my makeLine(X2 + 13, y2, X2 + 3, y2, myGroup) --横罫右下
	my makeLine(X2 + 13, y2 - 3, X2, y2 - 3, myGroup) --横罫右下
	my makeLine(X2, y2 - 13, X2, y2 - 3, myGroup) --縦罫右下
	my makeLine(X2 + 3, y2 - 13, X2 + 3, y2, myGroup) --縦罫右下
	my makeLine(X1 - 13, Y1 + (y2 - Y1) / 2, X1 - 3, Y1 + (y2 - Y1) / 2, myGroup) 
	--横罫左中
	my makeLine(X1 - 8, Y1 + (y2 - Y1) / 2 - 5, X1 - 8, Y1 + (y2 - Y1) / 2 + 5, myGroup) 
	--縦罫左中
	my makeEllipse(X1 - 8 - 1.5, (Y1 + (y2 - Y1) / 2) + 1.5, 3, 3, myGroup) 
	--○丸左中
	my makeLine(X2 + 13, Y1 + (y2 - Y1) / 2, X2 + 3, Y1 + (y2 - Y1) / 2, myGroup) 
	--横罫左中
	my makeLine(X2 + 8, Y1 + (y2 - Y1) / 2 - 5, X2 + 8, Y1 + (y2 - Y1) / 2 + 5, myGroup) 
	--縦罫左中
	my makeEllipse(X2 + 8 - 1.5, (Y1 + (y2 - Y1) / 2) + 1.5, 3, 3, myGroup) 
	--○丸右中
	my makeLine(X1 + (X2 - X1) / 2, Y1 + 13, X1 + (X2 - X1) / 2, Y1 + 3, myGroup) 
	--縦罫中上
	my makeLine(X1 + (X2 - X1) / 2 - 5, Y1 + 8, X1 + (X2 - X1) / 2 + 5, Y1 + 8, myGroup) 
	--横罫中上
	my makeEllipse(X1 + (X2 - X1) / 2 - 1.5, Y1 + 8 + 1.5, 3, 3, myGroup) --○丸中上
	my makeLine(X1 + (X2 - X1) / 2, y2 - 13, X1 + (X2 - X1) / 2, y2 - 3, myGroup) 
	--縦罫中下
	my makeLine(X1 + (X2 - X1) / 2 - 5, y2 - 8, X1 + (X2 - X1) / 2 + 5, y2 - 8, myGroup) 
	--横罫中下
	my makeEllipse(X1 + (X2 - X1) / 2 - 1.5, y2 - 8 + 1.5, 3, 3, myGroup) --○丸中下
end makeTombo

on makeLine(X1, Y1, X2, y2, myGroup)
	set X1 to X1 * pt
	set Y1 to Y1 * pt
	set X2 to X2 * pt
	set y2 to y2 * pt
	set W to lineW * pt
	tell application "Adobe Illustrator"
		tell document 1
			make new path item at end of myGroup with properties ¬
				{stroke color:{cyan:C, magenta:M, yellow:Y, black:K}, ¬
				stroke width:W, entire path:{{class:path point info, ¬
				anchor:{X1, Y1}}, {class:path point info, anchor:{X2, y2}}}}
		end tell
	end tell
end makeLine

on makeEllipse(X1, Y1, W, H, myGroup)
	tell application "Adobe Illustrator"
		tell document 1
			set X1 to X1 * pt
			set Y1 to Y1 * pt
			set myW to W * pt
			set myH to H * pt
			set W to lineW * pt
			make new ellipse at end of myGroup with properties ¬
				{filled:false, stroked:true, ¬
				stroke color:{cyan:C, magenta:M, yellow:Y, black:K}, ¬
				width:myW, height:myH, position:{X1, Y1}, stroke width:W}
		end tell
	end tell
end makeEllipse

on makeGroup()
	tell application "Adobe Illustrator"
		tell document 1
			if exists group item "TOMBO" then
				set myGroup to group item "TOMBO"
			else
				set myGroup to make group item at beginning ¬
					with properties {name:"TOMBO"}
			end if
		end tell
	end tell
	return myGroup
end makeGroup

18)画像の回転

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

回転

tell application "Adobe Illustrator"
	tell document 1
		rotate placed item 1 angle 5
	end tell
end tell

17)クリッピングマスクする

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

サンプルスクリプトをはしょって申し訳ないのですが下記はgroup itemに画像を配置しクリッピングマスクを適応する例です。

tell application "Adobe Illustrator"
	tell document 1
		make new group item at end with properties {name:KeyWord} 
		--レイヤーの一番後ろにグループが作られる
		make new placed item at end of group item KeyWord ¬
			with properties {file path:alias Fpath, position:{X, Y}}
		set clipped of group item KeyWord to true
	end tell
end tell

下記は画像を配置してさらに50%にサイズ変更する。

set filePath to (choose file) as string
set X to 10
set Y to 10
set mySize to 50
set pt to 2.83
set mySize to mySize * pt
set X to X * pt
set Y to Y * pt
tell application "Adobe Illustrator"
	--下記はリンク
	set placedRef to make new placed item in document 1 ¬
		with properties {file path:alias filePath}
	--下記は埋め込み
	--set placedRef to make new group item at end with data alias filePath
	set PW to width of placedRef
	set PH to height of placedRef
	if PW > PH then
		set buf to PW
	else
		set buf to PH
	end if
	set Percent to mySize / buf
	set W to PW * Percent
	set H to PH * Percent
	set properties of placedRef to {position:{X, Y}, height:H, width:W}
end tell
tell application "Adobe Illustrator"
	tell document 1
		set selectedItems to selection
		if selectedItems is {} then
			display dialog "text frameを選択ツールで選択してください。"
			return
		end if
		repeat with selItem in selectedItems
			if class of selItem is text frame then
				set Cnum to count characters of selItem
				repeat with N from 1 to Cnum - 1
					set TXT1 to contents of character N of selItem
					set TXT2 to contents of character (N + 1) of selItem
					if TXT1 is in "0123456789,." and TXT2 is in "0123456789,." then
						set alternate glyphs of character N of selItem¬
							 to half width --等幅半角字形
						set alternate glyphs of character (N + 1) of selItem¬
							 to half width --等幅半角字形
					end if
				end repeat
			end if
		end repeat
	end tell
end tell

14)テキスト全部保存

| コメント(0) | トラックバック(0)
set TexData to ""
set FileName to (choose file name) as string
tell application "Adobe Illustrator"
	tell document 1
		set boxnum to count text frames
		repeat with N from 1 to boxnum
			set TexData to TexData & contents of text frame N & return & return
		end repeat
	end tell
end tell

open for access file FileName with write permission
write TexData to file FileName
close access file FileName

13)文字を挿入する

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

文字列挿入。下記のスクリプトではtext frame "A"というようにtext frameを名前で指定しているのがポイント。名前をつけるにはレイヤーパレットでオブジェクトをダブルクリックすれば名前をつけられる。

tell application "Adobe Illustrator"
	tell document 1
		tell text frame "A"
			--1文字目を"あいうえお"にする
			set contents of character 1 to "あいうえお"
			--再終行を"かきくけこ"にする
			set contents of paragraph -1 to "かきくけこ"
		end tell
	end tell
end tell

12)オーバーフローを調べる

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

paragraphを合体したもの(全テキスト)とlineを合体したもの(見えているテキスト)を比べてオーバーフローしていたら警告する。

tell application "Adobe Illustrator"
	tell document 1
		repeat with tFrame in every text frame
			if kind of tFrame is {area text} then
				if my overflow(tFrame) then
					display dialog "オーバーフロー:" & contents of tFrame
				end if
			end if
		end repeat
	end tell
end tell

on overflow(tFrame)
	set OriginalDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	tell application "Adobe Illustrator"
		set myLastP to (contents of every paragraph of tFrame) as string
		set myLastL to (contents of every line of tFrame) as string
		set AppleScript's text item delimiters to OriginalDelimiters
		if myLastP is not myLastL then
			set AppleScript's text item delimiters to OriginalDelimiters
			return true
		else
			set AppleScript's text item delimiters to OriginalDelimiters
			return false
		end if
	end tell
end overflow

11)character 文字

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

テキストにいろいろな設定をします。Illustrator10ならtext frameをtext art itemに変えて記述してください。

set pt to 2.83464
set Q to 0.25 * pt
tell application "Adobe Illustrator"
	tell document 1
		set mySelObjList to selection--選択されたオブジェクトを調べる
		repeat with mySelObj in mySelObjList--選択分繰り返す
			if class of mySelObj is text frame then--もしtext frameなら
				tell mySelObj
					--プロパティを調べる
					get properties of character 1
					--内容を変更する
					set contents to "あいうえお" & return & "かきくけこ"
					--フォントを変える
					set text font of characters 1 thru 5 to ¬
						text font "Osaka" of application "Adobe Illustrator"
					--サイズを変える
					set size of characters 1 thru 5 to 13 * Q
					--ベースラインシフト
					set baseline shift of character 1 of paragraph 1 to 2
					--横変倍
					set horizontal scale of character 1 of paragraph 1 to 200
					--縦変倍
					set vertical scale of character 2 of paragraph 1 to 200
					--カーニング
					set kerning of character 2 of paragraph 1 to -300
					--塗り
					set fill color of character 3 of paragraph 2 to ¬
						{cyan:0, magenta:50, yellow:100, black:0}
					--線幅
					set stroke weight of character 3 of paragraph 2 to 0.2
					--線の色
					set stroke color of character 3 of paragraph 2 to ¬
						{cyan:0, magenta:100, yellow:100, black:0}
					--テキストフレームの幅を変更(長体がかかる)
					--これを行うと線幅等が消えてしまう要注意
					set width to 10 * pt
				end tell
			end if
		end repeat
	end tell
end tell

11)ルビ処理支援

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

InDesignCS3で選択された文字列を(たぶんCS2でもCS4でも動く)
ルビ処理します。

誕生日/たんじょうび
なら誕生日にグループルビが付きます。

誕生日/たん.じょう.び
なら誕生日にモノルビ"."区切りで付きます。
(普通こういうのは","区切りとかなのですがWindowsのテンキーでも打てるピリオドにしました。)

ルビ処理したい文字列をIビームカーソルで選択して実行してください。

ダウンロードはこちらruby02.zip

InDesignCS3で選択された文字列の(たぶんCS2でもCS4でも動く)
グループルビをモノルビに
モノルビをグループルビにします。

グループルビをバラす場合はルビをあらかじめ"/"区切りにしておきます。
1つの漢字に1つのルビなら区切らなくても大丈夫です。

ルビ処理したい文字列をIビームカーソルで選択して実行してください。

↓モノルビがグループルビに

↓グループルビがモノルビに("/"区切りで)

↓グループルビがモノルビに(1ルビ1文字なら区切らなくてもOK)
って変わっているように見えない。

ダウンロードはこちらruby01.zip

長体方法のうちのひとつ。テキストボックスに収まるように、まずテキストボックスの横幅を測り、1行のときの文字列の幅も測り比率をだして長体をかけます。ポイントはテキストボックスを複製し、横幅を10倍にします。(それで1行になると思われるため)さらに文字列をアウトラインし正確な文字幅を求めます。あとは比率を出してやるだけです。
もっと普通に長体をかける方法もありますがInDesignの長体を参照していただければと思います。

tell application "Adobe Illustrator"
	tell document 1
		set mySelObjList to selection --選択されたオブジェクトを調べる
		repeat with mySelObj in mySelObjList --選択分繰り返す
			if class of mySelObj is text frame then --もしtext frameなら
				my chouTai(mySelObj)
			end if
		end repeat
	end tell
end tell

on chouTai(mySelObj)
	tell application "Adobe Illustrator"
		set myW to width of mySelObj --text frameの幅を調べる
		--長体horizontal scaleを調べる(平体はvertical scale)--
		--Illustrator10ではscalingで縦横の比率がとれる
		set SCL to horizontal scale of paragraph 1 of mySelObj
		--横幅10倍サイズで複製する
		set dupTextObj to duplicate mySelObj to beginning of document 1 ¬
			with properties {width:myW * 10}
		--長体も10倍になるので長体を101%にする(なぜか100%にはならない)
		if SCL = 100 then
			set SCL to 101
		end if
		set horizontal scale of paragraph 1 of dupTextObj to SCL
		--行数を調べる
		set Pcount to count paragraph of dupTextObj
		if Pcount > 1 then --行数が2行以上なら
			display dialog "1行のテキストのみに有効です。"
			return --処理終了
		end if
		--行揃えを左にしておく。両揃えだと正しくはかれない
		set justification of paragraph 1 of dupTextObj to left
		--アウトライン作成
		set outLinePath to convert to paths dupTextObj
		--アウトラインの領域を調べる
		set myBounds to control bounds of outLinePath --control/visible
		--長さを取り出す
		set myW2 to (item 3 of myBounds) - (item 1 of myBounds) --(width of TBOX)
		delete outLinePath --アウトラインは消す
		if myW < myW2 then --もしアウトラインの方がtext frameよりも長ければ
			set myWhi to myW / myW2 --比率を求めて
			set SCL to SCL * myWhi - 1--誤差が出るので1%マイナスする
			set horizontal scale of paragraph 1 of mySelObj to SCL
		end if
	end tell
end chouTai

09)paragraph 段落

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

段落に対して行揃えの設定をします。ほかにもreadingで行間の設定や段落スタイルの設定等が出来ます。すいません省略します。

tell application "Adobe Illustrator"
	tell document 1
		tell text frame 1
			tell paragraph 1
				set justification to right
				--right以外にも下記の種類がある
				--center/full justify/full justify last line center/
				--full justify last line left/full justify last line right
				--/left/right
			end tell
		end tell
	end tell
end tell

08)text frame

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

縦組か横組かしらべます。

tell application "Adobe Illustrator"
	tell document 1
		tell layer 1
			tell text frame 1
				get text orientation --vertical=縦--horizontal=横
			end tell
		end tell
	end tell
end tell

Illustrator10は

text art item

07)path item

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

色や線を設定する

set pt to 2.83464
tell application "Adobe Illustrator"
	tell document 1
		tell path item 1
			set filled to false --塗りをなしにする
			set fill color to {cyan:0, magenta:0, yellow:0, black:0}
			--塗りを白にする
			set stroked to true --線の設定をありにする
			set stroke color to {cyan:0, magenta:100, yellow:50, black:0}
			--線をM100Y50にする
			set stroke width to 0.2 * pt
		end tell
	end tell
end tell

いっぺんに設定するにはset propertiesを使う

set pt to 2.83464
tell application "Adobe Illustrator"
	tell document 1
		tell path item 1
			set properties to {filled:false, ¬
			fill color:{cyan:0, magenta:0, yellow:0, black:0}, ¬
			stroked:true, ¬
			stroke color:{cyan:0, magenta:100, yellow:50, black:0}, ¬
			stroke width:0.2 * pt}
		end tell
	end tell
end tell
ガイドにする
tell application "Adobe Illustrator"
tell document 1
set guides of path item 1 to true
end tell
end tell
        

06)layer

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

作成する

tell application "Adobe Illustrator"
	tell document 1
		make new layer at beginning with properties {name:"TESTレイヤー"}
	end tell
end tell

レイヤーのロック

tell application "Adobe Illustrator"
	tell document 1
		--レイヤー1をロックする
		set locked of layer 1 to true
		--"画像"という名前のレイヤーをロックする
		set locked of layer "画像" to true
	end tell
end tell

04)AIかEPSでバリエーション保存

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

IllustratorCS2、CS3でロックされていない全てのレイヤーの表示・非表示を
切り替えながらaiまたはepsで別名保存します。
aiはCS2でEPSはver8で保存します。
互換性はスクリプト編集プログラムで書き換えられます。

1つのドキュメントで大量にバリエーションを作りたい時に便利です。

たとえば
ドキュメントの場合

ロックされていないレイヤーをいったん非表示にして

「5文字」レイヤーを表示してファイル名「5文字」で書き出し
「3文字」レイヤーを表示してファイル名「3文字」で書き出し
「4文字」レイヤーを表示してファイル名「4文字」で書き出し

完成

ダウンロードはこちらAItoAIorEPS.zip

03)バリエーション画像書き出し

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

IllustratorCS2、CS3でロックされていない全てのレイヤーの表示・非表示を
切り替えながらGIF、JPEG、PSDで書き出します。

ロックされているレイヤーはなにもしません。
1つのドキュメントで大量にバリエーションを作りたい時に便利です。

たとえば
ドキュメントの場合

ロックされていないレイヤーをいったん非表示にして

「5文字」レイヤーを表示してファイル名「5文字」で書き出し
「3文字」レイヤーを表示してファイル名「3文字」で書き出し
「4文字」レイヤーを表示してファイル名「4文字」で書き出し

完成

AppleScriptなのでMac専用です。

■■■使い方■■■
Illustratorドキュメントを開きます。
スクリプト編集プログラムで開き実行してください。
ロールオーバーか標準か聞いてきます。
ロールオーバーを選ぶとファイル名に"_h"が付きます。

ダウンロードはこちらAItoImage.zip

InDesignCS2でリンクされている画像を再リンクし、
同時にドキュメント内の同名の画像も再リンクします。
IndesignCS3でいうとの「すべてのインスタンスを再リンク」のような機能です。

すべてのインスタンスを再リンクの詳細はInDesignの勉強部屋へ
http://study-room.info/id/study/cs3/study12.html

ダウンロードはこちらrelinkall.zip

05)document

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

作成する

tell application "Adobe Illustrator"
	make new document with properties {color space:CMYK}
end tell

保存せずに閉じる

tell application "Adobe Illustrator"
	close document 1 saving no
end tell

保存する

set fileRef to "Macintosh HD:test.eps" as text
tell application "Adobe Illustrator"
	--通常のAI保存
	save document 1 in file fileRef
	--eps保存
	save document 1 in file fileRef as eps with options ¬
		{class:EPS save options, compatibility:Illustrator 10, embed all fonts:true}
	--PDF保存
	save document 1 in file fileRef as eps with options ¬
		{class:PDF save options, compatibility:Acrobat 4, preserve editability:true}
end tell

gif書き出し

set SaveFolder to choose folder with prompt "保存フォルダを選択してください"
tell application "Adobe Illustrator"
	activate
	tell document 1
		set saveF to (SaveFolder as text) & "myPicture.gif"
		export to file saveF as GIF with options ¬
		{class:GIF export options, horizontal scaling:200, vertical scaling:200}
	end tell
end tell

ロックされていない一旦すべて非表示にして、その後レイヤーを順に表示してgif出力。Web用のボタンをつくるのに便利なスクリプトです。

set SaveFolder to choose folder with prompt "保存フォルダを選択"
tell application "Adobe Illustrator"
	activate
	tell document 1
		set countLayer to count layer--レイヤーの数を数える
		repeat with t from 1 to countLayer--レイヤー分繰り返す
			if layer t is not locked then --もしロックされていなければ
			--ロックされていないレイヤーをぜんぶ非表示にする
				set visible of layer t to false
			end if
		end repeat
		repeat with t from 1 to countLayer--レイヤー分繰り返す
			if layer t is not locked then--もしロックされていなければ
				set visible of layer t to true--レイヤーを表示する
				set OutName to name of layer t--レイヤー名を取り出す
				set saveF to (SaveFolder as text) & (OutName as text) & ".gif"
				--レイヤー名でファイル名を作成
				export to file saveF as GIF--レイヤー名でGIF書き出し
				--export to file saveF as JPEG/Photoshop/SVG/PNG8/PNG24
				--などいろいろ選べる
				set visible of layer t to false--レイヤーを非表示にする
			end if
		end repeat
	end tell
end tell

04)変倍する

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

あるgroup itemをX=0pt Y=10pt 幅=100pt 高さ=200ptにしたいとき
set properties to {position:{0, 10}, width:100, height:200}
とやってしまうと大きさや場所は望みの場所に来るのですが線幅やアピアランスが消えてしまいます。解決するには

scale horizontal scale Wper vertical scale Hper line scale Wper ¬
	about top left with transforming objects, transforming fill patterns,¬
	 transforming fill gradients and transforming stroke patterns

を使って変倍したあとpositionで移動させます。

scaleのオプションパラメータは以下のとおり
horizontal scale 120--左右の変倍比率(120だと120%)
vertical scale 120-- 天地の変倍比率
line scale 120-- 線幅の変倍比率
about top left--左上基準で変倍 bottom rightで右下基準 centerで中心基準
with transforming objects,--効果も変倍する
transforming fill patterns,--塗りのパターンも変倍する
transforming fill gradients --グラデも変倍する
and transforming stroke patterns--線のパターンも変倍する

下記が変倍のサンプルスクリプトです。希望の大きさと現在の大きさの比率を出して変倍してから移動しています。

tell application "Adobe Illustrator"
	tell document 1
		tell group item 1
			set properties to {position:{0, 10}, width:100, height:200}
			--これをすると線幅やアピアランスが消えてしまう
			--(Illustrator10では大丈夫)
			set myCB to control bounds
			--オブジェクトを選択してドラッグするときに表示される輪郭。
			--オブジェクトよりひとまわり大きい。
			set myGB to geometric bounds
			--オブジェクトのアンカーポイントぴったりのサイズ
			set myVB to visible bounds
			--オブジェクトに線の設定がされていれば線幅も加えたサイズになる。
			set myX to item 1 of myGB
			set myY to item 2 of myGB
			set myW to (item 3 of myGB) - myX
			set myH to (item 4 of myGB) - myY
			set Wper to 100 / myW * 100
			set Hper to 200 / myH * 100
			scale horizontal scale Wper vertical scale Hper line scale Wper ¬
				about top left with transforming objects, transforming fill patterns,¬
				 transforming fill gradients and transforming stroke patterns
			set position to {0, 10}
		end tell
	end tell
end tell

03)オブジェクトを作成する

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

rectangleを作成

tell application "Adobe Illustrator"
	tell document 1
		make new rectangle at beginning with properties ¬
		{bounds:{0, 200, 200, 50}, fill color:{cyan:100, magenta:100, yellow:0, black:0}}
	end tell
end tell

text frameを作成(ポイントテキストとエリアテキスト)

tell application "Adobe Illustrator"
	tell document 1
		--ポイントテキスト
		make new text frame at beginning with properties ¬
		{position:{0, 200}, contents:"あいうえお"}
		--エリアテキスト
		make new text frame at beginning with properties¬
		 {geometric bounds:{100, 90, 160, 180}, contents:"かきくけこ", kind:area text}
	end tell
end tell

path itemを作成(entire pathで線の両端の座標を指定する。)

tell application "Adobe Illustrator"
	tell document 1
		make new path item at end with properties ¬
		{stroke color:{cyan:C, magenta:M, yellow:Y, black:K}, ¬
		stroke width:W, entire path:{{class:path point info, anchor:{X1, Y1}}, ¬
		{class:path point info, anchor:{X2, y2}}}}
	end tell
end tell

02)複製・移動する

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

複製する

tell application "Adobe Illustrator"
	tell document 1
		duplicate text frame 1 to end --背面に複製
		duplicate text frame 1 to beginning --前面に複製
		duplicate path item 1 to end of layer 2
		--レイヤー2の背面に複製
	end tell
end tell

移動する

tell application "Adobe Illustrator"
	tell document 1
		move text frame 1 to end --背面に移動
		move text frame 1 to beginning --前面に移動
		move path item 1 to end of layer 2
		--レイヤー2の背面に移動
	end tell
end tell

オブジェクトが存在するか

tell application "Adobe Illustrator"
	tell document 1
		if exists path item 1 then
			delete path item 1
		end if
	end tell
end tell

下記は選択されたアイテムの場所を調べX軸に10mmをプラスしている。AppleScriptを送信する際単位はptになるので10に2.83464をかける事になる

set pt to 2.83464--変数ptに2.83464を入れておく
tell application "Adobe Illustrator"
	tell document 1
		set selectedItemList to every page item whose selected is true
		--選択されたオブジェクトを調べる(listで返ってくる)
		repeat with selectedItem in selectedItemList
		--選択オブジェクト分繰り返す。
			set myPos to position of selectedItem--座標を調べる(listで返る)
			set myX to item 1 of myPos--X座標を取り出す
			set myY to item 2 of myPos--Y座標を取り出す
			set position of selectedItem to {myX + 10 * pt, myY}
			--新しい座標(X+10×2.83464)にする
		end repeat
	end tell
end tell

JavaScript、AppleScript、VBScript共通で
Illustrator10-CS3まで共通
/Adobe Illustrator CS3/プリセット/スクリプト/
にスクリプトをコピーします。

ins_illustrator01.jpg

すると下記のようにメニューに表示されるようになります。

ins_illustrator02.jpg

AppleScriptはダブルクリックするとスクリプト編集プログラムが起動するので実行ボタンをクリックしてください。

VBScriptはダブルクリックすると実行されます。

IllustratorCS2、CS3の配置画像にすべてFinderでラベルを付けます。
ただそれだけです。いっぱい画像を配置してどれがどれだか
わからなくなった時にお使いください。

       ↓

ダウンロードはこちらIllustratorlinklabel.zip

CS4 OSX10.6で動かないというレポートをいただいたので修正しました。2010/5/5
CS4 OSX10.6ダウンロードはこちらIllustratorCS4linklabel.zip

20)選択画像-時計回り0.1度回転

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

InDesignCS2、CS3で画像を0.1度刻みで回転させます。細かい微調整をたくさんしないといけない時はいちいち入力するのがめんどうなのでこのスクリプトをスクリプトパレットからダブルクリックして使っています。
ショートカットに割り当てて
ページ移動はShift Pagedown Shift Pageup
画像の右回転はShift →
画像の左回転はShift ←
画像の移動は→←↓↑
って感じでいけます。

こんな感じにちょっとだけ回転

ショートカットを割り当てるのがおすすめです。

ここからダウンロードInDesignrotation.zip

InDesignCS2、CS3にPDFかフォルダ内の画像を1ページに1枚ページを作成しながら配置していきます。

■■■使い方■■■
ドキュメントを開いた状態で
スクリプトパレットの「このスクリプト」をダブルクリックします。
InDesignPagePDF配置CS2-3cpt.scpt
のほうはPDFを選択するとつぎつぎにPDFが配置されていきます。
InDesignPage配置CS2-3cpt.scpt
のほうはフォルダを選択するとつぎつぎにフォルダ内の画像が配置されていきます。

ダウンロードはこちらInDesignPage.zip

2006/10月山口県印刷フォーラムに招待していただき
講演したときのレジュメです。このブログの記事にも掲載しています。
下記文章は奥付から。

このレジュメはAppleScriptのセミナーのためにわたしのホームページの
TakeNoteというコーナーから情報を集め、加筆したものです。
いいわけがましいのですが、書くほどに自分の知識不足を思い知らされ、
そして書かない方がよいのではと常に思います。ただAppleScriptを勉強す
る資料が非常に少なく、よくどうやって勉強するのか聞かれる事。それと
AppleScriptに少しでも恩返ししたい事。そんなこんなでなんとかがんばっ
て書いています。間違いや不適切な表現。もっと良い記述方法など多々ある
と思いますが、わたし程度の知識でもそれなりのものが作れると、逆に励み
にしていただければと思います。

ダウンロードはこちらASSeminarA4.pdf

30)InDesignトンボ

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

InDesignCS2、CS3でトンボを作ります。
オブジェクトを選択して実行してください。
あんまりまじめにエラー処理していませんのでテキスト選択状態で実行するとエラーになります。

ダウンロードはこちらInDesigntombo.zip

05)リファレンスの調べ方

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

用語辞書よりもっと詳しい解説が見たい場合は各メーカーが提供しているリファレンスやガイドブックを見ます。
AdobeInDesignやAdobeIllustratorなどはスクリプトリファレンスやスクリプティングガイドなどがインストーラCDに入っています。リファレンスは用語辞書で命令方法やテキストフレームやカラーといったオブジェクトの指定方法や使えるプロパティを調べるのに役に立ちます。(英語の場合もあります。)スクリプティングガイドは初心者からAppleScriptを扱う解説が詳しく載っています。特にInDesignCS2のスクリプティングガイドは非常に丁寧に解説されており。サンプルも多数掲載されています。私自身も知らないことやあやふやなことがたくさん載っていて、まだまだ勉強しなくてはと実感させられます。このガイドをくわしく読むだけで十分AppleScriptの勉強になりますし、この原稿を読む必要もなくなるぐらいです(笑)

04)コマンドを送る

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

get propertiesでtext frameのプロパティをいろいろ変更できました。ではtext frameを作成するのにはどうすれば良いでしょう。これにはコマンドを使います。用語辞書のCore Suiteのコマンドにmakeがあります。

make: Make a new element
	make
		new  type class  -- the class of the new element.
		at  location reference  -- the location at which to insert the element
		[with data  anything]  -- the initial data for the element
		[with properties  record]  -- the initial values for the properties of the element
	Result:   reference  -- to the new object(s)

makeというコマンドの次の行にnew type classとありさらに次にat location referenceとあります。これは

make new 作りたいオブジェクトのクラス at 場所

という書き方になります。その後の[]でくくられている部分は省略可能です。Resultにreferenceとありますがこれは返り値で新しく出来たオブジェクトが返ってきます。では作りたいオブジェクトのクラスですが、先ほどget propertiesで調べたtext frameクラスを渡してみましょう。場所は最前面につくるのならbeginningでいいでしょう。

tell application "Adobe Illustrator"
	tell document 1
		make text frame at beginning
	end tell
end tell

ぽつんと小さなオブジェクトが出来ました。これがスクリプトから作成したtext frameです。さきほど省略したwith propertiesを使ってみましょう。with properties recordと書いてあるのでwith properties {○○:〜〜}という使い方になります。text frameのプロパティは先ほど調べました。その中でcontentsを使ってみましょう。

tell application "Adobe Illustrator"
	tell document 1
		make text frame at beginning with properties {contents:"あああ"}
	end tell
end tell

ほかにもコマンドには

close: --ドキュメントを閉じる
	close document 1 saving no--保存せずに
count  reference  -- オブジェクトの数を数える
	count taxt frames
delete: -- オブジェクトを消す
	delete text frame 1
duplicate: --複製する
	duplicate text frame 1 to end
exists: --オブジェクトが存在するか?
	if exists text frame 1 then〜end if
move: --オブジェクトを動かす
	move text frame 1 to end of layer 1
open: --ファイルを開く
	open alias "Macintosh HD:test.ai"
save: --ファイルを保存する
	save  document  1 in  file "Macintosh HD:test.ai" as Illustrator

などがあります。まだまだ他にもいろいろありますので後述します。

03)用語辞書の調べ方

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

AppleScriptからアプリケーションをコントロールするときは用語辞書を見て使えるコマンドやセットできるプロパティを調べます。
用語辞書を見るにはまずスクリプト編集プログラムのウィンドウメニューからライブラリを選びます。ライブラリウィンドウ上部の+ボタンをクリックしてInDesignやIllustratorを選択します。ライブラリウィンドウに登録されたらダブルクリックしてください。用語辞書が開きます。
試しにAdobe Illustrator Suiteのクラスでtext frameを見てみましょう。
as4-3.jpg

Class text frame: Text frame item
Plural form:
	text frames
Elements:
	character by numeric index, as a range of elements, 
				before/after another element, satisfying a test
	insertion point by numeric index, as a range of elements, 
				before/after another element, satisfying a test
	line by numeric index, as a range of elements, 
				before/after another element, satisfying a test
	paragraph by numeric index, as a range of elements, 
				before/after another element, satisfying a test
	text by numeric index, as a range of elements, 
				before/after another element, satisfying a test
	word by numeric index, as a range of elements, 
				before/after another element, satisfying a test
Properties:
	<Inheritance>  page item  [r/o]  -- subclass of page item
	properties  record  
		-- all of this object's properties returned in a single record
	anchor  list  
		-- the position of the anchor point (start of base line for point text)
	column count  integer  
		-- the column count in the text frame (area text only)
	column gutter  real  
		-- the column gutter in the text frame (area text only)
	content variable  anything  
		-- the content variable bound to this text art item
	contents  Unicode text  
		-- the text contents of this text frame
	end T value  real  
		-- the end position of text along a path, as a value relative to 
		   the path's segments (path text only)
	flow links horizontally  boolean  
		-- Flow text between linked frame horizontally first. (area text only)
	kind  area text/path text/point text  [r/o]  
		-- the type of a text frame item
	matrix  matrix  [r/o]  
		-- The transformation matrix of the text frame object
	next frame  text frame  
		-- the linked text frame following this one
	optical alignment  boolean  
		-- is the optical alignment active?
	previous frame  text frame  
		-- the linked text frame preceding this one
	row count  integer  
		-- the row count in the text frame (area text only)
	row gutter  real  
		-- the row gutter in the text frame (area text only)
	selection  a list of text  [r/o]  
		-- the selected text (ranges) in the story
	spacing  real  
		-- the amount of spacing (path text only)
	start T value  real  
		-- the start position of text along a path, as a value relative to 
		  the path's segments (path text only)
	story  story  [r/o]  
		-- the story of the text frame
	text orientation  horizontal/vertical  
		-- the orientation of the text in the frame
	text path  text path  [r/o]  
		-- the path for the text frame (area and path text)
	text range  text  [r/o]  
		-- the text range of the text frame

大量にでてきますが--より右に書いてあるのが解説です。
Elementsは要素です。character(キャラクター)が書いてありますが、text frameの要素にcharacterがあるということです。これをスクリプトにすると下記のようにさらに深い階層になります。characterの用語辞書はText Suiteに書かれています。

tell application "Adobe Illustrator"
	tell document 1
		tell text frame 1
			tell character 2
				get properties
			end tell
		end tell
	end tell
end tell

Propertiesがプロパティですね。この値を変更することによって、Illustratorを操ることが出来ます。

contents  Unicode text  -- the text contents of this text frame

はプロパティcontentsの値はユニコードテキストだということと、--以下の説明をみるとtext frameの内容だということがわかります。

row count  integer  -- the row count in the text frame (area text only)

というのはrow count(段組みの段数)がinteger(整数)で返ってくることがわかります。 [r/o] と書いてあるのはread onlyのため変更することは出来ません。

kind  area text/path text/point text  [r/o]  -- the type of a text frame item

kindというのは種類でarea textかpath textかpoint textで値が返ってきます。テキストボックスを作ればarea textでクリックして文字を作ればpoint textで、ベジェ曲線に沿うようなのはpath textです。こちらも[r/o]なので変更不可です。(実はIllustrator10では変更できましたが変更すると変な形になっていました。)

AppleScriptでアプリケーションをコントロールする最も簡単な方法は適当にオブジェクト(テキストなど)をInDesignやIllustratorで作ってプロパティを調べる方法です。調べたらそれを変更できるかやってみます。
Illustratorに"あいうえお"というテキストを作りプロパティを見ましょう。

tell application "Adobe Illustrator"--Illustratorの
	tell document 1--最前面のドキュメントの
		tell page item 1--最前面のアイテムの
			get properties--プロパティを調べる
		end tell
	end tell
end tell

このget propertiesは「QuarkXPressを操ろう」をNiftyのFPRINTフォーラムで連載されてい鎌田さんゆずりの方法でこれを初めて知って以来いろんなオブジェクトにget propertiesしてきました。いまAppleScriptが使えるようになったのもこのget propertiesのおかげです。

さていろいろな値がかえってきました。抜粋します。

contents:"あいうえお", 
text orientation:horizontal, 
position:{86.22216796875, 641.115234375},
width:60.0,
height:15.0361328125,
name:"",
class:text frame,
index:1

まず注目はclassです。このテキストのクラス名はtext frameだということがわかります。indexは1なのでpage item 1ではなくてtext frame 1で指定する方が良いでしょう。contents:"あいうえお"とあります。これを変えてみましょう。変更するにはAppleScriptで変数に値を入れる方法と同じです。
set 変数 to ○○
でしたね。では

tell application "Adobe Illustrator"--Illustratorの
	tell document 1--最前面のドキュメントの
		tell text frame 1--最前面のtext frameの
			set contents to "かきくけこ"--contentsを"かきくけこ"にする。
		end tell
	end tell
end tell

これを動かしてください。文字内容が変わりましたね。

tell application "Adobe Illustrator"
	tell document 1
		tell text frame 1
			set position to {100, 100}
		end tell
	end tell
end tell

を動かすと文字の場所が変わります。場所とcontentsを一度にセットすることも出来ます。

tell application "Adobe Illustrator"
	tell document 1
		tell text frame 1
			set properties to {position:{100, 100}, contents:"かきくけこ"}
		end tell
	end tell
end tell

もちろんget propertiesだけではAppleScriptでInDesignをコントロールするだけの情報を調べるのは困難です。text frame 1でget propertiesしてもフォントやカラーのプロパティはありませんでした。実際フォントやカラーはtext frameのプロパティではなくtext frameの要素であるparagraphやcharacterのプロパティです。しかし、それを調べるのはget propertiesだけでは無理です。このようにもっといろいろ調べたい場合は後述する用語辞書やリファレンスを見ます。

01)階層構造 tell〜end tell

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

AppleScriptでアプリケーションをコントロールするときは階層になっています。"tell"や"of"でつないでいきます。例えばInDesignでは

tell application "Adobe InDesign CS2_J"--InDesignの
	tell document 1--最前面のドキュメントの
		tell page 1--1ページ目の
			tell text frame 1--最前面のテキストフレームの
				set contents to "あいうえお"--内容を"あいうえお"にする。
			end tell
		end tell
	end tell
end tell

とういうように階層になっています。InDesignのドキュメントの1ページ目のテキストフレームという意味。ofを使うと下のように簡略化して書けます。

tell application "Adobe InDesign CS2_J"
	tell text frame 1 of page 1 of document 1
		set contents to "あいうえお"
	end tell
end tell

文字スタイルの数を取り出すときもドキュメントの文字スタイルの数というように階層をたどっていって取り出します。

tell application "Adobe InDesign CS2_J"
	tell document 1
		set myChrCount to count character styles--文字スタイルの数を数える。
	end tell
end tell

01)ASじゃんけんスクリプト

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

下記のようなじゃんけんするスクリプトを書いてみました。簡単で楽しいのでスクリプトの練習にはもってこいです。
まず「さいしょはぐー」からはじめます。「ぐー」を選ばなければ「最初はぐーだよ」とダイアログを出します。次にコンピュータの手を決めます。random number from 1 to 3でランダムな数字を出してから決めます。最後に勝ち負け判定をして終了です。

set AAA to choose from list {"ぐー", "ちょき", "ぱー"} with prompt "さいしょはぐー!"
if AAA is not {"ぐー"} then
	display dialog "最初はぐーだよ"
end if

set AAA to choose from list {"ぐー", "ちょき", "ぱー"} with prompt "じゃーんけーん"
set myJanken to AAA--わたしの手はmyJankenに入る
set BBB to random number from 1 to 3--1から3のランダムな数字がBBBに入る
if BBB is 1 then--もし1なら
	set asJanken to {"ぐー"}--変数asJankenを {"ぐー"}にする
else if BBB is 2 then
	set asJanken to {"ちょき"}
else
	set asJanken to {"ぱー"}
end if

if myJanken is asJanken then
	display dialog "あいこだよ"
else if myJanken is {"ぱー"} and asJanken is {"ぐー"} then
	display dialog "わたしの手は" & asJanken & "でした。あなたの勝ちです。"
else if myJanken is {"ぐー"} and asJanken is {"ちょき"} then
	display dialog "わたしの手は" & asJanken & "でした。あなたの勝ちです。"
else if myJanken is {"ちょき"} and asJanken is {"ぱー"} then
	display dialog "わたしの手は" & asJanken & "でした。あなたの勝ちです。"
else if myJanken is {"ぱー"} and asJanken is {"ちょき"} then
	display dialog "わたしの手は" & asJanken & "でした。あなたの負けです。"
else if myJanken is {"ぐー"} and asJanken is {"ぱー"} then
	display dialog "わたしの手は" & asJanken & "でした。あなたの負けです。"
else if myJanken is {"ちょき"} and asJanken is {"ぐー"} then
	display dialog "わたしの手は" & asJanken & "でした。あなたの負けです。"
end if

さてこのスクリプトには欠点がいくつもあります。まず「さいしょはぐー」の部分ですが「ぐー」を出さなくてもじゃんけんに進めます。「ぐー」をださない限り進めなくしたいものです。

repeat
	set AAA to choose from list {"ぐー", "ちょき", "ぱー"} ¬
			with prompt "さいしょはぐー!"
	if AAA is not {"ぐー"} then--ぐーでなければ
		display dialog "最初はぐーだよ"--ダイアログを出す
	else--そうでなければ(ぐーなら)
		exit repeat--repeatを抜ける
	end if
end repeat

上のようにrepeatを入れぐーならばrepeatを抜けれるようにすれば「さいしょはぐー」の部分は完成です。
次の欠点は勝っても負けてもあいこでもじゃんけんが終了してしまう事です。こちらもrepeat文で直す事が出来ます。

repeat
	set AAA to choose from list {"ぐー", "ちょき", "ぱー"} with prompt "じゃんけん"
	set myJanken to AAA
	set BBB to random number from 1 to 3
	if BBB is 1 then
		set asJanken to {"ぐー"}
	else if BBB is 2 then
		set asJanken to {"ちょき"}
	else
		set asJanken to {"ぱー"}
	end if
	
	if myJanken is asJanken then
		display dialog "あいこだよ"
	else if myJanken is {"ぱー"} and asJanken is {"ぐー"} then
		display dialog "わたしの手は" & asJanken & "でした。あなたの勝ちです。"
		exit repeat
	else if myJanken is {"ぐー"} and asJanken is {"ちょき"} then
		display dialog "わたしの手は" & asJanken & "でした。あなたの勝ちです。"
		exit repeat
	else if myJanken is {"ちょき"} and asJanken is {"ぱー"} then
		display dialog "わたしの手は" & asJanken & "でした。あなたの勝ちです。"
		exit repeat
	else if myJanken is {"ぱー"} and asJanken is {"ちょき"} then
		display dialog "わたしの手は" & asJanken & "でした。あなたの負けです。"
		exit repeat
	else if myJanken is {"ぐー"} and asJanken is {"ぱー"} then
		display dialog "わたしの手は" & asJanken & "でした。あなたの負けです。"
		exit repeat
	else if myJanken is {"ちょき"} and asJanken is {"ぐー"} then
		display dialog "わたしの手は" & asJanken & "でした。あなたの負けです。"
		exit repeat
	end if
end repeat

これでいちおう完成ですが、勝ち負け判定に同じ処理を何度も書いています。こういうのはスクリプト的に非常によろしくないですし、そもそも勝ち負け判定自体もif文がいっぱいでスマートではありません。
まず勝ち負け判定ですが「あいこ」なら繰り返しでそれ以外なら勝ちか負けなので繰り返しを抜けます。そして「勝ち」かどうか判定できれば「あいこ」と「勝ち」以外は自動的に「負け」なので判定する必要はありません。これをスクリプトにすると下記のようになります。

	if myJanken is asJanken then
		display dialog "あいこだよ"
	else
		if myJanken is {"ぱー"} and asJanken is {"ぐー"} then
			display dialog "わたしの手は" & asJanken & "でした。あなたの勝ちです。"
		else if myJanken is {"ぐー"} and asJanken is {"ちょき"} then
			display dialog "わたしの手は" & asJanken & "でした。あなたの勝ちです。"
		else if myJanken is {"ちょき"} and asJanken is {"ぱー"} then
			display dialog "わたしの手は" & asJanken & "でした。あなたの勝ちです。"
		else
			display dialog "わたしの手は" & asJanken & "でした。あなたの負けです。"
		end if
		exit repeat
	end if

これで多少はスマートになりましたがまだまだ工夫の余地がありそうです。私がいろいろ考えたところなんとか下記のようになりました。みなさんもいろいろ工夫してみてください。

set MSGSTR to "さいしょはぐー!"
repeat
	set asJanken to item (random number from 1 to 3) of {{"ぐー"}, {"ちょき"}, {"ぱー"}}
	set myJanken to choose from list {"ぐー", "ちょき", "ぱー"} with prompt MSGSTR
	if myJanken is {"ぐー"} and MSGSTR is "さいしょはぐー!" then
		set MSGSTR to "じゃーんけーんぽん!"
	else if MSGSTR is "さいしょはぐー!" then
		display dialog "さいしょはぐーだよ!"
	else if myJanken is asJanken then
		set MSGSTR to "あーいこーでしょっ!"
	else if {myJanken, asJanken} is {{"ぱー"}, {"ぐー"}} or ¬
			{myJanken, asJanken} is {{"ぐー"}, {"ちょき"}} or ¬
			{myJanken, asJanken} is {{"ちょき"}, {"ぱー"}} then
		set FLG to "勝ち"
		exit repeat
	else
		set FLG to "負け"
		exit repeat
	end if
end repeat
display dialog "わたしの手は" & asJanken & return & "あなたの手は" & ¬
			myJanken & return & "あなたの" & FLG & "です。"

InDesignCS2、CS3でリンクされている画像を他のフォルダの同じ名前の画像に再リンクします。同じファイル名で拡張子が違うものに再リンクしたり、ひとつづつ結果を確認しながら再リンクできます。
CS3では本来の機能での再リンクをまずはおすすめします。
http://study-room.info/id/study/cs3/study12.html

CS4のフォルダに再リンクという機能が便利そうだったので
CS3、CS2でも使えるように作ってみました。

http://study-room.info/id/study/cs4/study03.html

これがダイアログです。

差し替えるか聞いてきます。

差し替え結果も表示してOKなら次へ進みます。

ダウンロードはこちらInDesignrelink.zip

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

05) ファイルの読み込み

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

下記はファイルを選択し読み込んだ内容を変数myStrに入れ表示します。
開いたファイルは必ず閉じるようにしてください。

set myFile to choose file with prompt "ファイルを選択"
open for access myFile--ファイルを開く
set myStr to read myFile--読み込み結果を変数myStrに入れる
close access myFile--ファイルを閉じる
display dialog myStr

上とほとんど同じなのですが as list using delimiter {return}を付けるとtextではなく改行区切りのlistで読み込みます。1行づつ処理したい場合は便利です。

set myFile to choose file with prompt "ファイルを選択"
open for access myFile
set myList to read myFile as list using delimiter {return}
close access myFile
display dialog item 1 of myList

改行コードがcrのShiftJISなら下記1行づつ処理。

set textPath to "Macintosh HD:test.txt"
set fobj to open for access alias textPath
read fobj
set str_data to result
close access fobj

set OriginalDelimiters to AppleScript's text item delimiters
set AppleScript's text item delimiters to {return}
set str_data to text items of str_data
repeat with L from 1 to count str_data
display dialog "#" & item L of str_data & "#"
end repeat
set AppleScript's text item delimiters to OriginalDelimiters

crlfは
set AppleScript's text item delimiters to {(ASCII character (13)) & (ASCII character (10))}

ifは
set AppleScript's text item delimiters to {ASCII character (10)}
です。

UTF16なら as Unicode textをつけます。
set textPath to "Macintosh HD:testutf16cr.txt"
set fobj to open for access alias textPath
read fobj as Unicode text
set str_data to result
close access fobj

04)ファイル保存choose file name

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

ファイル保存ダイアログを出します。alias型の値で返ってきます。このダイアログを使うのはテキスト整形したものを保存する時です。

set newFile to choose file name
display dialog newFile as string

作成したファイルに書き込むには下記を使います。

set newFile to choose file name

set file_h to open for access newFile with write permission
set eof file_h to 0
write "こんにちは" to file_h--書き込む
close access file_h--ファイルを閉じる

03)フォルダ選択 choose folder

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

choose folderでフォルダ選択ダイアログを出せます。フォルダはalias型の値で返ってきます。フォルダ内のファイルを全て処理するには下記のようなrepeatを使います。list folder フォルダでフォルダ内のファイルがリスト形式で返ってきますが、それらはファイル名のみです。そのためファイルの位置はフォルダのフルパス+ファイル名で調べます。

set myFol to choose folder with prompt "フォルダを選択"
set myFol to myFol as string--フォルダのフルパス
repeat with myFile in list folder myFol without invisibles--フォルダ内リピート
	set this_item to (myFol & myFile) as alias--フォルダとファイル名を合体
	set Fpath to this_item as string--フルパスを取り出す
	display dialog Fpath
end repeat

下記はフォルダ内のフォルダも含めて繰り返します。

set myFol to choose folder with prompt "フォルダを選択"
my folderLoop(myFol)--オリジナル関数folderLoopを呼び出す

on folderLoop(myFol)
	set myFol to myFol as string
	repeat with myFile in list folder myFol without invisibles
		set this_item to (myFol & myFile) as alias
		set myInfo to info for this_item
		if folder of myInfo is true then
			my folderLoop(this_item)--自分自身を呼び出す
		else
			set Fpath to this_item as string
			display dialog Fpath
		end if
	end repeat
end folderLoop

02)ファイル選択choose file

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

ファイルを選択します。choose fileで選ぶとalias型の値で返ってくるのでas stringで型変換するとテキスト形式のフルパスが返ってきます。その後なんらかの処理を(InDesignに配置するとかテキストを読み込むとか)すればOKです。

set this_item to choose file with prompt "配置ファイルを選択"
set Fpath to this_item as string--ファイルのフルパスをテキスト形式で取り出す
display dialog Fpath--表示する。

下記ではファイルタイプが"EPSF"か"EPSP"かまたは種類が"EPS ファイル"のファイルだけ処理関数に渡すといったちょっと気の利いた事をしています。下記では処理関数setEPS2を呼び出しています。

set this_item to choose file with prompt "配置ファイルを選択"
set Fpath to this_item as string--ファイルのフルパスをテキスト形式で取り出す
set Finfo to info for this_item--ファイルインフォメーションを取り出す
set FType to file type of Finfo
				--ファイルインフォメーションからファイルタイプを取り出す
set Fkind to kind of Finfo--ファイルインフォメーションから種類を取り出す
if FType is "EPSF" or FType is "EPSP" or Fkind is "EPS ファイル" then
	my setEPS2(this_item, Fpath)--処理をする。
end if

01)対話する display dialog

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

確認
display dialogの出し方と何を入力したか調べます。

display dialog "こんにちは" --キャンセルをクッリクすると処理終了

display dialog は変数を単に表示するだけで今入っている変数の値を確認するデバッグ用としてつかえる。

入力

set myAnswer to display dialog "入力してください" default answer "ここに入力"
set inputStr to text returned of myAnswer
display dialog "入力したのは「" & inputStr & "」ですね"

入力された文字は変数myAnswerの中に入っている。text returnedで取り出す事が出来る。

ボタン

set myAnswer to display dialog "食べたいものは" buttons {"くり", "こめ", "つけもの"}
set selectedBtn to button returned of myAnswer
display dialog "クリックしたのは「" & selectedBtn & "」ですね"

押されたボタンも変数myAnswerに入っている。こちらはbutton returnedで取り出す。

入力とボタン

set myAnswer to display dialog "ページ数" default answer "1" buttons {"縦組", "横組"}
set inputStr to text returned of myAnswer
set selectedBtn to button returned of myAnswer
display dialog "ページ数が" & inputStr & "ページで" & selectedBtn & "ですね"

入力した文字と押されたボタン両方を調べる事も出来る。

選択

set mySelect to choose from list {"りんご", "みかん", "ぶどう"} with prompt ¬
		"選択してください"--¬は実際には改行を入れない
display dialog item 1 of mySelect--listで返ってくるのでitem 1 で取り出す。

こちらはリストから選択する。さらに下記はリストから複数選択可能にしたもの。どちらも値はリストで返ってくるので要注意。

set mySelectList to choose from list {"りんご", "みかん", "ぶどう"} with prompt ¬
		"選択してください" with multiple selections allowed --複数選択可
repeat with mySelect in mySelectList --listの中のitem分繰り返す
	display dialog mySelect
end repeat

InDesignって配置された画像を個々に拡大・縮小とかできるのかな。。。
調べたけどわからなかったので
複数選択した画像を個々に拡大・縮小するスクリプトを作りました。

InDesignCS2からの変形を再実行
http://study-room.info/id/study/main4/study13.html
とはちょっと違って、選択された配置画像をすべて拡大縮小します。

Indesignresizeimage.zip

08)演算子2

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

文字を連結するときは&を使います。
 

set AAA to 10
set myStr to "変数AAAの内容は" & AAA & "です。"
=>"変数AAAの内容は10です。"

比較演算子などif文の中に書き込む内容です。

set AAA to 10
if AAA > 5 then
display dialog "変数AAAは5より小さい"
end if
if AAA is 10 then
display dialog "変数AAAは10"
end if
if AAA is not 5 then
display dialog "変数AAAは5以外"
end if
if AAA is in {9, 10, 11} then
display dialog "変数AAAは9,10,11に含まれる"
end if

07)演算子1

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

足し算、引き算、かけ算、割り算。基本はこれだけです。あとできれば割っ たあまりと割ったあまりを除いた整数部分が取り出せると便利です。

set X to A+1--変数Xに変数A+1を入れる
set X to X-1--変数Xに変数A-1を入れる
set X to N*5--変数Xに変数N×5を入れる
set X to N/2--変数Xに変数N÷2を入れる
set X to 5 mod 2--=>1になる。割ったあまり。
set X to 5 div 2--=>5になる。割ってあまりを除いた整数部分

割ったあまりをだすmodを使って偶数か奇数かを調べることが出来ます。

if N mod 2 = 1 then--もし変数Nを2で割ったあまりが1なら
    display dialog "変数Nは奇数です。"
else--そうでないなら
    display dialog "変数Nは偶数です。"
end if

これが最低限ですが、これだけでScriptを書くには十分です。

** コマンド(命令)**
メッセージを表示する。下のような命令をコマンドといいます。

display dialog AAA

上のように命令があってその後にパラメータが入ります。ほかにもいろい
ろなパラメータやオプションパラメータなどたくさんの文字が続くことも多
いです。さらに値を返すコマンドもあります。AdobeやQuarkXPress共通で
なにかを作りたいときにmakeというコマンドを作ります。

make text frame

というように書くのですが

set myObj to make text frame

とすると作られたオブジェクトが変数myObjに入ります。

ハンドラ(関数)

Round()は数字をまるめる関数です。関数には()←カッコがついていてカッ
コの中には値を入れます。

set BBB to Round(10.5)

自分でハンドラ(関数)を作る事も出来ます。

my Disp("あいうえお")
--自分で作った関数Dispを呼び出す。myが必要なので注意
on Disp(AAA)
--自分が作った関数Dispカッコの中の変数に渡された値が入る。
display dialog AAA & "です。"
end Disp
        

くりかえし1
1から10まで順に繰り返す。InDesignなら1ページから10ページまで処理
をするという時にrepeat文を使います。くりかえしでもっともよく使う処理
が下です。下記は変数CCCに1 〜10までの値を順番に入れながら実行します。
必ずend repeatがいります。

repeat with CCC from 1 To 10
    display dialog CCC
end repeat
=>結果は1から10までの文字がダイアログで発生する。

くりかえし2
無限に繰り返す処理を入れある条件が成立したら処理を終わります。

set DDD to 1--変数DDDに1を入れる。
repeat--無限に繰り返す。
    if DDD > 50 then--もし変数DDDが50以上なら
        exit repeat--繰り返しを抜ける
    end if
    set DDD to DDD + 1--変数DDDに1をプラスする。
end repeat

くりかえし3
配列変数のなかを1つづつ変数に入れます。

set meishiFile to {"たけうち", "森田", "おおもり", "ばば", "なかむら"} as list
repeat with myName in meishiFile
--↑配列変数meishiFileの中を1つづつ順に変数myNameに入れる
    display dialog myName & "さん"
end repeat
=>「たけうちさん」「森田さん」「おおもりさん」。。。と順に表示される

「もしも〜なら」という処理です。必ずend ifがいります。

set AAA to "あいうえお"
if AAA is "あいうえお" then --もしAAAが"あいうえお"なら
display dialog "あいうえおです。"
end if

「もし〜ではなかったら」の場合は下記のように書きます。

set AAA to "あいうえお"
if AAA is not "あいうえお" then --もしAAAが"あいうえお"ではなかったら
display dialog "あいうえおではありません。"
end if

「もしも〜なら」と「そうではなくて〜なら」と「それ以外なら」という
ように条件によって振り分ける場合は下のように書きます。

set AAA to "あいうえお"
if AAA is in "あいう" then --もしAAAが"あいうえお"に含まれるなら
display dialog "あいうえおです。"
else if AAA is "かきくけこ" then --もしAAAが"かきくけこ"なら
display dialog "かきくけこです。"
else--それ以外なら
display dialog "それ以外です。"
end if

「もしも〜でさらに〜なら」と「もしも〜または〜なら」というように2つ
の条件によって振り分ける場合は下のように書きます。

set AAA to 10
if 0 < AAA and AAA < 20 then--andの入ったif文
display dialog "変数AAAは0より大きく20より小さい"
end if
if 0 > AAA or AAA > 20 then--orの入ったif文
display dialog "変数AAAは0より小さいまたは20より大きい"
end if

以上のようにif文はかなり簡単ですがif文そのものよりもどこでif文を使う
かという考え方のほうが理解しにくいのかもしれません。

変数は何でも入る名前のついた入れ物と書きましたがlistはその入れ物の中にさらに仕切りがあり番号がついているイメージです。例えば名刺ファイ
ルがあったとします。その中に名刺を入れます。1から順に名刺を入れてい
きます。1番目は"たけうち"さん、2番目は"森田"さん、3番目は"おおもり"さん、
4番目は"ばば"さん、5番目は"なかむら"さんといった感じです。あとで取り
出すときに、名刺ファイルの5番目とか1番目から順番に全部の名刺とかい
うように取り出します。これをAppleScriptで書くと

set meishiFile to {"たけうち","森田","おおもり","ばば","なかむら"} as list
display dialog item 5 of meishiFile & "さん"
=>"なかむらさん"

というようになります。このlist変数がわかりにくいのですが例えば
Illustratorで選択したアイテムを調べると選択アイテムは1つとは限らないの
で複数のlistで値が帰ってきます。そこから選択アイテムの1つめといったよ
うに取り出します。とにかく非常によく出てくるしlistを使わないと
AppleScriptは書けないので、がんばって理解してください。

02.jpg

変数meishiFileに1つめは「たけうち」2つめは「森田」といれる。取り出す
ときはmeishiFIleの1番目というように取り出す。

02)変数 set 変数 to ○○

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

変数は入れ物です。名前のついた入れ物で、なんでも入ります。取り出す
ときは入れ物の名前を指定してやると中身を見ることができます。変数には
好きな名前をつけることができます。

set myStr to "こんにちは。"--変数myStrに"こんにちは。"を入れる
display dialog myStr--変数myStrをダイアログに表示

もちろん変数ですから数字も入れることができます。

set Y to 1--変数Yに1を入れる
set X to 5--変数Xに5を入れる
display dialog Y+X--変数Y+Xの結果をダイアログに表示

※--はコメントで、プログラムでは--以降の文字は無視されます。

変数の種類と変数の型変換

01.jpg

boolean--真偽値
integer--整数
real--実数
string--文字列
list--リスト
booleanにはtrue(真)かfalse(偽)が入ります。integerは整数です。小
数点を含む実数を整数に変換するときに使います。

set X to 1.5258--変数Xに1.5258を入れる
set X to X as integer--変数Xを型変換する
display dialog X--変数Xをダイアログに表示
=>2

変数1.5258を整数に型変換したため結果は2になりました。

set myNum to 1
set myStr to myNum as string

上の例は変数myNumを文字列に変換してmyStrに入れます。人間の見た
目には整数の1も文字列の"1"もあまり変わりませんが、AppleScriptからDTP
アプリケーションをコントロールするときなど良く使いわけます。

01)スクリプト編集プログラム

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

MacOSXでアプリケーション:AppleScriptの中のスクリプトエディタを起
動して下さい。起動したら下記のように入力します。

display dialog "こんにちは! "

ウィンドウ上部の実行ボタンをクリックしてください。"こんにちは! "と
ダイアログが現れたら大成功です。次はIllustratorを起動してください。ド
キュメントを作成してテキストオブジェクトを作り、適当に文字を入力して
おいてください。スクリプト編集プログラムに下記を入力して実行します。

tell application "Adobe Illustrator"
tell document 1
set contents of text frame 1 to "こんにちは! "
end tell
end tell

Illustratorのテキストが"こんにちは! "になれば成功です。"Adobe
Illustrator"を"Adobe InDesign CS2_J"に変えても動きます。
※実行ボタンをクリックするとInDesignはどこにあるか聞いてくるかもしれ
ません。その場合アプリケーションフォルダの中から選択してください。
どうでしょう。簡単なスクリプトですが大きな可能性に気がつくと思いま
す。このスクリプトを応用してやるとクライアントからもらったEXCELの原
稿をつぎつぎにIllustratorに流し込む事ができます。簡単ではないですが
AppleScriptが使えるようになれば現場で大きな効率化をはたすことができ
ます。ぜひチャレンジしてみてください。

00)AppleScriptとは

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

AppleScriptとはMacintoshのマクロ言語です。AppleScriptを使うと普段手
作業の仕事がプログラムから行う事ができます。例えば100個ファイルが
あったとき、ファイル名を1 〜100までの連番にしようとすると手作業でも
できますがたいへんだし不毛です。AppleScriptを使うとこれらの作業が一
気に行えますし正確です。さらにファイル数が1000でも10000でも問題あり
ません。
MacOSXでDTPの世界ではAdobeのアプリケーションがAppleScriptに対応
しています。(対応しているということはAppleScriptでコントロールできる
ということです。)ほかにも古くからQuarkXPressがAppleScriptに対応し我々
スクリプターがさまざまなツールを作ってきました。さらにFileMakerや
EXCELなども対応していますのでDB組版などにも利用できます。
AppleScriptによってInDesignやIllustratorの細かい作業を自動化すること
により作業効率が大幅にアップします。さらに突き詰めていくと自動組版が
可能になり。もっと突き詰めていくとWebブラウザからのリクエストをう
けて組版を開始することも可能です。さまざまな可能性を秘めたAppleScript
ですが最近は少し目立たない存在になっているような気がします。ソフト自
体どんどん便利になってきてAppleScriptで自動化しなくても大きく効率化
できるからか、それとも、いまだAppleScript未対応のIllustrator8で仕事をつ
づけているためかもしれません。
これを読んでAppleScript対応の魅力に気がついていただき、ぜひ
AppleScriptにチャレンジしてみてください。

IllustratorCS以上で
選択したオブジェクトにトンボをつけます
このスクリプトはフリーウェアです

下記はJavaScriptです。
tomboCS4.jsx.zip

こちらはAppleScript
tomboCS4.scpt.zip

ドロップされたファイルを処理

| コメント(0) | トラックバック(0)
on adding folder items to this_folder after receiving added_items
	try
		tell application "Finder"
			--↓フォルダアクションの設定されているフォルダ
			set the folder_name to the name of this_folder
		end tell
		--set the item_count to the number of items in the added_items
		--↓フォルダにドロップされたファイル
		repeat with myFile in added_items
			tell application "Finder"
				set the file_name to the name of myFile
			end tell
			display dialog folder_name & "内の" & file_name
		end repeat
	end try
end adding folder items to

設定方法

| コメント(0) | トラックバック(0)
作成したスクリプトは
Library=>Scripts=>Folder Action Scripts
に入れておく

スクリプトメニューから
Folder Actions=>Attach Script to Folder
を選ぶ

使用するスクリプトを選ぶ

フォルダアクションを設定したいフォルダを選ぶ

処理をストップする

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

タイムアウト

| コメント(0) | トラックバック(0)
with timeout of 1 second
処理
end timeout
tell application "Finder"
	set OriginalDelimiters to AppleScript's text item delimiters
	--現在のdelimiters設定を保存しておく。
	set AppleScript's text item delimiters to {tab}
	set PROSESS_LIST to (name of every application process)
	set TexData to PROSESS_LIST as string
	--すべての起動しているアプリケーションのリストをPROSESS_LISTへ
	set AppleScript's text item delimiters to OriginalDelimiters
	--必ずdelimiters設定を戻しておく。
	--display dialog TexData
end tell

ファイルのコピー

| コメント(0) | トラックバック(0)
set downFolder to choose folder with prompt "収集するフォルダを選択してください。"
tell application "Finder"
	duplicate alias "Macintosh HD:gazou:PAK2:001111" to downFolder with replacing
end tell

 with replacingで上書きコピー
 with replacingをつけないと上書きコピーされない
同じ名前のファイルがあるとエラーでストップ
Applications
=>AppleScript
=>Install Script Menu
をダブルクリックする。

アクセス権変更

| コメント(0) | トラックバック(0)
on run
	set curItem to choose folder with prompt "フォルダを選択"
	my chmod777(curItem)
end run

on open theList
	repeat with curItem in theList
		my chmod777(curItem)
	end repeat
end open

on chmod777(curItem)
	set myPath to POSIX path of curItem
	set cmdStr to "chmod -R 777 " & myPath
	do shell script cmdStr
end chmod777

commentにファイル名を書き込む

| コメント(0) | トラックバック(0)
on open inputList
	tell application "Finder"
		repeat with inputFile in inputList
			set comment of inputFile to name of inputFile
		end repeat
	end tell
end open

Fileタイプ変更のドロップレット

| コメント(0) | トラックバック(0)
on open inputList
	tell application "Finder"
		repeat with inputFile in inputList
			my getMyFolder(inputFile)
		end repeat
	end tell
end open

on run
	set myFolder to choose file with prompt "ファイルを選択してください"
	my getMyFolder(myFolder)
end run

on getMyFolder(myFolder)
	tell application "Finder"
		set creator type of myFolder to "ART5" --"YoED"
		--set file type of myFolder to "TEXT" --"YoED"
	end tell
end getMyFolder

--★Illustrator
--set creator type of myFolder to "ART5"
--set file type of myFolder to "TEXT"
--★QXP
--set creator type of myFolder to "XPRa"
--set file type of myFolder to "XDOC"
--★mi
--set creator type of myFolder to "MMKE"
--set file type of myFolder to "TEXT"
--★Excel
--set creator type of myFolder to "XCEL"
--set file type of myFolder to "XLS8"
--★Word
--set creator type of myFolder to "MSWD"
--set file type of myFolder to "W8BN"
--★REALbasic
--set creator type of myFolder to "RBv2"
--set file type of myFolder to "RbBF"
--★Application
--set file type of myFolder to "APPL"

フォルダ選択ダイアログ

| コメント(0) | トラックバック(0)
set myFol to choose folder with prompt "フォルダを選択" set myFol to myFol as string set myPage to 1 repeat with myFile in list folder myFol without invisibles set this_item to (myFol & myFile) as alias set FType to file type of (info for this_item) if FType is "EPSF" then my setEPS(myPage, myFol & myFile, 3) else end if set myPage to myPage + 1 end repeat on setEPS(myPage, inputFile, NURI) --ここに処理 end setEPS

クリップボード

| コメント(0) | トラックバック(0)
set the clipboard to "123"



set myTable to the clipboard
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

myStrをsep区切りでnum番目の項目を返す

delimitersを使った検索置換

| コメント(0) | トラックバック(0)
set OriginalDelimiters to AppleScript's text item delimiters
	
	set KEN to return --検索文字
	set TIKAN to "" --置換文字
	set AppleScript's text item delimiters to {KEN}
	set TexData to text items of TexData
	set AppleScript's text item delimiters to {TIKAN}
	set TexData to TexData as string
	
	set AppleScript's text item delimiters to OriginalDelimiters

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

countFields(?)区切りの要素数

| コメント(0) | トラックバック(0)
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

myStrをsep区切りで項目数を返す

SORT

| コメント(0) | トラックバック(0)
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

File入力

| コメント(0) | トラックバック(0)
open for access file FileName
read file FileName
set TexData to result
close access file FileNam

File出力

| コメント(0) | トラックバック(0)
open for access file FileName with write permission
write TexData to file FileName
close access file FileName

ASCII コードで指定

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

ASCII character (10)

ASCII number "A"

Movable Type 4 へようこそ!

| コメント(1)

このブログ記事は、Movable Type 4のインストール完了時に、システムによって自動的に作成されたブログ記事です。 新しくなったMT4の管理画面で、早速ブログを更新してみましょう。

カテゴリ

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