1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Keynote または Pages を PDF に一括エクスポートする AppleScript

Last updated at Posted at 2023-09-20

経緯

とあるユーチューバーさんの動画を見ていたら、
「大量の Keynote(または Pages)を、ひとつひとつ PDF に変換するのが大変
と仰っていた。そこで、全くの余計なお節介ではあるが、
特定のフォルダ内の Keynote または Pages を PDF に一括エクスポートする AppleScript」
を、ここに記しておく。

Keynote の場合

以下の AppleScript が「やけに長い」のは、「コメントアウトでスクリプトの説明」をしていて、「スクリプトの塊ごとに2行の空行」を入れていて、さらに「フォルダ選択するダイアログを表示」させるようにしつつ「その説明のダイアログや実行前後の確認ダイアログも表示」させて分かりやすくしているからである。従って、もっと短く書こうと思えば、「このスクリプトの核である tell application "Keynote" to repeat with… から始まる部分」を含めた7行程度のスクリプトとなる。これは後述の「Pages の場合」においても同様である。

AppleScript
-- 最前面で開いているアプリ名を取得
tell application "System Events" to set FrontmostApp to name of first process whose frontmost is true


-- 実行前に Keynote アプリを起動しておいてください
display dialog "このスクリプトの実行前に、あらかじめ Keynote アプリを起動しておいてください。

※ 初めて Keynote を起動する場合は、「続ける → プレゼンテーションを作成」の順にクリックしておいてください。

それだけで大丈夫です。Keynote アプリを起動しましたか?" with title "AppleScript 実行前の確認"


-- Keynote ファイルがあるフォルダのパスを指定
display dialog "まず、変換したい Keynote ファイルがあるフォルダを指定します。" with title "フォルダの指定 その1"
set InputFolder to POSIX path of (choose folder)


-- PDF ファイルを保存するフォルダのパスを指定
display dialog "次に、変換後の PDF ファイルを「保存するフォルダ」を指定します。

先ほどと同じフォルダを指定することもできます。" with title "フォルダの指定 その2"
set OutputFolder to POSIX path of (choose folder)


-- スクリプト実行前の確認
display dialog "最後の確認です。

指定したフォルダ内にある全ての Keynote を PDF に変換します。

このスクリプトを実行してもよろしいですか?" with title "最後の確認"


-- PDF ファイルを保存するフォルダを開いて実行状況を確認
tell application "Finder"
	activate
	open OutputFolder as POSIX file
end tell


-- フォルダ内の Keynote ファイルのリストを取得
tell application "System Events" to set KeynoteFileNameList to name of ((files in folder InputFolder) whose name extension is "key")


-- 各 Keynote ファイルを PDF にエクスポート
tell application "Keynote" to repeat with KeynoteFileName in KeynoteFileNameList
	
	-- 拡張子を除いたファイル名を取得
	set KeynoteFileBaseName to text 1 through -5 of KeynoteFileName
	
	-- Keynote ファイルを開いて PDF ファイルにエクスポートして閉じる
	set KeynoteFile to open (InputFolder & KeynoteFileName)
	export KeynoteFile to file ((OutputFolder & KeynoteFileBaseName & ".pdf") as POSIX file) as PDF
	close KeynoteFile
	
end repeat


-- スクリプト完了メッセージ
if FrontmostApp is "Script Editor" then tell application "Script Editor" to activate
display dialog "全ての Keynote が PDF に変換されました。お疲れさまでした。

それでは今日も1日 Apple を楽しみましょう!" buttons {"バイバイ!"} default button 1 with title "AppleScript 実行完了のお知らせ"
if FrontmostApp is "Script Editor" then tell application "Finder" to activate
return

Pages の場合

AppleScript
-- 最前面で開いているアプリ名を取得
tell application "System Events" to set FrontmostApp to name of first process whose frontmost is true


-- 実行前に Pages アプリを起動しておいてください
display dialog "このスクリプトの実行前に、あらかじめ Pages アプリを起動しておいてください。

※ 初めて Pages を起動する場合は、「続ける → 書類を作成」の順にクリックしておいてください。

それだけで大丈夫です。Pages アプリを起動しましたか?" with title "AppleScript 実行前の確認"


-- Pages ファイルがあるフォルダのパスを指定
display dialog "まず、変換したい Pages ファイルがあるフォルダを指定します。" with title "フォルダの指定 その1"
set InputFolder to POSIX path of (choose folder)


-- PDF ファイルを保存するフォルダのパスを指定
display dialog "次に、変換後の PDF ファイルを「保存するフォルダ」を指定します。

先ほどと同じフォルダを指定することもできます。" with title "フォルダの指定 その2"
set OutputFolder to POSIX path of (choose folder)


-- スクリプト実行前の確認
display dialog "最後の確認です。

指定したフォルダ内にある全ての Pages を PDF に変換します。

このスクリプトを実行してもよろしいですか?" with title "最後の確認"


-- PDF ファイルを保存するフォルダを開いて実行状況を確認
tell application "Finder"
	activate
	open OutputFolder as POSIX file
end tell


-- フォルダ内の Pages ファイルのリストを取得
tell application "System Events" to set PagesFileNameList to name of ((files in folder InputFolder) whose name extension is "pages")


-- 各 Pages ファイルを PDF にエクスポート
tell application "Pages" to repeat with PagesFileName in PagesFileNameList
	
	-- 拡張子を除いたファイル名を取得
	set PagesFileBaseName to text 1 through -7 of PagesFileName
	
	-- Pages ファイルを開いて PDF ファイルにエクスポートして閉じる
	set PagesFile to open (InputFolder & PagesFileName)
	export PagesFile to file ((OutputFolder & PagesFileBaseName & ".pdf") as POSIX file) as PDF
	close PagesFile
	
end repeat


-- スクリプト完了メッセージ
if FrontmostApp is "Script Editor" then tell application "Script Editor" to activate
display dialog "全ての Pages が PDF に変換されました。お疲れさまでした。

それでは今日も1日 Apple を楽しみましょう!" buttons {"バイバイ!"} default button 1 with title "AppleScript 実行完了のお知らせ"
if FrontmostApp is "Script Editor" then tell application "Finder" to activate
return

もっと短いスクリプト

念のため、より短いスクリプトも記しておく。確認ダイヤログ等は一切表示されないし、変換後の PDF は Keynote(または Pages)と同じフォルダに保存される。

Keynote の場合

AppleScript
set TargetFolder to POSIX path of (choose folder)
tell application "System Events" to set KeynoteFileNameList to name of ((files in folder TargetFolder) whose name extension is "key")
tell application "Keynote" to repeat with KeynoteFileName in KeynoteFileNameList
	set KeynoteFile to open (TargetFolder & KeynoteFileName)
	export KeynoteFile to file ((TargetFolder & (text 1 through -5 of KeynoteFileName) & ".pdf") as POSIX file) as PDF
	close KeynoteFile
end repeat

Pages の場合

AppleScript
set TargetFolder to POSIX path of (choose folder)
tell application "System Events" to set PagesFileNameList to name of ((files in folder TargetFolder) whose name extension is "pages")
tell application "Pages" to repeat with PagesFileName in PagesFileNameList
	set PagesFile to open (TargetFolder & PagesFileName)
	export PagesFile to file ((TargetFolder & (text 1 through -7 of PagesFileName) & ".pdf") as POSIX file) as PDF
	close PagesFile
end repeat

変換後の全 PDF を結合する方法

また、こうも仰っていた。「変換した PDF を結合するのも大変」と。
そこで、(これはご存じかもしれないが)複数の PDF を結合する、恐らく最も簡単な方法も記載しておく。

  1. Finder で、結合したい PDF が保存されているフォルダを開く
  2. 結合したい PDF を全て選択する
  3. 選択した PDF を右クリック → クイックアクション → PDF を作成

これで選択した全ての PDF が名前順に結合される。かなりの時間短縮になるのではなかろうか。

あとがき

以上が「Keynote または Pages を PDF に一括エクスポートする AppleScript」である。
この記事を読んだあなたのお役に立てば幸いである。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?