batch update Pages docs

@ Yvan Koenig

Hi, I’m using your script “batch Pages’08 2 Pages’09” to update Pages docs. It has suddenly stopped working. It created new Pages09 documents from a folder earlier, now it doesn’t work on a similar folder of documents, and I can’t reproduce the success of a copy of the previous folder.

Any idea ???

I’m on MacOSx10.13.6. I have Pages 09 & Pages v8.0 on my system.

Thanks

AppleScript: 2.10
Browser: Safari 605.1.15
Operating System: macOS 10.13

May you post the script in the version that you are using?
This very old script received numerous changes and I can’t guess which are applied on your machine.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 1 février 2021 07:59:01

Here’s the code… Thanks



--[SCRIPT batch_Pages'08_2_Pages'09.app]
(*
Save the script as an Application Bundle (Application under Snow Leopard).
Run it or drag and drop a folder icon on its icon.

It will open the passed documents and save them as Pages '09 ones in the subfolder [09] of the source folder.

This scheme is required because Pages refuse to save upon the existing file.

Yvan KOENIG (VALLAURIS, France)
2010/04/05
*)

property newSubFolder : "[09]"

property theApp : "Pages"
property theExt : "pages"
property srcTypeId : {"com.apple.iwork.Pages.Pages", "com.apple.iwork.pages.sffpages"}

property msg1 : "" -- globale
property msg3 : "" -- globale
property msg90 : "" -- globale
property msg91 : "" -- globale
property msg92 : "" -- globale
property msg95 : "" -- globale
property msg98 : "" -- globale
property msg99 : "" -- globale

property rapport : "" -- globale

-- ===========

on run (* lignes exécutées si on double clique sur l'icône du script application
• lines executed if one double click the application script's icon *)
	
	tell application "System Events" to set dossier to choose folder (*
dans un bloc System Events pour avoir un titre de dialogue "localisé" 
• in a System Events block to get a localized dialog title. *)
	open {dossier}
end run

-- ===========

on open (sel) (* sel contient une liste d'alias des éléments qu'on a déposés sur l'icône du script (la sélection)
• sel contains a list of aliases of the items dropped on the script's icon (the selection) *)
	
	my nettoie()
	my prepareMessages()
	my afficheLeMessage(msg1) (*
Éviter de cliquer…
• Don't click… *)
	
	try
		repeat with elem in sel
			try
				my exploreTraite(elem as Unicode text, "")
			end try
		end repeat
		
		if rapport = "" then set rapport to msg90 (*
crée un fichier texte sur le Bureau *)
		set nomDuRapport to "report_Pages_'08_to_Page_'09.txt"
		set p2d to path to desktop
		set p2r to (p2d as Unicode text) & nomDuRapport
		tell application "System Events"
			if exists (file p2r) then delete (file p2r)
			make new file at end of p2d with properties {name:nomDuRapport}
		end tell
		set rapport to rapport as text
		write rapport to (p2r as alias)
		
		my afficheLeMessage(msg3) (*
Traitement terminé
• Export done. *)
		
	on error MsgErr number NroErr
		if NroErr is not -128 then
			beep 2
			tell application (path to frontmost application as string) to ¬
				display dialog "" & NroErr & " : " & MsgErr with icon 0 buttons {msg99} giving up after 20
		end if -- NroErr is…
	end try
	
	my nettoie()
end open

-- =============  

on afficheLeMessage(m)
	beep 1
	tell application (path to frontmost application as string)
		activate
		display dialog m buttons {msg98} default button 1 giving up after 10
	end tell
end afficheLeMessage

-- =============

on exploreTraite(elem, ptree) (*
elem est un Unicode text
• elem is an Unicode text *)
	local cl_, typeId_
	tell application "System Events" to tell disk item elem
		set cl_ to class
		if cl_ is folder then
			set typeId_ to ""
		else
			set typeId_ to type identifier
		end if
	end tell
	set cl_ to cl_ as Unicode text
	
	if typeId_ is in srcTypeId then
		my TraiteUnDocument(elem) (*
C'est un fichier Pages.
• It's a Pages document *)
	else if cl_ is in {"file package", "«class cpkg»"} then
		set rapport to rapport & elem & msg91 & return (*
"Package", Attention, un package EST un dossier "spécial".
• Caution, a package IS a "special" folder. *)
	else if cl_ is in {"folder", "«class cfol»"} then
		my ExploreUnDossier(elem, ptree)
	else
		set rapport to rapport & elem & msg92 & return (*  
"Pas un document Pages".
• "Not a Pages document" *)
	end if -- cl_ is …
end exploreTraite

-- ===========

on ExploreUnDossier(dossier, ptree)
	local nomElement, cheminElement, c
	if dossier does not end with ":" then set dossier to dossier & ":"
	tell application "Finder" to set listFolder to name of every item of folder dossier
	--repeat with nomElement in list folder dossier without invisibles -- list folder is deprecated :-(
	repeat with nomElement in listFolder
		tell application "System Events" to set c to name of (dossier as alias)
		my exploreTraite((dossier & nomElement), ptree & c & ":")
	end repeat
end ExploreUnDossier

-- ===========

on TraiteUnDocument(leCheminOriginal_UniText) (*
• leCheminOriginal_UniText is of class Unicode text *)
	local dossierSource, nomSource, p_xport
	
	set {nomSource, dossierSource} to my quelNomEtDossier(leCheminOriginal_UniText)
	set target to (dossierSource as text) & newSubFolder & ":"
	tell application "System Events"
		if not (exists folder target) then make new folder at end of folder dossierSource with properties {name:newSubFolder}
	end tell
	
	set p_xport to target & nomSource
	try
		tell application theApp
			open leCheminOriginal_UniText as alias
			save front document in p_xport
			close front document saving no (*
• Take care of old Pages documents *)
		end tell -- Pages
	on error
		set rapport to rapport & msg95 & leCheminOriginal_UniText & return
	end try
end TraiteUnDocument

-- ===========

on quelNomEtDossier(f)
	local nom, dossier
	tell application "System Events" to tell disk item (f as Unicode text)
		set nom to name (* Unicode text *)
		set dossier to path of container (* Unicode HFS path *)
	end tell -- System Events
	return {nom, dossier}
end quelNomEtDossier

-- =============

on parleAnglais()
	local z
	try
		tell application theApp to set z to localized string "Cancel"
	on error
		set z to "Cancel"
	end try
	return (z is not "Annuler")
end parleAnglais

-- =============

on nettoie() (*
pour ne pas stocker dans le fichier script
• So it will not be stored in the script file *)
	set dossierDeStockage to ""
	set rapport to ""
	set msg1 to ""
	set msg3 to ""
	set msg90 to ""
	set msg91 to ""
	set msg92 to ""
	set msg95 to ""
	set msg98 to ""
	set msg99 to ""
end nettoie

-- ============= 

on prepareMessages()
	if my parleAnglais() is false then
		set msg1 to "Éviter de cliquer durant l’exécution du script" & return & "sauf s’il le demande."
		set msg3 to "Traitement terminé."
		set msg90 to "Exportation réussie sans incident."
		set msg91 to "est un Package"
		set msg92 to "n’est pas un document " & theApp
		set msg95 to theApp & " n’a pas ouvert"
		set msg98 to " Vu "
		set msg99 to " Vu "
	else
		set msg1 to "Don’t click when the script is running." & return & "Except, of course, if it ask for."
		set msg3 to "Export done."
		set msg90 to "No problem during the export process."
		set msg91 to "is a Package"
		set msg92 to "is not a " & theApp & "’s document"
		set msg95 to theApp & " can’t open"
		set msg98 to " OK "
		set msg99 to "Oops"
	end if
	set msg91 to " ### " & msg91 & " ###"
	set msg92 to " ### " & msg92 & " ###"
	set msg95 to "### " & msg95 & " ### "
end prepareMessages

-- ===========
--[/SCRIPT]



Thank you.

I’m puzzled because I don’t understand how the given script was ever able to work.
To make it usable, I had to edit three instructions.

on exploreTraite(elem, ptree) (*
elem est un Unicode text
• elem is an Unicode text *)
	local cl_, typeId_
	tell application "System Events" to tell disk item elem
		set cl_ to get its class -- EDITED
		set cl_ to cl_ as Unicode text -- EDITED
		if cl_ is in {"folder", "«class cfol»"} then --folder then -- EDITED
			set typeId_ to ""
		else
			set typeId_ to type identifier
		end if
	end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) lundi 1 février 2021 16:19:23

Thanks a lot Yvan. This is working on my system now. (I had to open Pages 09 to prevent the script from exporting via the later version of Pages!)