• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: automatic saving pdf files in pages
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: automatic saving pdf files in pages


  • Subject: Re: automatic saving pdf files in pages
  • From: KOENIG Yvan <email@hidden>
  • Date: Tue, 21 Sep 2010 12:19:23 +0200

Le 21 sept. 2010 à 11:29, Jarda Kotesovec a écrit :

>
> Hi all!
>
> I was wondering if would be possible solve the following issue in
> Pages with apple script.
>
> I need to force Pages to save document in PDF format at the moment
> when you save it as a native Pages document.  Normally user have to
> save it (as a native pages document) and then pick up "File -> Export
> ... -> PDF -> Next.. -> Export" to have pdf copy as well. I need to
> save these clicks (exporting to pdf) in term of faster workflow.
>
> Any feedback is really appreciated.
>
> Regards
> Jarda

Here is a script doing the trick.

This one use GUI scripting so you may define the quality level thru the setting of a property.

In an other mail I will post an other one which doesn't use GUI scripting. It behaves like the Print as PDF command so you can't select the quality level.

--{code}
--[SCRIPT Pages2PDFattachment]
(*
Enregistrer le script en tant que Script : Pages2PDFattachment
déplacer le fichier ainsi créé dans le dossier
<VolumeDeDémarrage>:Users:<votreCompte>:Library:Scripts:Applications:Pages:
Il vous faudra peut-être créer le dossier Pages et peut-être même le dossier Applications et le dossier Scripts.

Sélectionner un document Pages
aller au menu Scripts , choisir Pages puis choisir  Pages2PDFattachment
Si le document n'avait jamais été enregistré, il le sera.
Il le sera également s'il a été modifié.
Un fichier PDF (meilleure qualité) sera créé
et il sera inséré comme pièce jointe dans un nouveau Mail.

--=====

L'aide du Finder explique:
L'Utilitaire AppleScript permet d'activer le Menu des scripts :
Ouvrez l'Utilitaire AppleScript situé dans le dossier Applications/AppleScript.
Cochez la case "Afficher le menu des scripts dans la barre de menus".
Sous 10.6.x,
aller dans le panneau "Général" du dialogue Préférences de l'Éditeur Applescript
puis cocher la case "Afficher le menu des scripts dans la barre des menus".

--=====

Save the script as a Script: Pages2PDFattachment

Move the newly created file into the folder:
<startup Volume>:Users:<yourAccount>:Library:Scripts:Applications:Pages:
Maybe you would have to create the folder Pages and even the folders Applications  and Scripts by yourself.

Select a Pages document.
go to the Scripts Menu, choose Pages, then choose "Pages2PDFattachment"
If the document was never saved, it will be.
If it was modified, it's saved too.
Then, the document is exported in a PDF file (Best quality).
A new mail is created with the PDF inserted as attachment.

--=====

The Finder's Help explains:
To make the Script menu appear:
Open the AppleScript utility located in Applications/AppleScript.
Select the "Show Script Menu in menu bar" checkbox.
Under 10.6.x,
go to the General panel of AppleScript Editor’s Preferences dialog box
and check the “Show Script menu in menu bar” option.

--=====

Yvan KOENIG (VALLAURIS, France)
2010/09/10
*)

property theApp : "Pages"
property theExt : "pages"
property srcTypeId : "com.apple.iwork.pages.pages"

property storeOnDesktop : true (*
true = dest folder will be on Desktop
false = dest folder will be in "~/Documents"
*)
property create_a_mail : false

property theMenu : missing value -- globale
property menuExport : missing value -- 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 *)
	my nettoie()
	my export2PDF()
	my nettoie()
end run

--=====

on nettoie() (*
pour ne pas stocker dans le fichier script
• So it will not be stored in the script file *)
	set theMenu to missing value
	set menuExport to missing value

end nettoie

--=====

on export2PDF()
	local sysVersion, theMenu, menuExport, localExport, dossierParDefaut, theButton
	local quality, newExt, p2d, nom_de_p, nouveauChemin, w, x, p_xport

	set sysVersion to get system attribute "sysv"

	if 4160 > sysVersion then
		if my parleAnglais() then
			error "This script requires MacOs X 10.4 or higher !"
		else
			error "Ce script requiert MacOs X 1.4 ou ultérieur !"
		end if
	else if sysVersion < 4176 then (* it's macOS X 10.4.x *)
		if my getVersion() < "4" then
			set theMenu to 3
			set menuExport to 13
		else
			set theMenu to 10
			set menuExport to 9
		end if
	else (* it's macOS X 10.5.x or 10.6.x *)
		if my getVersion() < "4" then
			set theMenu to 3
			set menuExport to 13
		else
			set theMenu to 10
			set menuExport to 8
		end if
	end if

	tell application theApp
		activate
		set localExport to localized string "Export"
		set dossierParDefaut to my getDefaultExport() as text
		set theButton to 1
		(*
1	button PDF
2	button Word
3	button RTF
4 	button Standard
*)
		set quality to 3
		(* 1 = Good, 2 = Better, 3 = Best *)

		set newExt to "pdf"

		if path of (get properties of document 1) is missing value then
			(*
The document was never saved, save it *)
			if storeOnDesktop then
				set p2d to "" & (path to desktop folder)
			else
				set p2d to "" & (path to documents folder)
			end if
			set nom_fichier to (do shell script "date +_%Y%m%d_%H%M%S.pages")
			save document 1 in (p2d & nom_fichier)
		end if
		(*
If the document was modified, save it *)
		if modified of document 1 then save document 1

		set nom_de_p to name of document 1
	end tell -- to theApp
	set w to nom_de_p
	(*
Build the PDF's pathname *)
	if nom_de_p ends with theExt then set nom_de_p to text 1 thru -(2 + (length of theExt)) of nom_de_p
	set nouveauChemin to dossierParDefaut & nom_de_p & "." & newExt
	(*
If such a PDF already exists date_time stamp it according to its modification date. *)
	tell application "System Events" to if exists (file nouveauChemin) then set name of file nouveauChemin to nom_de_p & my horoDateur(modification date of file nouveauChemin) & "." & newExt (* stamped name *)
	try
		(*
Trigger the Export menu item *)
		tell application "System Events" to tell (first process whose title is theApp)
			click menu item menuExport of menu 1 of menu bar item theMenu of menu bar 1 (* Exporter… *)
			repeat until exists sheet 1 of window w
				delay 0.1
			end repeat
			(*
Trigger the Export as PDF 'button' *)
			tell sheet 1 of window w (* sheet containing the buttons PDF, Word, RTF, Standard *)
				if sysVersion = 4 then (* it's macOS X 10.4.x *)
					click button theButton of radio group 1
				else if sysVersion = 5 then (* it's macOS X 10.5.x *)
					click checkbox theButton of radio group 1
				else (* it's macOS X 10.6.x *)
					click radio button theButton of radio group 1 (* I hope that they will no longer change it *)
				end if -- isOs4 is true

				if theButton is 1 then
					(*
Exporting as PDF, so trigger the quality button *)
					delay 0.2
					tell pop up button 1
						click
						click menu item quality of menu 1
					end tell
					delay 0.2
				end if
				click button 1 (* Suivant… *)
				repeat until exists button localExport
					delay 0.2
				end repeat
				(*
Click the Export button *)
				click button localExport (* Exporter… *)
			end tell -- to sheet…
			(*
If it appears, click the "Don't review" button in the warning report *)
			repeat 20 times
				if exists sheet 1 of window w then
					click button 2 of sheet 1 of window w (*
"Ne pas consulter " dans éventuel rapport d'anomalies
• "Don't review" in sheet reporting possible export anomalies *)
					exit repeat
				end if
				delay 0.5
			end repeat
		end tell -- to process … System Events

		(*
Go to the handler which creates a new mail *)
		if create_a_mail then my cree_un_mail(nouveauChemin as alias)

	on error errMsg number errNbr
		beep 1
		error "#" & errNbr & space & errMsg
	end try
end export2PDF

--=====

on cree_un_mail(the_file)
	local the_subject, the_body, the_file, new_message

	set the_subject to ""
	set the_body to return

	tell application "Mail"
		set new_message to make new outgoing message ¬
			with properties {subject:the_subject, content:the_body, visible:true}
		tell content of new_message
			make new attachment with properties {file name:the_file} at after last paragraph
		end tell
	end tell -- Mail
end cree_un_mail

--=====
(*
• Build a stamp from the modification date_time
*)
on horoDateur(date_de_modification)
	local les_secondes
	set les_secondes to time of date_de_modification
	return "_" & year of date_de_modification & text -2 thru -1 of ("0" & (month of date_de_modification as integer)) & text -2 thru -1 of ("0" & day of date_de_modification) & "_" & text -2 thru -1 of ("0" & les_secondes div 3600) & text -2 thru -1 of ("0" & (les_secondes mod 3600) div 60) & text -2 thru -1 of ("0" & les_secondes mod 60)
	(*
• Here, the stamp is  "_YYYYMMDD-hhmmss" *)
end horoDateur

--=====

on getPlistValue(valName, default)
	local thePlist, u
	set thePlist to "" & (path to preferences folder) & "com.apple.iWork." & theApp & ".plist"
	tell application "System Events"
		if exists file thePlist then
			tell contents of property list file thePlist
				try
					set u to (value of property list item valName) (* Unicode Text *)
				on error (*
On est là si Pages n'a rien enregistré avec des préférences neuves
• Here if Pages never saved with the new preferences file. *)
					set u to default
				end try
			end tell -- to contents of…
		else (*
On est là s'il n'y a pas de fichier de préférences
• Here if there is no preferences file. *)
			set u to default
		end if
	end tell -- to system events
	return u
end getPlistValue

--=====

on getDefaultExport()
	local u
	set u to my getPlistValue("SLDocumentDefaultExportDirectory", "~/Documents")

	set u to (POSIX file (do shell script "echo " & u)) as text
	if u ends with ":" then
		return u
	else
		return (u & ":")
	end if
end getDefaultExport

--=====

on getLocale(a, x)
	tell application a to return localized string x
end getLocale

--=====

on getVersion()
	try
		tell application theApp to return version
	on error
		return "1"
	end try
end getVersion

--=====

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

--=====
--[/SCRIPT]
--{code}


Yvan KOENIG (VALLAURIS, France) mardi 21 septembre 2010 12:18:54



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

References: 
 >automatic saving pdf files in pages (From: Jarda Kotesovec <email@hidden>)

  • Prev by Date: automatic saving pdf files in pages
  • Next by Date: Re: automatic saving pdf files in pages
  • Previous by thread: automatic saving pdf files in pages
  • Next by thread: Re: automatic saving pdf files in pages
  • Index(es):
    • Date
    • Thread