Open 500+ files, save as text

I have a several folders with 500+ PDF files in each one. I want to create a loop where the script will loop through all of the files, open each PDF in Adobe Acrobat, and then do “File” - “Save as Text”. The new file name should be the same file name as the PDF, with .txt instead of .pdf.

I am having a problem getting the menu to do a “Save as Text…”. In fact, I can’t seem to even get any menu items to work. I am getting the following error - System Events got an error: NSReceiverEvaluationScriptError: 4.

Any help is greatly appreciated.

Thanks. Tony

Here is what I have so far:


set sourceFolder to choose folder with prompt "Source Folder?"

tell application "Finder"
	set fileNameList to every file of sourceFolder
end tell

repeat with aFile in fileNameList
	
	set destPath to sourceFolder as string
	
	tell application "System Events"
		tell process "Adobe Reader 7.0.8"
			open aFile
			delay 5
			tell menu bar 1
				
				tell menu "File"
					pick menu item "Save as Text..." of menu "File"
					
					-- ERROR on above line - System Events got an error: NSReceiverEvaluationScriptError: 4
					
				end tell
				
			end tell
		end tell
	end tell
	
	
	
end repeat


Tony,

I tried out your script and ran into a couple of problems, but I’m not sure if they’re because I’m on Panther and you might have Tiger. Nonetheless, I was able to get your script working with some minor changes. Here’s my script that works, based on yours:

tell application "Adobe Reader 7.0.7" to activate

set sourceFolder to choose folder with prompt "Source Folder?"

tell application "Finder"
	set fileNameList to every file of sourceFolder
	--set variable to the path to Adobe Reader for use later
	set readerApp to path to application "Adobe Reader 7.0.7"
end tell

repeat with aFile in fileNameList
	
	set destPath to sourceFolder as string
	--Use Finder to open the doc.  I had problems using process "Adobe Reader 7.0.8"
	--the documents kept opening in Preview instead of Adobe Reader.
	tell application "Finder" to open aFile using readerApp
	delay 5
	tell application "System Events"
		--I checked the running processes, and the Adobe Reader process is just "Adobe Reader"
		tell process "Adobe Reader"
			tell menu bar 1
				tell menu "File"
					--Just use select menu item "name", you don't need 'of menu "file"'
					--since it's the object of the TELL block
					select menu item "Save as Text..."
				end tell
			end tell
		end tell
	end tell
end repeat

I just tried it, and though it doesn’t give errors, I don’t see it saving anything, either. It should bring up a Save dialog, but I don’t see one coming up. I’ll work on it a bit more and post back.

Oh, it works, just change “select” back to “pick” (my bad).

You will still need to script the Save dialog if you want this to be completely automated.

OK, here’s one that will click the “Save” button so you can go on to the next file. Keep in mind, if you’re doing a lot of files, that you’re not closing the windows after you’re done with them, and they will pile up.

tell application "Adobe Reader 7.0.7"
	activate
	set sourceFolder to choose folder with prompt "Source Folder?"
end tell

tell application "Finder"
	set fileNameList to every file of sourceFolder
	--set variable to the path to Adobe Reader for use later
	set readerApp to path to application "Adobe Reader 7.0.7"
end tell

repeat with aFile in fileNameList
	
	set destPath to sourceFolder as string
	--Use Finder to open the doc.  I had problems using process "Adobe Reader 7.0.8"
	--the documents kept opening in Preview instead of Adobe Reader.
	tell application "Finder" to open aFile using readerApp
	delay 5
	tell application "System Events"
		--I checked the running processes, and the Adobe Reader process is just "Adobe Reader"
		tell process "Adobe Reader"
			tell menu bar 1
				tell menu "File"
					--Just use select menu item "name", you don't need 'of menu "file"'
					--since it's the object of the TELL block
					pick menu item "Save as Text..."
					delay 2
					
				end tell
			end tell
			click button "Save" of front window
			delay 5
		end tell
	end tell
end repeat


Kevin,

I really appreciate your help (I worked on this @#$%! for six hours last night), but I am still getting the error “System Events got an error: NSReceiverEvaluationScriptError: 4” on the line:

pick menu item “Save as Text…”

The script will open the file, and Adobe Acrobat will go to the front, but then I get the error…

I am using OS 10.4.6, so I don’t know if that is the problem.

Is Acrobat Reader even scriptable? Maybe that is the problem?

Thanks,

Tony

Tony,

Actually, Adobe Reader isn’t scriptable in the usual sense of the word. It doesn’t have a dictionary. But System Events doesn’t need an application that’s scritable. It was created for those times when you can’t get something done any other way. So I doubt that’s the problem.

I’m not sure if the issue because you have Tiger. Did you try my script, or just make the changes to yours? There were several changes in mine, I tried to note most of them in the comments, but if you didn’t try mine, do so and post back if it still doesn’t work.

I ran your script exactly as written below, with the only exception is that I changed the Acrobat Reader version.

When I ran your script, I received the error - System Events got an error: NSReceiverEvaluationScriptError: 4 - and the following line was highlighted:

pick menu item “Save as Text…”

I could try it with the full version of Acrobat, but with that program you have to click - “File” - “Save As…” - and then select “Text (Plain)” from a drop-down menu, and then press “Save” (see http://www.tonydarnell.com/AcrobatPro.jpg). But I don’t know how to re-create those steps either. I tried recording my steps in the Script Editor, but I haven’t been able to get it to record anything…

Is Acrobat Pro scriptable? How do you tell if an application is scriptable?

Thanks



tell application "Adobe Reader 7.0.8"
	activate
	set sourceFolder to choose folder with prompt "Source Folder?"
end tell

tell application "Finder"
	set fileNameList to every file of sourceFolder
	--set variable to the path to Adobe Reader for use later
	set readerApp to path to application "Adobe Reader 7.0.8"
end tell

repeat with aFile in fileNameList
	
	set destPath to sourceFolder as string
	--Use Finder to open the doc. I had problems using process "Adobe Reader 7.0.8"
	--the documents kept opening in Preview instead of Adobe Reader.
	tell application "Finder" to open aFile using readerApp
	delay 5
	tell application "System Events"
		--I checked the running processes, and the Adobe Reader process is just "Adobe Reader"
		tell process "Adobe Reader"
			tell menu bar 1
				tell menu "File"
					--Just use select menu item "name", you don't need 'of menu "file"'
					--since it's the object of the TELL block
					pick menu item "Save as Text..."
					delay 2
					
				end tell
			end tell
			click button "Save" of front window
			delay 5
		end tell
	end tell
end repeat



Not sure if Acrobat Pro is scriptable. If it is, when you go into Script Editor and select “Open Dictionary…” you should see it listed in the list of applications to choose from.

If it is scriptable, it should be quite scriptable, Adobe knows lots of professionals in graphics and newspapers use their products in automated workflows, so it should have a lot of functionality.

That said, I don’t know why the script doesn’t work on Tiger. I’m running Panther 10.3.9 and it works ok here. Maybe one of our Tiger-using members can take a look and see what’s up.

I have been looking at the dictionary commands for an hour or so, and searching the web for help. It appears that Acrobat Pro version 6 was scriptable, but I couldn’t get anything to work.

However, I just found out that Acrobat Professional allows for batch processing. So, now I am able to do what I want by just using Acrobat Professional. ARGH! (twelve hours down the drain).

So, while I am still learning AppleScript, I guess that we can forget trying to get the menu commands to work.

Next project - trying to script FileMaker Pro (which hopefully will be a bit easier).

I REALLY appreciate all of your help. I am glad that I found this BBS…

Thanks again.

Tony