Saving a Renamed File

I have been trying to write a Script that will allow the operator to change the name of the file and the location to save it to. However I cannot work out how to then save the renamed file I get this error.

error “PDFpenPro got an error: Can’t make "Gilliland 042018.pdf" into type specifier.” number -1700 from “Gilliland 042018.pdf” to specifier

Thisi is my script so far

tell application "PDFpenPro"
	activate
	get name of front window of application "PDFpenPro"
	set frontwin to name of front window of application "PDFpenPro"
	display dialog "Name of this file is " & frontwin & " Change it" buttons {"yes", "No"}
	if button returned of result = "YES" then
		display dialog "Enter Name of file" default answer ""
		set frontwin to text returned of result & ".pdf" as alias
		set name of front window of application "PDFpenPro" to frontwin
	end if
	tell application "Finder"
		if exists Finder window 1 then
			set currentDir to target of Finder window 1 as alias
		end if
	end tell
	display dialog "Current path to Save this file is  " & currentDir & " Change it" buttons {"yes", "No"}
	if button returned of result = "NO" then
		set saveto to currentDir
	else
		set saveto to choose folder with prompt "Select File Location:"
	end if
	display dialog saveto as string
	--save frontwin in saveto as file --NOT WORKING
end tell

The line above that is commented out is the problem. I have tried " as PDF", “as Alias” and of course “as File” because the issue is type specifier, any thoughts greatly appreciated

Model: MacbookPro
AppleScript: 2.10
Browser: Safari 11.0.3 604.5.6
Operating System: Mac OS X (10.13 Developer Beta 3)

When you try to save,frontwin is an alias and saveto is an alias too.

What would you get using :

save frontwin in saveto

Yvan KOENIG running High Sierra 10.13.5 in French (VALLAURIS, France) dimanche 3 juin 2018 09:33:02

Hi.

You’re thinking in terms of saving a file to a folder. The ‘save’ command in most applications saves a document to a file. So in your ‘save’ line, ‘frontwin’ needs to be a reference to the front document in PDFpenPro and ‘saveto’ a file reference to the destination file. The file will usually be created during the save if it doesn’t already exist. In pseudocode:

tell application "PDFpenPro"
	-- Blah blah blah.

	save document 1 in file ("full:path:to:an:existing:folder:" & "file name.pdf")
end tell

That’s the general principle. But I don’t have PDFpenPro.

An AppleScript command you may like to check out is ‘choose file name’, in the StandardAdditions. It presents a normal ‘save’ dialog and returns a file reference to the destination the user enters. You can then use this reference as the ‘in’ parameter value in PDFpenPro’s ‘save’ command.

Yvan

thanks you but that left me with a type specifier problem

Nigel

Thank you very much that was the trick for some reason I had to concatenate path and file name before " Save document 1 etc" .

Anyway now working after countless hours trying to figure it out for myself.

Thanks again

Just in case it is of use to anyone else this is the working script.

tell application "PDFpenPro"
	activate
	set frontwin to name of front window of application "PDFpenPro"
	display dialog "Name of this file is " & frontwin & " Change it" buttons {"yes", "No"}
	if button returned of result = "YES" then
		display dialog "Enter Name of file" default answer ""
		set frontwin to text returned of result & ".pdf"
		set name of front window of application "PDFpenPro" to frontwin
	end if
	tell application "Finder"
		if exists Finder window 1 then
			set currentDir to target of Finder window 1 as alias
		end if
	end tell
	display dialog "Current path to Save this file is  " & currentDir & " Change it" buttons {"yes", "No"}
	if button returned of result = "NO" then
		set saveto to currentDir
	else
		set saveto to choose folder with prompt "Select File Location:"
	end if
	set saveto to saveto & frontwin as string
	save document 1 in file saveto
end tell

Hi MitchBVI.

Just to point out that if there are no Finder windows open, ‘currentDir’ won’t be set, which will cause an error in the following ‘display dialog’ line. You could perhaps rearrange the second half of the script like this:

tell application "PDFpenPro"
	-- First part of script up to renaming of window, then:
	
	tell application "Finder" -- This shouldn't really be inside the PDFpenPro 'tell' block.
		if exists Finder window 1 then
			set currentDir to target of Finder window 1 as text
		else
			set currentDir to missing value
		end if
	end tell
	if (currentDir's class is text) then
		display dialog "Current path to Save this file is " & currentDir & " Change it" buttons {"yes", "No"}
		if (button returned of result = "yes") then set currentDir to missing value
	end if
	if (currentDir is missing value) then
		set saveto to ((choose folder with prompt "Select File Location:") as text) & frontwin
	else
		set saveto to currentDir & frontwin
	end if
	save document 1 in file saveto
end tell

Or, if you were to use my ‘choose file name’ suggestion, the whole script might be something like this:

tell application "Finder"
	if (Finder window 1 exists) then
		set currentDir to target of Finder window 1 as alias
	else
		set currentDir to missing value
	end if
end tell

tell application "PDFpenPro"
	activate
	set frontwin to name of front window
	if (frontwin does not end with ".pdf") then set frontwin to frontwin & ".pdf"
	
	-- Use a standard "Save As…" dialog to get the user's input for the name and location of the destination file. The result's either a file reference or a "User canceled" error, depending on which button's clicked.
	if (currentDir is missing value) then
		set saveto to (choose file name with prompt "Save the front document as:" default name frontwin)
	else
		set saveto to (choose file name with prompt "Save the front document as:" default location currentDir default name frontwin)
	end if
	
	-- Save the document under the new name (if different) at the new location. If the name's different from the original, the name of the document window will probably change to match.
	save document 1 in saveto -- 'saveto' is already a file reference here, so no 'file' in front of it.
end tell