0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Photoshopで開いている画像を、InDesignの貼り込みサイズに合わせてリサイズする・AppleScript版

Last updated at Posted at 2025-01-03

このAppleScript版です。

画像フレームに画像が足りなければカンバスサイズ変更で伸ばし、食い込んでいればそこにガイドを引きます。
※内容・フレームともに回転にはまだ対応していませんのでまともな結果にはなりません。


-- InDesign側でなく、実行側のPsでアラートを出すための関数
on PSMsg(DispMesg)
	tell application id "com.adobe.photoshop" to display alert DispMesg
end PSMsg

--PSドキュメントチェック
tell application id "com.adobe.photoshop"
	if (count of documents) = 0 then
		my PSMsg("画像が開かれていません")
		return
	end if
	set myFile to current document
	set myFilePath to file path of current document
end tell

tell application id "com.adobe.indesign"
	
	--IDドキュメントチェック
	if (count of documents) = 0 then
		my PSMsg("⚠️InDesignドキュメントが開かれていません")
		return
	end if
	
	-- 単位をmmに、定規をスプレッドに
	tell view preferences
		set idoriRuler to ruler origin
		set idoriHoriunits to horizontal measurement units
		set idoriVerunits to vertical measurement units
		set ruler origin to spread origin
		set horizontal measurement units to millimeters
		set vertical measurement units to millimeters
	end tell
	
	
	--リンク名が同じものをリストアップ。なければ終了
	set myLinkObj to active document's links whose file path = myFilePath
	if (count of myLinkObj) = 0 then
		my PSMsg("この画像は配置されていません")
		return
	end if
	
	--一番大きく配置したものを選択
	set myMaxScale to 0
	repeat with i in myLinkObj
		if status of i is normal then
			set myScale to absolute horizontal scale of parent of i
			if myScale > myMaxScale then
				set myMaxScale to myScale
				set myLargestLink to i
			end if
		end if
	end repeat
	
	--リンク内容の実効ppi(タテヨコ)、プレースホルダオブジェクトを取得
	set myLink to parent of myLargestLink
	set myLinkParent to parent of myLink
	set myPPI to effective ppi of myLink
	set a to item 1 of geometric bounds of myLink
	
	--内容の位置とプレースホルダの位置を計算(上・左・下・右)
	set myLinkBounds to {}
	repeat with i from 1 to 4
		set mL to item i of geometric bounds of myLink
		set mP to item i of geometric bounds of myLinkParent
		
		--タテヨコの実効PPIを取得しておく
		if i = 1 or i = 3 then
			set myPPIXY to item 1 of myPPI
		else
			set myPPIXY to item 2 of myPPI
		end if
		
		--上・左はホルダ-内容、下・右は内容-ホルダ
		if i  2 then
			set end of myLinkBounds to round (myPPIXY / 25.4 * (mP - mL))
		else
			set end of myLinkBounds to round (myPPIXY / 25.4 * (mL - mP))
		end if
	end repeat
end tell

tell application id "com.adobe.photoshop"
	--■ガイドを四辺に引く
	
	--単位をpxに(元を保存)
	set oriUnit to ruler units of settings
	set ruler units of settings to pixel units
	
	set myX to width of myFile
	set myY to height of myFile
	
	--伸ばす場合は元の画像位置、食い込む場合はタテヨコで計算
	set myGuide to {}
	repeat with i in myLinkBounds
		if i  0 then
			set end of myGuide to 0
		else
			set end of myGuide to i as number
		end if
	end repeat
	
	--ガイド位置設定
	set horizontalGuides to {item 1 of myGuide, myY - (item 3 of myGuide)}
	set verticalGuides to {item 2 of myGuide, myX - (item 4 of myGuide)}
	
	--jsxでガイド描画
	repeat with i in horizontalGuides
		do javascript "app.activeDocument.guides.add(Direction.HORIZONTAL, " & i & ");"
	end repeat
	repeat with i in verticalGuides
		do javascript "app.activeDocument.guides.add(Direction.VERTICAL, " & i & ");"
	end repeat
	
	--■画像をカンバスサイズで伸ばし
	
	-- マイナス値があるかどうかをテキスト化で判定
	if (myLinkBounds as string) contains "-" then
		
		--あとから選択範囲をとりやすいように、背景レイヤーなら通常レイヤーにする
		try
			set bgLayer to background layer of myFile
			set name of bgLayer to "背景"
		end try
		
		--食い込む場合は0、伸ばす場合は数値を残す
		set myCanvas to {}
		repeat with i in myLinkBounds
			if i  0 then
				set end of myCanvas to 0
			else
				set end of myCanvas to (i as number) * -1
			end if
		end repeat
		
		--jsxでカンバスサイズ変更
		do javascript "app.activeDocument.resizeCanvas(" & myX + (item 4 of myCanvas) & ", " & myY + (item 3 of myCanvas) & ", AnchorPosition.TOPLEFT);"
		do javascript "app.activeDocument.resizeCanvas(" & myX + (item 2 of myCanvas) + (item 4 of myCanvas) & ", " & myY + (item 1 of myCanvas) + (item 3 of myCanvas) & ", AnchorPosition.BOTTOMRIGHT);"
	end if
	
	--単位を元に戻す
	set ruler units of settings to oriUnit
end tell

--単位を元に戻す
tell application id "com.adobe.indesign"
	tell view preferences
		set ruler origin to idoriRuler
		set horizontal measurement units to idoriHoriunits
		set vertical measurement units to idoriVerunits
	end tell
end tell

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?