見出し画像

【DTP作業効率化】PDFを任意の解像度のJPG画像にするAppleScript

SNS等による情報発信、データによる業務の管理等、1つのデータが様々に使われる今日。印刷用に制作したデータも下版用データ以外に様々な形で求められることがあります。
下版用PDFを、350dpi、72dpiでJPG化したいという状況があり制作したApple Script。

ドラッグ&ドロップしたPDFをPhotoshopでJPGにします。
その際、ダイアログでどのような解像度にするか決定できます。
「350dpiと72dpi」、「350dpi」、「72dpi」、「解像度を入力」の選択肢があり、「解像度を入力」を選んだときは任意の解像度を入力する。「300/150/72」とすれば300dpi、150dpi、72dpiのJPGが作られる。
複数ページあるPDFはすべてのページが処理される。

JPGには解像度が350dpi以外の場合、「_XXXdpi」という形でファイル名に記入される。

PDFのページ数を取得する箇所についてはどちらかのページでお知恵を拝借しています。どこかは忘れてしまいました。感謝。

下記のコードを「スクリプトエディタ」にコピペの後、アプリケーション形式で保存。変換したいPDFをドロップ&ドラッグで使えます。

on run
	display dialog "PDFをドロップしてください。" & return & "PDFをJPGにします。\n解像度は「350dpiと72dpi」「350dpi」「72dpi」から選べます。また400までの整数を入力することもできます。(複数入力可)" buttons {"OK"} default button 1
end run


on open the_PDFs
	with timeout of 86400 seconds
		set issueList to {"350dpiと72dpi", "350dpi", "72dpi", "解像度を入力"}
		set selectedIssue to (choose from list issueList with prompt "ドロップしたPDFでJPGを作ります。" default items {"OK"} cancel button name {"Cancel"})
		if selectedIssue is false then
			return
		end if
		
		
		if selectedIssue is {"解像度を入力"} then
			repeat
				display dialog "解像度を入力して下さい。" & return & "※複数入力する場合は「/」で区切る。" default answer "400以下の整数を入力してください。"
				set the_resolutions to text returned of result
				set the_resolutions to my divide_text(the_resolutions, "/")
				
				set the_boolean to true
				repeat with a in the_resolutions
					try
						set a to a as number
					end try
					if the class of a is integer and a is less than 401 then
						set the_boolean to false
					end if
				end repeat
				if the_boolean is false then exit repeat
			end repeat
			
		else if selectedIssue is {"350dpi"} then
			set the_resolutions to {"350"}
		else if selectedIssue is {"72dpi"} then
			set the_resolutions to {"72"}
		else if selectedIssue is {"350dpiと72dpi"} then
			set the_resolutions to {"350", "72"}
		end if
		set the_resolutions to bubblesort(the_resolutions)
		
		repeat with afile in the_PDFs
			tell application "Finder"
				set the_destination to the container of afile as alias
			end tell
			set num_of_Pages to my GetPDFNoPages(afile)
			set the_count to 0
			
			
			repeat num_of_Pages times
				set jpg_350 to {}
				set the_count to the_count + 1
				set the_resolution to item 1 of the_resolutions
				tell application "Adobe Photoshop 2022"
					open afile as PDF with options {class:PDF open options, page:the_count as number, crop page:art box, mode:RGB, resolution:the_resolution}
				end tell
				repeat with the_resolution in the_resolutions
					set the_destination_posix to (the POSIX path of the_destination)
					tell application "Adobe Photoshop 2022"
						resize image current document resolution the_resolution resample method bicubic
						save current document in the_destination_posix as JPEG with options {class:JPEG save options, embed color profile:true, quality:12} without copying
					end tell
					tell application "Finder"
						set afile_name to name of afile
						set ne to name extension of afile
						set offset_name to text 1 thru ((offset of ("." & ne) in afile_name) - 1) of afile_name
						if num_of_Pages is 1 then
							set the_JPG to (the_destination & offset_name & ".jpg") as string as alias
							set name of the_JPG to offset_name & "_" & the_resolution & "dpi.jpg"
							if the_resolution as string is "350" then
								set the_JPG to (the_destination & offset_name & "_" & the_resolution & "dpi.jpg") as string as alias
								set the end of jpg_350 to the_JPG as alias
							end if
						else
							set the_JPG to (the_destination & offset_name & "-" & the_count & ".jpg") as string as alias
							set name of the_JPG to offset_name & "-" & the_count & "_" & the_resolution & "dpi.jpg"
							if the_resolution as string is "350" then
								set the_JPG to (the_destination & offset_name & "-" & the_count & "_" & the_resolution & "dpi.jpg") as string as alias
								set the end of jpg_350 to the_JPG as alias
							end if
						end if
					end tell
				end repeat
				if jpg_350 is not {} then
					tell application "Finder"
						repeat with a_file in jpg_350
							set its_name to name of a_file
							set ne to name extension of a_file
							set offset_name to text 1 thru ((offset of ("_350dpi." & ne) in its_name) - 1) of its_name
							set name of a_file to offset_name & ".jpg"
						end repeat
					end tell
				end if
				tell application "Adobe Photoshop 2022" to close (close the current document without saving)
			end repeat
		end repeat
		
		
		activate
		display dialog "JPEGができました。" buttons {"OK"} default button 1
		if button returned of result is "OK" then
			return
		end if
	end timeout
end open

-------------------------------
on GetPDFNoPages(MyPDF)
	set This_PDF to quoted form of POSIX path of (MyPDF as text)
	set PDF_Pages to do shell script "/usr/bin/mdls -name kMDItemNumberOfPages" & space & This_PDF
	set PDF_Pages to (GetRoot(PDF_Pages, 2, "kMDItemNumberOfPages = ")) as number
	return PDF_Pages
end GetPDFNoPages

on GetRoot(MyAlias, N, SP)
	set AppleScript's text item delimiters to SP
	set MyRoot to item N of (text items of (MyAlias as text))
	set AppleScript's text item delimiters to ""
	return MyRoot
end GetRoot
------------------------
on bubblesort(the_list)
	set is_sorted to false
	repeat until is_sorted
		set is_sorted to true
		repeat with i from 1 to (length of the_list) - 1
			set item_i to item i of the_list as integer
			set item_iplus1 to item (i + 1) of the_list as integer
			if item_i < item_iplus1 then
				set {item i of the_list, item (i + 1) of the_list} to {item (i + 1) of the_list, item i of the_list}
				set is_sorted to false
			end if
		end repeat
	end repeat
	return the_list
end bubblesort
------------------------
to divide_text(the_text, the_delimiter)
	set the_original_delimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to the_delimiter
	set these_items to text items of the_text
	set AppleScript's text item delimiters to the_original_delimiters
	return these_items
end divide_text

解説

このAppleScriptは、ドロップしたPDFファイルをJPGに変換するスクリプトです。以下はスクリプトの主要な部分とその説明です。

  1. このスクリプトはPDFファイルがドロップされたときに実行されます:

    • 解像度の選択肢を含むダイアログボックスを表示します。

    • ユーザーが選択した解像度に応じて、変換を行います。解像度を入力するオプションも提供されています。

    • Adobe Photoshop アプリケーションを使用してPDFをJPGに変換し、指定した解像度で保存します。

  2. on GetPDFNoPages(MyPDF) ハンドラ:

    • このハンドラは指定されたPDFファイルのページ数を取得します。mdls コマンドを使用してページ数を抽出します。

  3. on GetRoot(MyAlias, N, SP) ハンドラ:

    • このハンドラは、指定された文字列から指定された区切り文字でテキストアイテムを取得します。mdls の出力から必要な情報を取得するのに使用されます。

  4. on bubblesort(the_list) ハンドラ:

    • このハンドラは、リストを降順にソートするためのバブルソートアルゴリズムを実装しています。

この記事が気に入ったらサポートをしてみませんか?