Batch-convert ppt to pdf

Hi @ all

I’m trying to write a script to batch-convert all the ppts in a folder to pdf.

I found a script over here which does exactly that bit for pages documents. Unfortunately i cant bring it to work for PP-docs.

Here’s what I have so far:

set F to choose folder
tell application "Finder"
	set P to (files of entire contents of F)
	repeat with I from 1 to number of items in P
		set this_item to item 1 of P
		--if kind of this_item as string is "ppt" then
			tell application "Microsoft PowerPoint"
				activate
				open this_item
				tell application "System Events"
					tell process "Powerpoint"
						activate
						set frontmost to true
						click menu item "Export." of menu 1 of menu bar item "File" of menu bar 1
						click button "PDF" of radio group 1 of sheet 1 of window 1
						click button "Next..." of sheet 1 of window 1
						click button "Export" of sheet 1 of window 1
					end tell
				end tell
				close window 1 without saving
			end tell
		--end if
	end repeat
end tell

The basic problem is that nothing happens.

I alo tried to leave out the if-condition, still no result.

I tried to isolate the error and came to the guess that “set this_item to item 1 of P” returns an error, basically: “cannot read the file…”

Does anyone has an idea how to bring this thingy to work?

All the best

joh

Hi,

I don’t have PP 2008 with the export feature,
but I assume the class to open a file in PP should be an alias

try this


set F to choose folder
tell application "Finder" to set P to (files of entire contents of F whose name extension is "ppt")

repeat with oneFile in P
	tell application "Microsoft PowerPoint"
		activate
		open (oneFile as alias)
	end tell
	tell application "System Events"
		tell process "Powerpoint"
			click menu item "Export." of menu 1 of menu bar item "File" of menu bar 1
			click button "PDF" of radio group 1 of sheet 1 of window 1
			click button "Next..." of sheet 1 of window 1
			click button "Export" of sheet 1 of window 1
		end tell
	end tell
	tell application "Microsoft PowerPoint" to close window 1 without saving
end repeat

Notes: It’s not necessary to activate PP three times in every loop :wink:
The kind of the file is never “PPT” it’s better to filter the extension.
I always try to avoid nested application tell blocks

Hi iprecious,

An alternative to GUI scripting would be to install the free cups-pdf package on your Mac, set it as the default printer and execute code as follows (a slightly modified version of StefanK’s great script, tested on MS Office 2004):


set F to choose folder
tell application "Finder" to set P to (files of entire contents of F whose name extension is "ppt")

repeat with oneFile in P
	tell application "Microsoft PowerPoint"
		activate
		open (oneFile as alias)
		print out presentation 1
		close presentation 1 without saving
	end tell
end repeat

First of all: Thanks so much for the quick reply.

@Stefan:

I somehow tried to edit the script so that i works with the German version of PP. It even triggers the “Drucken…”-Dialog, but when it should hit the pdf-Button, I receive the error message System Events has received an error that sheet 1 of window 1 of process “Microsoft PowerPoint” can’t be read due to an invalid index.

I would try to tackle that problem, but I don’t really have a clue what AS wants to tell me.

@Martin: This one is a pretty good one, too. The only problem is that I still have to press the “Print” button. I tried to use the click button command from Stefans script, but i receive the same error message. And of course I still have no clue what AS wants to tell me.

If you have an idea: THANKS!!!

All the best

joh

Which version of Microsoft Office are you running? I am using MS Office 2004 and I do not have to hit the “Print” button when using the «print out» command.

Unfortuntely the 2008-version.

And I don’t have the 2004-version any more… :frowning:

maybe it’s a timing problem, the script should wait until the sheet is visible


.
tell application "System Events"
	tell process "Powerpoint"
		click menu item "Export." of menu 1 of menu bar item "File" of menu bar 1
		tell window 1
			repeat until exists sheet 1
				delay 0.5
			end repeat
			tell sheet 1
				click button "PDF" of radio group 1
				click button "Next..."
				click button "Export"
			end tell
		end tell
	end tell
end tell
.

Hmmmm…

I don’t think that a timing problem is the cause.

I tried to use the click button in various ways. NONE worked. I always get either the error message System Events recieved an Error: žbutton “Drucken” of process “Microsoft PowerPoint” cannot be read or the message I posted before.

Might it be a change in Leopard? Just a guess.

do you refer to the proper UI element?

a button must be an element at least of a window. Button “x” of process “y” is impossible

Ok, but in this case, your Script should do at least the clicking of hte pdf-button, because this one is defined properly, isn’t it?

Ho can I classify such a window?

the PDF button is a menu button, not just as button, this is a significant difference

take a look at Nigel’s Save as PDF “ An attempt at a general-purpose GUI method

GREAT!!!

Thanks for this link. That’s the stuff I need for a good start PLUS it should spare you from further questions.

At least for a day… :smiley:

Thanks again!!!

A wonderful good morning!

After another hour reading, trying, fixing I brought the Script to work.

set F to choose folder
tell application "Finder" to set P to (files of entire contents of F whose name extension is "ppt")

repeat with oneFile in P
	tell application "Microsoft PowerPoint"
		activate
		open (oneFile as alias)
		tell application "System Events"
			set process "Microsoft PowerPoint"'s frontmost to true
			tell application process "Microsoft PowerPoint"
				keystroke "p" using command down
				delay 1
				keystroke return
			end tell
		end tell
		close presentation 1 without saving
	end tell
	delay 5
end repeat

As you see, I choose the cups-pdf-variant and took a shortcut by using the keystroke-commands instead of the clicki-bits, but all the attempts using the latter entirely failed.

I got the idea with the keystrokes from Nigel’s little how-to.

I would have loved to bring the clickies to work, as for future-script that “might” be helpful, but for the moments I had to finish it as there are three exams next week.

Thanks to you two for helping me with this one!!!

All the best

joh

One last question: Does anyone know a comprehensie tutorial/manual for UI-Scripting?

I found an introductory one at the apple-page, but that’s that what I’m looking for.

Thanks and all the best

joh

iPrecious,

The script you wrote July 5th is printing the selected files, not converting them to PDF. For that, did you go with the GUI scripting route?

Yeap, I did:

set F to choose folder
tell application “Finder” to set P to (files of entire contents of F whose name extension is “ppt”)

repeat with oneFile in P
	tell application "Microsoft PowerPoint"
		activate
		open (oneFile as alias)
		tell application "System Events"
			set process "Microsoft PowerPoint"'s frontmost to true
			tell application process "Microsoft PowerPoint"
				keystroke "p" using command down
				delay 1
				keystroke return
			end tell
		end tell
		close presentation 1 without saving
	end tell
	delay 5
end repeat

All the best

joh

I recently got a large collection of song lyrics in PPT format from church (not PPTX). I wanted to export to TXT. Instead of using PowerPoint to open and close all the 1800 files, I used QuickLook to convert to HTML, and then to TXT.

If you want, I could modify the script to export HTML to PDF instead.

set this_folder_path to "/Users/peter/Desktop/KGCC PPT convert/"
set this_folder to this_folder_path as POSIX file

tell application "Finder"
	set these_files to every file of folder this_folder
end tell

repeat with this_file in these_files
	
	set this_file_path to POSIX path of (this_file as alias)
	
	set this_file_name to name of this_file
	
	do shell script "qlmanage -p \"" & this_file_path & "\" -o \"" & this_folder_path & "\""
	
	set this_file_name_no_extension to my replace_chars(this_file_name, ".ppt", "")
	set this_subfolder_name to this_file_name & ".qlpreview"
	
	tell application "Finder"
		
		repeat while (name of every folder of folder this_folder) does not contain this_subfolder_name
			delay 0.5
		end repeat
		
		set export_folder to (folder this_subfolder_name in folder this_folder) as alias
		set export_html_file to (file "Preview.html" in folder export_folder) as alias
		set export_html_path to POSIX path of export_html_file
		--set name of export_html_file to this_file_name_no_extension & ".html"
	end tell
	
	do shell script "textutil -convert txt \"" & export_html_path & "\" -output \"" & this_folder_path & this_file_name_no_extension & ".txt" & "\""
	
	tell application "Finder"
		delete export_folder
	end tell
	
end repeat



on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars


Model: MacBook Pro Retina
AppleScript: 2.5.1
Browser: Safari 537.78.2
Operating System: Mac OS X (10.9)