Apple の脅威の通知と金銭目当てのスパイウェアへの対策について

しばらく返答が寄せられていないようです。 再度ディスカッションを開始するには、新たに質問してください。

.page や .key ファイルを一括して .docx や .pptx に変換するには?

Pages や Keynote で作成した多くの文書やプレゼンファイルがあるのですが、それらを一気に Microsoft Office ファイルに変換する方法・手順をお教え下さい。


Automator のライブラリには該当するような項目が無いようですが、Apple scriptなどを使えばできるのでしょうか?


よろしくお願い致します。

iMac (2017 – 2020)

投稿日 2023/10/03 16:52

返信
スレッドに付いたマーク ベストな回答

投稿日 2023/10/04 17:35

すみません。こちらでも事象を確認できました。

ファイルを正常に開けてなかった?ようです。

convert(dirName, fileName, appName, exportFormat, exportExtension)ハンドラ内の

「set doc to open fullPath」 を 「set doc to open (fullPath as alias)」に書き換えました。

これでどうでしょうか。


----------

tell application "Finder"

set aSel to the selection as alias list

end tell

if aSel = {} then return


repeat with theFile in aSel


tell application "Finder"

set fExt to (name extension of theFile) as string

set fName to (name of theFile) as string

set fDir to (folder of theFile) as string

end tell


if fExt is "pages" then

tell application "Finder"

if not (exists folder "docx" of (fDir as alias)) then make new folder at (fDir as alias) with properties {name:"docx"}

end tell

using terms from application "Pages"

convert(fDir, fName, "Pages", Microsoft Word, "docx") of me

end using terms from

end if


end repeat


on convert(dirName, fileName, appName, exportFormat, exportExtension)


tell application appName

set fullPath to (dirName & fileName)

set doc to open (fullPath as alias)

set docName to name of doc


set aRes to confirmationOfFileExisting(dirName & "docx:", docName, exportExtension) of me

set exportFileName to aRes as string


if appName is "Pages" then

tell application "Pages"

export doc to file exportFileName as exportFormat

end tell

end if


close doc


end tell


end convert


on confirmationOfFileExisting(dirName, docName, exportExtension)


tell application "Finder"

set exportFileName to (dirName & docName & "." & exportExtension) as string

if (exists file (exportFileName)) then

repeat with i from 2 to 65536

if not (exists file (dirName & docName & " " & i & "." & exportExtension)) then

set exportFileName to (dirName & docName & " " & i & "." & exportExtension)

exit repeat

end if

end repeat

end if

end tell


return exportFileName

end confirmationOfFileExisting

----------

返信: 18
スレッドに付いたマーク ベストな回答

2023/10/04 17:35 gensur への返信

すみません。こちらでも事象を確認できました。

ファイルを正常に開けてなかった?ようです。

convert(dirName, fileName, appName, exportFormat, exportExtension)ハンドラ内の

「set doc to open fullPath」 を 「set doc to open (fullPath as alias)」に書き換えました。

これでどうでしょうか。


----------

tell application "Finder"

set aSel to the selection as alias list

end tell

if aSel = {} then return


repeat with theFile in aSel


tell application "Finder"

set fExt to (name extension of theFile) as string

set fName to (name of theFile) as string

set fDir to (folder of theFile) as string

end tell


if fExt is "pages" then

tell application "Finder"

if not (exists folder "docx" of (fDir as alias)) then make new folder at (fDir as alias) with properties {name:"docx"}

end tell

using terms from application "Pages"

convert(fDir, fName, "Pages", Microsoft Word, "docx") of me

end using terms from

end if


end repeat


on convert(dirName, fileName, appName, exportFormat, exportExtension)


tell application appName

set fullPath to (dirName & fileName)

set doc to open (fullPath as alias)

set docName to name of doc


set aRes to confirmationOfFileExisting(dirName & "docx:", docName, exportExtension) of me

set exportFileName to aRes as string


if appName is "Pages" then

tell application "Pages"

export doc to file exportFileName as exportFormat

end tell

end if


close doc


end tell


end convert


on confirmationOfFileExisting(dirName, docName, exportExtension)


tell application "Finder"

set exportFileName to (dirName & docName & "." & exportExtension) as string

if (exists file (exportFileName)) then

repeat with i from 2 to 65536

if not (exists file (dirName & docName & " " & i & "." & exportExtension)) then

set exportFileName to (dirName & docName & " " & i & "." & exportExtension)

exit repeat

end if

end repeat

end if

end tell


return exportFileName

end confirmationOfFileExisting

----------

2023/10/03 17:12 gensur への返信

このあたりを参考にされてはいかがでしょうか?


pages2docx/pages2docx.applescript at master · gitmalong/pages2docx · GitHub

Keynote-to-Powerpoint/keynote to powerpoint.applescript at master · sumentse/Keynote-to-Powerpoint · GitHub


私は動作検証していませんので悪しからず。

2023/10/04 14:50 gensur への返信

anne3さんご提示の方法で問題解決につながったようで、もう必要ないかもしれませんが、

スクリプトちょっと修正?してみました。こんな感じでどうでしょうか。

最終的にどういう風に動かすかで実行動作が変わってくると思いますが、とりあえず動作を見るなら、

スクリプトエディタでスクリプトを立ち上げた状態で、Finderで変換したいファイルを選択状態に。

その状態でスクリプトを実行すると、同フォルダにサブフォルダができてその中に書き出されると思います。

オリジナルのコードはAutomatorでの動作を想定して作られたものかもしれませんね。


---pages2docx---

tell application "Finder"

set aSel to the selection as alias list

end tell

if aSel = {} then return


repeat with theFile in aSel


tell application "Finder"

set fExt to (name extension of theFile) as string

set fName to (name of theFile) as string

set fDir to (folder of theFile) as string

end tell


if fExt is "pages" then

tell application "Finder"

if not (exists folder "docx" of (fDir as alias)) then make new folder at (fDir as alias) with properties {name:"docx"}

end tell

using terms from application "Pages"

convert(fDir, fName, "Pages", Microsoft Word, "docx") of me

end using terms from

end if


end repeat


on convert(dirName, fileName, appName, exportFormat, exportExtension)


tell application appName

set fullPath to (dirName & fileName)

set doc to open fullPath

set docName to name of doc


set aRes to confirmationOfFileExisting(dirName & "docx:", docName, exportExtension) of me

set exportFileName to aRes as string


if appName is "Pages" then

tell application "Pages"

export doc to file exportFileName as exportFormat

end tell

end if


close doc


end tell


end convert


on confirmationOfFileExisting(dirName, docName, exportExtension)


tell application "Finder"

set exportFileName to (dirName & docName & "." & exportExtension) as string

if (exists file (exportFileName)) then

repeat with i from 2 to 65536

if not (exists file (dirName & docName & " " & i & "." & exportExtension)) then

set exportFileName to (dirName & docName & " " & i & "." & exportExtension)

exit repeat

end if

end repeat

end if

end tell


return exportFileName

end confirmationOfFileExisting

----------




---keynote to powerpoint---

on run


tell application "Finder"

set input to the selection as alias list

end tell

if input is {} then return


set pathList to {}

set n to 0

repeat with itemNum from 1 to count of input

tell application "System Events"

copy POSIX path of (container of (item itemNum of input)) to end of pathList

tell application "Finder" to set anExt to name extension of (item itemNum of input)

if anExt is "key" then set n to n + 1

end tell

end repeat

if n is 0 then return


set pathList to (item 1 of pathList as string) & "/"

set tempFolder to POSIX path of pathList & "PPTX"


--create folder to store powerpoint if folder does not exist

if not fileExists(tempFolder) then

do shell script "mkdir " & quoted form of POSIX path of tempFolder

end if


--the file exists

set the defaultDestinationFolder to POSIX file tempFolder as alias


tell application "Keynote"

activate

--repeat for each file

repeat with keynotefile in input

if name extension of (info for keynotefile) is "key" then

open keynotefile

try

if playing is true then tell the front document to stop


if not (exists document 1) then error number -128


tell front document

set documentName to its name

if documentName ends with ".key" then ¬

set documentName to text 1 thru -5 of documentName

set movieCount to the count of every movie of every slide

set audioClipCount to the count of every audio clip of every slide

end tell


set MicrosoftPowerPointFileExtension to "pptx"


tell application "Finder"

set newExportItemName to documentName & "." & MicrosoftPowerPointFileExtension

set incrementIndex to 1

repeat until not (exists document file newExportItemName of defaultDestinationFolder)

set newExportItemName to ¬

documentName & "-" & (incrementIndex as string) & "." & MicrosoftPowerPointFileExtension

set incrementIndex to incrementIndex + 1

end repeat

end tell

set the targetFileHFSPath to (defaultDestinationFolder as string) & newExportItemName

-- EXPORT THE DOCUMENT

with timeout of 1200 seconds

export front document to file targetFileHFSPath as Microsoft PowerPoint

end timeout


close front document without saving


on error errorMessage number errorNumber

display alert "EXPORT PROBLEM" message errorMessage

error number -128

end try

end if

end repeat


end tell


end run


on fileExists(posixPath)

return ((do shell script "if test -e " & quoted form of posixPath & "; then

echo 1;

else

echo 0;

fi") as integer) as boolean

end fileExists

----------

2023/10/04 16:40 vz.r への返信

vz.r 様

ありがとうございました。

まず前半を試してみましたが、上手く動作する時と動作しない時がありました。

動作しないときは、 Pages.app から 「現在 "nnnn.pages"を開けません。 その操作は許可されていません」、

スクリプトエディタから「スクリプトエラー  name of missing valueを取り出すことはできません。」 というエラーメッセージが出ました。

Pages で一度 nnnn.pages ファイルを開いて、閉じてから実行すると上手く動作しました。

2023/10/04 21:36 vz.r への返信

ありがとうございました。 pagesの変換は完璧に動作しました。 


前半のPages用のスクリプトの Pages を Keynote に、 docxをpptxに変更してみましたら、 .key ファイルを .pptx ファイルにコンバートできました。


これで変換は完璧にできるようになりました。ありがとうございました。



素人でよくわかりませんが、

後半のKeynote用スクリプトは、Automatorのフォルダアクションに入れるものなのでしょうか。スクリプトエディタで構文チェックすると、「“else” などがあるべきところですが“end tell”が見つかりました。」という構文エラーが出ます。

2023/10/05 08:59 vz.r への返信

vz.r 様 再度ご教示ありがとうございました。

色々試してみて、正常に動作することを確認しました。このスクリプトを使用させていただきます。


本題からは外れますが、今回わかったこと(上手くいかなかった理由)は、

・ Firefox や Chrome でこのページのスクリプトをコピーすると、なぜか全ての改行が二重になり、テキストが一行おきになってしまいます。

・その状態でスクリプトエディタにペーストしてコンパイルすると、上記エラーが出ます。

Safariでこのページからコピペすれば改行が二重にならず普通にペーストできて、スクリプトエディタでのコンパイルも正常にできました。


この現象が私の環境に特有のものなのか、レンダリングエンジンの問題なのかわかりません。もし理由がおわかりの方がおられましたらお教え願います。



2023/10/05 09:41 gensur への返信

Firefox や Chrome でこのページのスクリプトをコピーすると、なぜか全ての改行が二重になり、テキストが一行おきになってしまいます。


この現象が私の環境に特有のものなのか、レンダリングエンジンの問題なのかわかりません。もし理由がおわかりの方がおられましたらお教え願います。


Firefox と Chrome ではテキストが一行おきになってしまいます。これは当方でも同様でバグの類ではありません。


このボードの投稿エディタで「コードの挿入」を使って投稿されたテキストは Firefox でも Google Chrome でも正しくコピペできます。


tell application "Finder"
    activate
end tell


今回のように「コードの挿入」を使わずに投稿されたテキストは Safari でコピペするしかないと思います。


2023/10/05 09:46 gensur への返信

> Firefox や Chrome でこのページのスクリプトをコピーすると、なぜか全ての改行が二重


firefox、chromeの改行コードはwindows方式だからです。

標準では、改行コードは、unix系では0A、Windows系では0A0D、macでは0Dです。macでwindows系で作成されたテキストは0A0Dを2行の改行とします。普通のテキストなら改行が2行になるだけで、あまり問題になりませんが、スクリプトやプログラムコードの場合はこの違いのためにエラーになることはよくあります。

2023/10/05 11:27 gensur への返信

うまくいきましたか。良かったです。


コードの件、普段メインで使っているSafariでしか確認できてなかったです、すみません。

FirefoxやChromeだとただ単にペーストして書き込んだコードだとそうなってしまうんですね。

下記のように「¬」で後に続くところがあると改行されてしまうとうまくいきません。

今回の場合はここらで引っかかっていたものと思われます。


if documentName ends with ".key" then ¬
set documentName to text 1 thru -5 of documentName


コードの書き込みは、以後気をつけます。

2023/10/05 13:28 vz.r への返信

皆様ご教示ありがとうございました。

CR LF についてはうっすらと認識していましたが、今回の改行二重化と結びつけるまでに考えが至りませんでした。浅学を恥じます。


1行おきのコードでも、前半は上手くコンパイルできたのに、後半でできなかったのは「¬」で後に続くところがあったからなのですね。この部分を削除してみると上手くコンパイルできました。


余談ですが、今回まとめてコンバートしたかった理由は、

1. 退職の準備を進めています。これまで作成したプレゼンや文書を後任が(使うか使わないかはともかくとして)開きやすい汎用性のある形で残しておきたいと思います。

2.昔のAppleWorksの書類が見られなくて困る事があります。実用上困るというよりは開けなくて悔しいという程度の物が多いのですが。 PagesやKeynoteもいつサポートが打ち切られるかわからない、と心配してしまいます。Office形式にしておけば後々でもなんとかなるんじゃないかと思います。


Pagesの方は、 PDF にしておいても良いかもしれません。前半のスクリプトを少し改変してできないか試してみます。


2023/10/05 13:38 gensur への返信

CR LF についてはうっすらと認識していましたが、今回の改行二重化と結びつけるまでに考えが至りませんでした。


これは違うと思います。一行おきとなる問題の原因については説明が長くなりそうなのであえて書きませんでしたが、一応書いときますね。


マージンが指定されている段落内のテキストをコピーした際 Firefox / Google Chrome と Safari では改行コードの扱いが異なるのが一行おきになったりならなかったりする問題の原因です。


a1.html - Firefox、Google Chrome、Safari で一行おきとなる

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>title</title>
</head>
<body>
    <p>1行目</p><p>2行目</p>
</body>
</html>


a2.html - Firefox と Google Chrome では一行おきとなるが Safari ではそうならない

<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="utf-8">
    <title>title</title>
    <style type="text/css">p {margin:0;}</style>
</head>
<body>
    <p>1行目</p><p>2行目</p>
</body>
</html>


解決策は前掲のとおり「コードの挿入」を使うこと。


2023/10/05 14:22 gensur への返信

試されるとのことで以下余計かもしれませんが、必要箇所をちょっと書き換えるだけでPDFで書き出しできますよ。

よければお試しください。


以下コード

tell application "Finder"
	set aSel to the selection as alias list
end tell
if aSel = {} then return

repeat with theFile in aSel
	
	tell application "Finder"
		set fExt to (name extension of theFile) as string
		set fName to (name of theFile) as string
		set fDir to (folder of theFile) as string
	end tell
	
	if fExt is "pages" then
		tell application "Finder"
			if not (exists folder "pdf" of (fDir as alias)) then make new folder at (fDir as alias) with properties {name:"pdf"}
		end tell
		using terms from application "Pages"
			convert(fDir, fName, "Pages", PDF, "pdf") of me
		end using terms from
	end if
	
end repeat

on convert(dirName, fileName, appName, exportFormat, exportExtension)
	
	tell application appName
		set fullPath to (dirName & fileName)
		set doc to open (fullPath as alias)
		set docName to name of doc
		
		set aRes to confirmationOfFileExisting(dirName & "pdf:", docName, exportExtension) of me
		set exportFileName to aRes as string
		
		if appName is "Pages" then
			tell application "Pages"
				export doc to file exportFileName as exportFormat
			end tell
		end if
		
		close doc
		
	end tell
	
end convert

on confirmationOfFileExisting(dirName, docName, exportExtension)
	
	tell application "Finder"
		set exportFileName to (dirName & docName & "." & exportExtension) as string
		if (exists file (exportFileName)) then
			repeat with i from 2 to 65536
				if not (exists file (dirName & docName & " " & i & "." & exportExtension)) then
					set exportFileName to (dirName & docName & " " & i & "." & exportExtension)
					exit repeat
				end if
			end repeat
		end if
	end tell
	
	return exportFileName
end confirmationOfFileExisting


2023/10/04 21:43 gensur への返信

Pagesの方はちゃんと動きましたか。良かった!

Keynoteの方は構文で使われるものが違うので互換性がなく、流用はできません。

Keynoteの方のスクリプトもスクリプトエディタ上で動作しますよ。Automator用ではありません。

ちゃんと全文コピペしてますか?

こちらではここにアップしたものを、スクリプトエディタで構文チェック(コンパイル)しても構文エラーは出ません..



2023/10/03 18:17 gensur への返信

一時的にでもiCloud Driveに保存できるなら以下の方法でも変換できます。


1.ブラウザーでiCloudにアクセスする

2.Pages や Keynoteを開く

3.変換したいファイルを選択する(複数可)

4.ダウンロードボタンをクリックする

5.フォーマットを選択する(Wordなど)


スクリプトの方が使い勝手がいいかもしれませんがご参考まで。

2023/10/04 10:05 Pajerow への返信

Pajerow様

ご教示ありがとうございました。

Keynoteの方で試してみましたが、上手く動作しませんでした。サブフォルダ PPTX はできましたがその後の変換は動作しませんでした。


applescriptもautomatorもよく理解していないので、もう少し時間をかけて試してみたいと思います。

.page や .key ファイルを一括して .docx や .pptx に変換するには?

Apple サポートコミュニティへようこそ
Apple ユーザ同士でお使いの製品について助け合うフォーラムです。Apple ID を使ってご参加ください。