●メニューへ ●単福へ ●自動車の維持費をシミュレーション ●InDesign JavaScript ノート

01はじめに/AppleScriptでInDesignをコントロールする


InDesignもAppleScriptでかなりの効率化が可能です。
細かいところでは
書籍組の表の体裁をととのえるスクリプトや
エクセルから表の内容のコピペ
特殊なINDEXの作成から
本格的な自動組版までありとあらゆることができます。


TOPへ


application/ファイルを開く


on run {fileRef}
	set fileRef to fileRef as text --"DV HD:新商品 1"
	try
		tell application "InDesign 2.0J"
			open file fileRef
		end tell
	on error
		return "false"
	end try
	return "true"
end run


TOPへ


application/保存


on run {fileRef}
	set fileRef to fileRef as text --"DV HD:新商品 1"
	tell application "InDesign 2.0J"
		save document 1 to file fileRef
		close document 1
	end tell
	return "true"
end run


TOPへ


document/ルーラ原点


on run {}
	tell application "InDesign 2.0J"
		tell active document
			tell view preferences
				set ruler origin to page origin
				--spread origin --/page origin/spine origin
			end tell
		end tell
		
	end tell
	return "true"
end run


TOPへ


document/作成


	tell application "InDesign 2.0.2J"
		set myDocument to make document
		set properties of document preferences of myDocument to {pages per document:1, pages per spread:1, page width:"210 mm", page height:"297 mm"}
	end tell
	return "true"


TOPへ


guide/マスターの横ガイドを消す


tell application \"InDesign 2.0.2J\"
	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


TOPへ


guide/作成


tell application \"InDesign 2.0.2J\"
	set LayObj to layer \"写真\" of active document
	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


TOPへ


layer/レイヤーの名前変更・新規作成


on run {}
	tell application "InDesign 2.0J"
		tell active document
			try
				with timeout of 1 second
					if not (exists (layers whose name is "EPS")) then
						set name of layer 1 to "EPS"
					end if
					if not (exists (layers whose name is "IMG")) then
						make layer with properties {name:"IMG"}
					end if
					if not (exists (layers whose name is "Line")) then
						make layer with properties {name:"Line"}
					end if
					if not (exists (layers whose name is "RedLine")) then
						make layer with properties {name:"RedLine"}
					end if
					return "true"
				end timeout
			on error
				return "false"
			end try
		end tell
	end tell
	return "false"
end run


TOPへ


line/作成


on run {Blist, C, M, Y, K, Ws, pageNum, Lay, colName}
	set Blist to Blist as list
	set C to C as number
	set M to M as number
	set Y to Y as number
	set K to K as number
	set Ws to Ws as text
	set pageNum to pageNum as number
	set Lay to Lay as text
	set colName to colName as text
	(*set Blist to {{"12 mm", "10 mm", "109 mm", "10 mm"}, {"12 mm", "9.95 mm", "12 mm", "136.05 mm"}, {"12 mm", "136 mm", "60.5 mm", "136 mm"}, {"12 mm", "135.95 mm", "12 mm", "178.05 mm"}, {"12 mm", "178 mm", "109 mm", "178 mm"}, {"12 mm", "177.95 mm", "12 mm", "262.05 mm"}, {"12 mm", "262 mm", "109 mm", "262 mm"}, {"60.5 mm", "136 mm", "109 mm", "136 mm"}, {"60.5 mm", "135.95 mm", "60.5 mm", "178.05 mm"}, {"109 mm", "10 mm", "157.5 mm", "10 mm"}, {"109 mm", "9.95 mm", "109 mm", "52.05 mm"}, {"109 mm", "52 mm", "133.25 mm", "52 mm"}, {"109 mm", "51.95 mm", "109 mm", "94.05 mm"}, {"133.25 mm", "52 mm", "157.5 mm", "52 mm"}, {"133.25 mm", "51.95 mm", "133.25 mm", "94.05 mm"}, {"109 mm", "94 mm", "157.5 mm", "94 mm"}, {"109 mm", "93.95 mm", "109 mm", "136.05 mm"}, {"109 mm", "136 mm", "157.5 mm", "136 mm"}, {"109 mm", "135.95 mm", "109 mm", "178.05 mm"}, {"109 mm", "178 mm", "206 mm", "178 mm"}, {"109 mm", "177.95 mm", "109 mm", "262.05 mm"}, {"109 mm", "262 mm", "206 mm", "262 mm"}, {"157.5 mm", "10 mm", "206 mm", "10 mm"}, {"157.5 mm", "9.95 mm", "157.5 mm", "52.05 mm"}, {"157.5 mm", "52 mm", "206 mm", "52 mm"}, {"157.5 mm", "51.95 mm", "157.5 mm", "94.05 mm"}, {"157.5 mm", "94 mm", "206 mm", "94 mm"}, {"157.5 mm", "93.95 mm", "157.5 mm", "136.05 mm"}, {"157.5 mm", "136 mm", "206 mm", "136 mm"}, {"157.5 mm", "135.95 mm", "157.5 mm", "178.05 mm"}, {"206 mm", "10 mm", "400 mm", "10 mm"}, {"206 mm", "9.95 mm", "206 mm", "262.05 mm"}, {"206 mm", "262 mm", "400 mm", "262 mm"}, {"400 mm", "9.95 mm", "400 mm", "262.05 mm"}} as list

set C to 0 as number
set M to 100 as number
set Y to 100 as number
set K to 0 as number

set Lay to "Line" as text
set colName to "C0M100Y100K0"
set pageNum to 3 as text
set Ws to "0.5 mm" as text*)
	
	tell application "InDesign 2.0J"
		set LayObj to layer Lay of active document
		tell active document
			try
				set myCol to color colName
			on error
				set myCol to make color with properties {name:colName, color value:{C, M, Y, K}, color space:cmyk, color model:process}
			end try
		end tell
		tell page pageNum of active document
			repeat with buf in Blist
				make graphic line with properties {geometric bounds:buf, fill color:none, stroke color:myCol, stroke weight:Ws, item layer:LayObj}
			end repeat
		end tell
	end tell
	return "true"
end run


TOPへ


page/ページ数を調べる


on run {}
	tell application "InDesign 2.0J"
		tell active document
			set PageCount to count page
		end tell
	end tell
	return PageCount as text
end run


TOPへ


page/右ページか左ページか?


tell application "InDesign 2.0J"
	tell active document
		tell page 1
			set Leftpoint to item 2 of bounds
		end tell	end tell
	if Leftpoint > 0 then
		display dialog "右ページです。"
	else
		display dialog "左ページです。"
	end if
end tell


TOPへ


page/幅を調べる


on run {}
	tell application "InDesign 2.0J"
		tell active document
			tell page 1
				set pageW to item 4 of bounds
			end tell
		end tell
	end tell
	return pageW as text
end run


TOPへ


spread/pageの数を調べる


tell application "InDesign 2.0J"
	tell active document
		tell spread 2
			count page
			get name of page 1
		end tell
	end tell
end tell


TOPへ


spread/pageはどのspreadか調べる


set myList to "" as text
tell application "InDesign 2.0J"
	tell active document
		set spreadCount to count spread
		repeat with N from 1 to spreadCount			tell spread N
				set pageCount to count page
				repeat with T from 1 to pageCount
					set myList to myList & (N as text) & ","
				end repeat
			end tell
		end repeat
		display dialog myList
	end tell
end tell

なんでこんなことするかというと
Lineを作成するとき
0.5 mm以下の太さだと右ページに作ろうとしても
左ページに作られたりするので
spreadに作成しようというねらい。


TOPへ


spread/選択されたオブジェクトはどこのspreadか調べる


tell application "InDesign 2.0J"
	tell active document
		set selectedItems to selection
		if selectedItems is {} then
			return
		end if
		repeat with selItem in selectedItems
			if class of selItem is text frame then
				set myParent to parent of selItem
				if class of myParent is not spread then
					repeat until class of myParent is spread
						set myParent to parent of myParent
					end repeat
				end if
				set spreadId to id of myParent as text
				set frameId to id of selItem as text
				display dialog "spread id=" & spreadId & " frame id=" & frameId
			end if
		end repeat
	end tell
end tell


TOPへ


text frame/textをセット


on run {Pnum, setTXT}
	set Pnum to Pnum as number
	set setTXT to setTXT as text
	--set Pnum to 1 as number
	--set setTXT to "1が津" as text
	tell application "InDesign 2.0J"
		tell document 1
			tell page Pnum
				set countNum to count text frames
				repeat with N from 1 to countNum
					set cTXT to contents in text frame N
					if "月" is in cTXT then
						set contents in text frame N to setTXT
					end if
				end repeat
			end tell
		end tell
	end tell
	return "true"
end run


TOPへ


サンプル/EPSをページに配置


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)
	try
		set myPage to myPage as integer
		set inputFile to inputFile as string
		set NURI to NURI as integer
		set NURI2 to NURI * 2
		tell application "InDesign 2.0.2J"
			tell active document
				set myB to bounds of page 1
				set Y to -NURI
				set W to (item 4 of myB) + NURI2
				set H to (item 3 of myB) + NURI2
				tell spread 1
					set MHIRAKInum to count page
				end tell
				
				if MHIRAKInum = 2 then
					set SPnum to ((myPage + 1) div 2)
					if myPage mod 2 = 1 then
						set X to -NURI
					else
						set X to W - NURI
					end if
				else
					set SPnum to (myPage div 2) + 1
					if myPage mod 2 = 1 then
						set X to W - NURI
					else
						set X to 0 - NURI
					end if
				end if
				tell spread SPnum
					set EPSobj to place inputFile destination layer 1 --with properties {item layer:LayObj}
					tell parent of EPSobj --rectangle 1
						set geometric bounds to {Y, X, Y + H, X + W}
						tell EPSobj
							set Blist to geometric bounds
							set W2 to (item 4 of Blist) as real
							set H2 to (item 3 of Blist) as real
							set X2 to X + (W - W2) / 2
							set Y2 to Y + (H - H2) / 2
							set geometric bounds to {Y2, X2, Y2 + H2, X2 + W2}
						end tell
					end tell
				end tell
			end tell
		end tell
		return "true"
	on error
		return "false"
	end try
end setEPS


TOPへ


サンプル/トンボ作成


set lineW to 0.1
set mypt to 2.83
tell application "InDesign 2.0J"
	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" --"Registration" -- "レジストレーション"
		if number of selectedItems is 1 then
			set selItem to item 1 of selectedItems
			set myParent to parent of selItem
			set myBounds to geometric bounds of selItem
			set X to item 2 of myBounds --selAry[0].left/mypt
			set Y to item 1 of myBounds --selAry[0].top/mypt
			set W to (item 4 of myBounds) - (item 2 of myBounds) --selAry[0].width/mypt
			set H to (item 3 of myBounds) - (item 1 of myBounds) --selAry[0].height/mypt
			--左上
			my drowLine(X - 13, X - 3, Y, Y, lineW, mypt, myParent, myCol)
			my drowLine(X - 13, X, Y - 3, Y - 3, lineW, mypt, myParent, myCol)
			my drowLine(X - 3, X - 3, Y - 13, Y, lineW, mypt, myParent, myCol)
			my drowLine(X, X, Y - 13, Y - 3, lineW, mypt, myParent, myCol)
			--上センター
			my drowLine(X + W / 2, X + W / 2, Y - 13, Y - 3, lineW, mypt, myParent, myCol)
			my drowLine(X + W / 2 - 5, X + W / 2 + 5, Y - 7, Y - 7, lineW, mypt, myParent, myCol)
			my drowEllipse(X + W / 2 - 2, Y - 7 - 2, 4, 4, lineW, mypt, myParent, myCol)
			--右上
			my drowLine(X + W + 3, X + W + 13, Y, Y, lineW, mypt, myParent, myCol)
			my drowLine(X + W, X + W + 13, Y - 3, Y - 3, lineW, mypt, myParent, myCol)
			my drowLine(X + W + 3, X + W + 3, Y - 13, Y, lineW, mypt, myParent, myCol)
			my drowLine(X + W, X + W, Y - 13, Y - 3, lineW, mypt, myParent, myCol)
			--左センター
			my drowLine(X - 13, X - 3, Y + H / 2, Y + H / 2, lineW, mypt, myParent, myCol)
			my drowLine(X - 7, X - 7, Y + H / 2 - 5, Y + H / 2 + 5, lineW, mypt, myParent, myCol)
			my drowEllipse(X - 7 - 2, Y + H / 2 - 2, 4, 4, lineW, mypt, myParent, myCol)
			--右センター
			my drowLine(X + W + 3, X + W + 13, Y + H / 2, Y + H / 2, lineW, mypt, myParent, myCol)
			my drowLine(X + W + 7, X + W + 7, Y + H / 2 - 5, Y + H / 2 + 5, lineW, mypt, myParent, myCol)
			my drowEllipse(X + W + 7 - 2, Y + H / 2 - 2, 4, 4, lineW, mypt, myParent, myCol)
			--左下
			my drowLine(X - 13, X - 3, Y + H, Y + H, lineW, mypt, myParent, myCol)
			my drowLine(X - 13, X, Y + H + 3, Y + H + 3, lineW, mypt, myParent, myCol)
			my drowLine(X - 3, X - 3, Y + H + 13, Y + H, lineW, mypt, myParent, myCol)
			my drowLine(X, X, Y + H + 13, Y + H + 3, lineW, mypt, myParent, myCol)
			--下センター
			my drowLine(X + W / 2, X + W / 2, Y + H + 13, Y + H + 3, lineW, mypt, myParent, myCol)
			my drowLine(X + W / 2 - 5, X + W / 2 + 5, Y + H + 7, Y + H + 7, lineW, mypt, myParent, myCol)
			my drowEllipse(X + W / 2 - 2, Y + H + 7 - 2, 4, 4, lineW, mypt, myParent, myCol)
			--右上
			my drowLine(X + W + 3, X + W + 13, Y + H, Y + H, lineW, mypt, myParent, myCol)
			my drowLine(X + W, X + W + 13, Y + H + 3, Y + H + 3, lineW, mypt, myParent, myCol)
			my drowLine(X + W + 3, X + W + 3, Y + H + 13, Y + H, lineW, mypt, myParent, myCol)
			my drowLine(X + W, X + W, Y + H + 13, Y + H + 3, lineW, mypt, myParent, myCol)
		end if
	end tell
end tell

on drowLine(X1, X2, Y1, Y2, lineW, mypt, myParent, myCol)
	set buf to {Y1, X1, Y2, X2}
	tell application "InDesign 2.0J"
		tell myParent
			make graphic line with properties {geometric bounds:buf, fill color:none, stroke color:myCol, stroke weight:lineW * mypt, item layer:"TOMBO"}
		end tell
	end tell
end drowLine

on drowEllipse(X1, Y1, W1, H1, lineW, mypt, myParent, myCol)
	set buf to {Y1, X1, Y1 + H1, X1 + W1}
	tell application "InDesign 2.0J"
		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


TOPへ


ライブラリ/ドキュメントに配置する


tell application "InDesign CS_J"
	set objList to place asset asset "標準" of library 1 on document 1
	get class of item 1 of objList
end tell


TOPへ


画像/フィット


tell application "InDesign 2.0.2J"
	tell active document
		tell page 1
			tell rectangle 1
				fit given center content
			end tell
		end tell
	end tell
end tell


TOPへ


画像/回転


on run {pageNum, inputFile, X, Y, W, H, X2, Y2, Hscale, Wscale, ANG, Lay}
	set X to X as real
	set Y to Y as real
	set W to W as real
	set H to H as real
	set X2 to X2 as real
	set Y2 to Y2 as real
	set ANG to ANG as real
	set HS to Hscale as real
	set Ws to Wscale as real
	set pageNum to pageNum as number
	set inputFile to inputFile as text
	set Lay to Lay as text
	
	(*
set X to 5 as real
set Y to 5 as real
set W to 100 as real
set H to 60 as real
set X2 to -5 as real
set Y2 to -5 as real
set ANG to 5 as real
set HS to 110 as real
set Ws to 110 as real
set pageNum to 1 as number
set inputFile to "Macintosh HD:Desktop Folder:aa_takeuchi: Documents:0204_1_23:3:フリー3:フリー0301"
set Lay to "Line" as text
*)
	
	tell application "InDesign 2.0J"
		set LayObj to layer Lay of active document
		tell active document
			tell page pageNum
				set EPSobj to place inputFile destination layer LayObj --with properties {item layer:LayObj}
				tell parent of EPSobj --rectangle 1
					set geometric bounds to {Y, X, Y + H, X + W}
					tell EPSobj
						set Blist to geometric bounds
						set W2 to (item 4 of Blist) * HS / 100 as real
						set H2 to (item 3 of Blist) * Ws / 100 as real
						set Y2 to Y + Y2
						set X2 to X + X2
						set geometric bounds to {Y2, X2, Y2 + H2, X2 + W2}
						set rotation angle to ANG
					end tell
				end tell
			end tell
		end tell
	end tell
	return "true"
end run


TOPへ


画像/配置


on run {pageNum, inputFile, X, Y, W, H, X2, Y2, Lay}
	(*
set X to 5 as real
set Y to 5 as real
set W to 100 as real
set H to 60 as real
set X2 to -5 as real
set Y2 to -5 as real
set HS to 110 as real
set Ws to 110 as real
set pageNum to 1 as number
set inputFile to "Macintosh HD:Desktop Folder:aa_takeuchi: Documents:0204_1_23:3:フリー3:フリー0301"
set Lay to "Line" as text
*)
	
	set X to X as real
	set Y to Y as real
	set W to W as real
	set H to H as real
	set X2 to X2 as real
	set Y2 to Y2 as real
	set HS to Hscale as real
	set Ws to Wscale as real
	set pageNum to pageNum as number
	set inputFile to inputFile as text
	set Lay to Lay as text
	
	tell application "InDesign 2.0J"
		set LayObj to layer Lay of active document
		tell active document
			tell page pageNum
				place inputFile destination layer LayObj --with properties {item layer:LayObj}
				tell rectangle 1
					set geometric bounds to {Y, X, Y + H, X + W}
					tell EPS 1
						--set Blist to geometric bounds
						--set X2 to (item 2 of Blist) + X2
						--set Y2 to (item 1 of Blist) + Y2
						--set W2 to (item 4 of Blist) as real
						--set H2 to (item 3 of Blist) as real
						set item 1 of geometric bounds to Y2
						set item 2 of geometric bounds to X2
						--item 2 of geometric bounds:X2,
						--get properties
					end tell
				end tell
			end tell
		end tell
	end tell
	return "true"
end run


TOPへ


画像/配置2


set X to 10 as real
set Y to 5 as real
set W to 100 as real
set H to 60 as real
set X2 to -10 as real
set Y2 to -5 as real
set ANG to 0 as real
set HS to 90 as real
set Ws to 110 as real
set pageNum to 1 as number
set inputFile to "Macintosh HD:Desktop Folder:aa_takeuchi: Documents:0204_1_23:3:フリー3:フリー0301"
set Lay to "Line" as text


tell application "InDesign 2.0J"
	set LayObj to layer Lay of active document
	tell active document
		tell page pageNum
			set EPSobj to place inputFile destination layer LayObj 
			--with properties {item layer:LayObj}
			tell parent of EPSobj --rectangle 1
				set geometric bounds to {Y, X, Y + H, X + W}
				--外枠のbounds。Setするときは(Y,X,W,H)になる
				
				tell EPSobj
					set Blist to geometric bounds --(X,Y,H,W)でとれる
					set H2 to (item 3 of Blist) * HS / 100 as real
					set W2 to (item 4 of Blist) * Ws / 100 as real
					set Y2 to Y + Y2
					set X2 to X + X2
					set geometric bounds to {Y2, X2, Y2 + H2, X2 + W2} 
					--Setするときは(Y,X,W,H)になる???
					set rotation angle to ANG
				end tell
			end tell
		end tell
	end tell
end tell


TOPへ


画像/配置センターにそろえる


on run {pageNum, inputFile, X, Y, W, H, Lay}
	(*
set X to 5 as real
set Y to 5 as real
set W to 100 as real
set H to 60 as real
set pageNum to 1 as number
set inputFile to "Macintosh HD:Desktop Folder:aa_takeuchi: Documents:0204_1_23:3:フリー3:フリー0301"
*)
	set X to X as real
	set Y to Y as real
	set W to W as real
	set H to H as real
	set pageNum to pageNum as number
	set inputFile to inputFile as text
	set Lay to Lay as text
	tell application "InDesign 2.0J"
		set LayObj to layer Lay of active document
		tell active document
			tell page pageNum
				set EPSobj to place inputFile destination layer LayObj --with properties {item layer:LayObj}
				tell parent of EPSobj --rectangle 1
					set geometric bounds to {Y, X, Y + H, X + W}
					tell EPSobj
						set Blist to geometric bounds
						set W2 to (item 4 of Blist) as real
						set H2 to (item 3 of Blist) as real
						set X2 to X + (W - W2) / 2
						set Y2 to Y + (H - H2) / 2
						set geometric bounds to {Y2, X2, Y2 + H2, X2 + W2}
						--get properties
					end tell
				end tell
			end tell
		end tell
	end tell
	return "true"
end run


TOPへ


画像/配置画像の名前など


set pageNum to 1 as number
set BoxNum to 2 as number
tell application "InDesign 2.0J"
	tell active document
		tell page pageNum
			tell rectangle BoxNum
				--外枠のbounds
				set BoxBounds to geometric bounds
				tell EPS 1
					--配置画像のbounds
					set EPSbounds to geometric bounds
					--配置画像の回転
					set myAngle to rotation angle
					--配置画像のスケール
					get horizontal scale
					get vertical scale
					tell item link
						set EPSname to name--配置画像の名前
					end tell
				end tell
			end tell
		end tell
	end tell
end tell


TOPへ


画像/変倍


tell application "InDesign 2.0.2J"
	tell active document
		tell page 1
			tell rectangle 1
				--rectangleの領域を設定
				set geometric bounds to {Y, X, Y + H, X + W}
				tell EPS 1
					--拡大率設定
					scale horizontal scale myPer vertical scale myPer
				end tell
				--センターあわせに
				fit given center content
			end tell
		end tell
	end tell
end tell


TOPへ