Pages: untitled pages document is saved despite command not to save.

Thanks for looking at this for me.

I am creating a new pages doc from a template.
a document “untitled.pages” appears.
i then substitute text for placeholder text in the doc.
i export a pdf copy of the modified pages document.
i close the original “untitled.pages” document without saving.

Error
The original pages document (thisDocument) does close but IS saved into iCloud. This means I have a slowly growing crop of “untitled.pages” documents in iCloud.I am not getting any error messages, it LOOKS as though it is working.

Thank you in advance for any ideas.

Max

set the testDoc to (path to desktop folder as text) & "deleteme.pdf"
try
	tell application "Pages"
		activate
		set thisDocument to make new document with properties {document template:template "code_inject"}
		
		-- need the following "patch" due to a bug in Pages.app. Selects all text and so I can replace placeholders
		tell application "System Events"
			tell process "Pages"
				key code 0 using command down
			end tell
		end tell
		
		tell application "Pages"
			tell thisDocument
				set (every placeholder text whose tag is "start") to "test"
			end tell
			
			export thisDocument to file testDoc as PDF -- this makes and saves the output file
			tell thisDocument
				close without saving
			end tell
			
		end tell
	end tell
end try

Which operating system are you running ?

Here, under 10.12.2, the export command fails.
My first guess was that the template which I created to match your needs was odd so I tried to export a standard file : error too.
As I used it more than 100 times last month with a script defining the target file as a «class furl» object I tested with this setting : error too.
Here is the last state of the test file.

set testDoc to ((path to desktop folder as text) & "deleteme.pdf") as «class furl»
try
	tell application "Pages"
		activate
		set thisDocument to make new document
		tell thisDocument
			set body text to "hello"
		end tell
		export document 1 to testDoc as PDF -- this makes and saves the output file
		tell thisDocument
			close without saving
		end tell
	end tell
end try

To be sure, I booted under 10.12.1 - alas, it’s on an external mechanical HD connected with USB 2 (no USB 3 on my iMac) so it was awfully long - but the answer was neat : under 10.12.1, export behaves flawlessly.

I wait for confirmation from other users before filing a report.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) vendredi 16 décembre 2016 18:50:24

Hello again Yvan

I am running,
macOS 10.12.1 (16B2555)
Kernel Version: Darwin 16.1.0
Applescript Version 2.9(191)

on a mid 2011 iMac 27 , using the internal boot drive for all file operations.

I just ran the script in the latest form that you sent.
It exports the .pdf file every time with no error.

In case I did not make it clear, I have had no problem with the export function, it’s trying to stop pages saving the “untitled.pages” file into icloud:pages every time.
Is this right script to close a pages document, in your opinion?

tell thisDocument
           close without saving
       end tell

Hello Headroom San

Thanks for the feedback. As you run 10.12.1 I’m not surprised that the export instruction does its duty flawlessly as it did more than 100 times last month on my machine.

In case I did not make it clear, I can’t test the behaviour of the close part because under 10.2.2 - at least on my machine - the export process fails so the close code isn’t triggered.

I must add that, I would certainly not get something store on iCloud because I don’t use this feature : I refuse to store my works in the cloud.

As I am curious, I enabled iCloud Drive then I ran the script which I posted after disabling the export instruction.
Nothing was saved in iCloud Drive.
Of course, after posting this message I will carefully disable iCloud Drive.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 17 décembre 2016 18:31:15

Hi guys,

Just in case this helps, or maybe it is helpful to add some extra testing to this, I have tested Yvans script (pasted again below for clarity).

First I checked in 10.12.1 and it worked as expected. I then updated my machine to 10.12.2 and I get an error when it exports to PDF:

set testDoc to ((path to desktop folder as text) & "deleteme.pdf") as «class furl»
try
   tell application "Pages"
       activate
       set thisDocument to make new document
       tell thisDocument
           set body text to "hello"
       end tell
       export document 1 to testDoc as PDF -- this makes and saves the output file
       tell thisDocument
           close without saving
       end tell
   end tell
end try

Thanks Adam

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) samedi 17 décembre 2016 20:33:02

Here is a workaround replacing the failing export instruction.
It gives us an extraneous feature : define the level of quality of the issued PDF.

--[SCRIPT]
use AppleScript version "2.4" # Yosemite or later OS
use framework "Foundation"
use scripting additions

set POSIXDestFolder to POSIX path of (path to desktop folder)
set PDFName to "deleteme.pdf"

tell application "Pages"
	activate
	considering numeric strings
		if its version < "6" then error "The script requires Pages version 6 or later."
	end considering
	set thisDocument to make new document
	tell thisDocument
		set body text to "hello"
	end tell
	--export document 1 in testDoc as PDF -- this makes and saves the output file
	
	my exportAsPDF(POSIXDestFolder, PDFName)
	close document 1 without saving
end tell

on exportAsPDF(POSIXDestFolder, PDFName)
	set {theApp, mt, ms} to {"Pages", 3, 1}
	set OSversion to (current application's NSProcessInfo's processInfo()'s operatingSystemVersion())
	--> {majorVersion:10, minorVersion:12, patchVersion:2} if macOS 10.12.2
	tell OSversion to set OSversion to (its majorversion as text) & "." & its minorversion
	activate application theApp
	tell application "System Events"
		set maybe to POSIXDestFolder & "/" & PDFName
		if exists disk item maybe then delete disk item maybe
		tell process theApp
			set frontmost to true
			# Trigger the menu item "PDF." of the menu item "Exportation vers" of menu "Fichier"
			tell menu bar 1
				get name of menu bar items
				(*{
01 - "Apple", 
02 - "Pages", 
03 - "Fichier", 
04 - "Édition", 
05 - "Insertion", 
06 - "Format", 
07 - "Disposition", 
08 - "Présentation", 
09 - "Partager",
10 - "Fenêtre", 
11 - "Aide"}
*)
				get name of menu bar item mt
				--> "Fichier"
				tell menu bar item mt to tell menu 1
					set theNames to get name of menu items
					(* {
01 - "Nouveau", 
02 - "Créer à partir de la liste de modèles.", # CAUTION: exists only if, since the installation of the app, the menu File was triggered once with alt depressed
03 - "Ouvrir.", 
04 - "Ouvrir un élément récent", 
05 - missing value, 
06 - "Fermer", 
07 - "Tout fermer", 
08 - "Enregistrer.", 
09 - "Enregistrer sous.", 
10 - "Dupliquer", 
11 - "Renommer.", 
12 - "Déplacer vers.", 
13 - "Revenir à ", 
14 - "Exportation vers", 
15 - missing value, 
16 - "Convertir en Mise en page", 
17 - missing value, 
18 - "Avancé", 
19 - missing value, 
20 - "Définir le mot de passe.", 
21 - missing value, 
22 - "Enregistrer comme modèle.", 
23 - missing value, 
24 - "Format d'impression.", 
25 - "Imprimer."}
*)
					set indx to 0
					repeat with aName in theNames
						set indx to indx + 1
						if contents of aName is missing value then exit repeat
					end repeat
					set mi to 9 + indx
					get name of menu item mi
					--> "Exportation vers"
					tell menu item mi to tell menu 1
						get name of menu items
						(* {
1 - "PDF.", 
2 - "Word.", 
3 - "Format texte.", 
4 - "ePub.", 
5 - "Pages '09." 
}*)
						get name of menu item ms
						--> "PDF."
						click menu item ms
					end tell -- menu item.
				end tell
			end tell -- menu bar.
			tell window 1
				repeat
					delay 0.01
					if exists sheet 1 then exit repeat
				end repeat
				tell sheet 1
					-- class of UI elements --> {group, static text, list, static text, static text, pop up button, checkbox, static text, button, button, button}
					-- name of buttons --> {"Annuler", "Suivant.", missing value}
					tell pop up button 1
						click it
						-- class of UI elements -->{menu}
						tell menu 1
							-- class of UI elements --> {menu item, menu item, menu item}
							-- name of menu items --> {"Bonne", "Supérieure", "Optimale"}
							# Define the wanted level of quality
							click menu item 3 --> "Optimale"
						end tell
					end tell # pop up button 1
					click button 2
				end tell # sheet
				delay 0.1
				tell sheet 1 # Save As dialog
					-- class of UI elements --> {button, button, button, group, UI element, text field, static text, static text, text field}
					-- name of buttons --> {"Exporter", "Nouveau dossier", "Annuler"}
					-- value of text fields --> {"Sans titre 4.pdf", ""}
					set value of text field 1 to PDFName
					# Open the dialog allowing to define the destination folder *)
					keystroke "g" using {command down, shift down}
					repeat until exists sheet 1
						tell me to delay 0.02
					end repeat
					tell sheet 1
						-- class of UI elements --> {static text, combo box, button, button}
						-- value of static text 1 --> "Aller au dossier :"
						# if 10.11 or higher
						--> {static text, combo box, button, button}
						# if 10.10.x
						--> {static text, text field, button, button}
						--|-- log "Point ox0090"
						(*
CAUTION the class of the field where the path must be inserted changed with 10.11 *)
						considering numeric strings
							if OSversion < "10.11" then
								set theTarget to text field 1
							else
								set theTarget to combo box 1
							end if
						end considering
						value of theTarget
						(*
Insert the destination folder *)
						set value of theTarget to POSIXDestFolder
						keystroke return # ditto click button 1 (ou Aller)
					end tell # sheet 1
					click button 1 # (Exporter)
				end tell # sheet 1
			end tell # window
		end tell # process
	end tell # System Events.
end exportAsPDF
--[/SCRIPT]

CAUTION, I just received a mail saying that on an user’s machine, the menu item 14 is not the title of a submenu.
I’m waiting for more infos about that.

I got it.
If, since the installation of the app, the menu File was triggered once with alt depressed there is one complementary menu item in the menu File. It’s commented in the edited script.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) dimanche 18 décembre 2016 13:58:53

The export bug is reported as :
report #29753839 “under 10.12.2 (and 10.12.3) exporting iWork doc thru AppleScript is killed”

Would be fine if some of you report it too.

Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) mardi 20 décembre 2016 16:19:00

Reported the issue on #29814795 “Export file from AppleScript broken on macOS 10.12.2”

Update on Jan. 5, 2017:
My radar has been marked as duplicate of yours!