Please a little help with file rename script

Thank you for all the help in advance.

I am trying to write a script that will rename a pdf based on the pdf’s Title.

For example:

pdf name: 519-1.pdf
pdf title: 05-0006-omccerealheather.indd

new pdf name: 05-0006-omccerealheather.pdf

I do not have much yet, and I am already stuck. Here is what I have:

tell application “Finder”
try
set the_name to name of thisFile
tell application “Adobe Reader 7.0”
set doc_title to get info of thisFile key “Title” --I am getting an error message here!!! end of line expected.
end tell
set name of thisFile to doc_title as string
on error
– message
end try
end tell

Agian thank you for all the help.

Just a quick response (not fix) - Adobe Reader 7 is not scriptable so you can’t tell it anything - it won’t hear.

The way to approach this is to get the name of the saved file from the Finder.

How do I get the title of the file from the finder?

Thanks,
sam

I’m afraid Finder won’t be able to get a pdf document’s title for you, Sam. The data is embedded in the file itself - so we’ll have to try and extract it, one way or another.

Since many of my own pdf files don’t pack a title, the testing I’m able to do on this is somewhat limited. So our first task is to try and come up with an extraction method that works for you. I’ve cobbled together the following routine that should allow you to carry out your own tests on a range of typical target files:

to |get title| from pdf_alias
	set m to 2000
	tell (get eof pdf_alias) to if it < m then set m to it
	set t to read pdf_alias to m
	set d to text item delimiters
	set text item delimiters to "/Title ("
	if (count t's text items) is 1 then
		set text item delimiters to d
		error "Could not locate title of file."
	end if
	set t to text 1 thru -2 of paragraph 1 of t's text item 2
	set text item delimiters to d
	t
end |get title|

display dialog "Extracted title:" & return & "\"" & ¬
	(|get title| from choose file of type {"com.adobe.pdf"}) & "\"" buttons {"OK"} default button 1

With the above routine, all we’re checking for is whether a document’s title is accurately returned. If it’s not, perhaps you could come back with details of the results you’re getting - compared to what they should be. Until you’re satisfied with the extracted results, don’t try to use the routine below.

However, if the above works consistently with your files, then we can go on to change filenames:

to |get title| from pdf_alias
	set m to 2000
	tell (get eof pdf_alias) to if it < m then set m to it
	set t to read pdf_alias to m
	set d to text item delimiters
	set text item delimiters to "/Title ("
	if (count t's text items) is 1 then
		set text item delimiters to d
		error "Could not locate title of file."
	end if
	set t to text 1 thru -2 of paragraph 1 of t's text item 2
	set text item delimiters to d
	t
end |get title|

to |change extension| of t to new_extension
	considering case
		if "." is in t then
			set d to text item delimiters
			set text item delimiters to "."
			set t to t's text 1 thru text item -2
			set text item delimiters to d
		end if
	end considering
	t & new_extension
end |change extension|

to |change filename to title| from target_pdf
	set new_name to |change extension| of (|get title| from target_pdf) to ".pdf"
	tell application "Finder" to set target_pdf's name to new_name
end |change filename to title|

|change filename to title| from choose file of type {"com.adobe.pdf"}

Once, you’re happy with that, you may then want to consider running a similar routine on batches of files - rather than changing names individually. However, while that’s a fairly easy step on, let’s first make sure that we’re on the right track…
:slight_smile: