saving file via applescript with Pages 5.2

Is it possible to save a file via applescript with Pages 5.2? This script does not work:

tell application “Pages”
activate

set fileName to "testsave"

set thisDocument to ¬
	make new document with properties {document template:template "TestTemplate3"}
	end tell

# I haven't figured out a better way to position the cursor

set desktopFolder to path to desktop as text
save thisDocument in file (desktopFolder & fileName)

end tell

What is the meaning of your sentences :
This script does not work:
and

I haven’t figured out a better way to position the cursor ?

Here, it can’t be compiled because the instructions « tell application » and « end tell » aren’t balanced.

To compile it you must use :

tell application "Pages"
	activate
	
	set fileName to "testsave"
	
	set thisDocument to ¬
		make new document with properties {document template:template "TestTemplate3"}
	-- end tell # DISABLED
	
	# I haven't figured out a better way to position the cursor
	
	set desktopFolder to path to desktop as text
	save thisDocument in file (desktopFolder & fileName)
end tell

or

tell application "Pages"
	activate
	
	set fileName to "testsave"
	
	set thisDocument to ¬
		make new document with properties {document template:template "TestTemplate3"}
end tell

# I haven't figured out a better way to position the cursor

set desktopFolder to path to desktop as text

tell application "Pages" # ADDED
	save thisDocument in file (desktopFolder & fileName)
end tell

I would use a third one :

tell application "Pages"
	activate
	
	set fileName to "testsave"
	
	set thisDocument to ¬
		make new document with properties {document template:template "TestTemplate3"}

# I haven't figured out a better way to position the cursor

tell me to set desktopFolder to path to desktop as text

	save thisDocument in file (desktopFolder & fileName)
end tell

or even a fourth one :


 set desktopFolder to path to desktop as text
tell application "Pages"
	activate
	
	set fileName to "testsave"
	
	set thisDocument to ¬
		make new document with properties {document template:template "TestTemplate3"}

# I haven't figured out a better way to position the cursor

	save thisDocument in file (desktopFolder & fileName)
end tell

The instruction : set desktopFolder to path to desktop as text
can’t be encapsulated in a tell application . end tell block because path to is a function belonging to an OSAX (Standard Addition)

If we really need to leave it in such a block we must prefix it by « tell me to » as I did in my third code.

In fact, even third and fourth codes fail because for an obscure permissions problem, the application is not permitted to save a document on the Desktop.

As I had a folder named “theMainFolder” available on the Desktop I edited the script to store in it.

tell application "Pages"
	activate
	
	set fileName to "testsave"
	
	set thisDocument to ¬
		make new document with properties {document template:template "TestTemplate3"}
	
	# I haven't figured out a better way to position the cursor
	
	tell me to set desktopFolder to path to desktop as text
	
	save thisDocument in file (desktopFolder & "theMainFolder:" & fileName) # EDITED
end tell

And this time it behaved flawlessly.

I tried to store in an other predefined folder, the Documents one.
The application is permitted to store a document in it.

Yvan KOENIG (VALLAURIS, France) dimanche 27 avril 2014 10:51:13