1 Reply Latest reply: Aug 23, 2011 2:24 AM by KOENIG Yvan
Level 8 Level 8 (41,780 points)

Here is a clean way to retrieve Save As… feature in Pages

 

--(SCRIPT Pages_save_a_copy]

(*

Enregistrer le script en tant que Script : Pages_save_a_copy.scpt

déplacer le fichier créé dans le dossier

<VolumeDeDémarrage>:Utilisateurs:<votreCompte>:Bibliothèque:Scripts:Applications :Pages:

Il vous faudra peut-être créer le dossier Pages et peut-être même le dossier Applications.

 

Aller au menu Scripts , choisir Pages puis choisir “Pages_save_a_copy”

 

Le script enregistre une copie du document au premier plan

au format natif de Pages en ajoutant la date et l'heure au nom du fichier.

 

--=====

 

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".

 

+++++++++

 

Save the script as a Script: Pages_save_a_copy.scpt

 

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 folder Applications by yourself.

 

Go to the Scripts Menu, choose Pages, then choose “Pages_save_a_copy”

 

The script saves the frontmost document

in the native Pages format in a date_time stamped file.

 

--=====

 

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

 

--=====

 

Yvan KOENIG (VALLAURIS, France)

2011/08/12

*)

 

on run

          local docPath, docName, docFolder, fileName, theExt, newName, newPath

          set major_OS to get system attribute "sysv"

  (*

Grab infos about the open doc at front *)

          tell application "Pages"

                    tell document 1

                              set docPath to its path

                              set docName to its name

                    end tell

                    if major_OS ≥ 4208 then save docName (* Required for Lion *)

          end tell

  (*

Grab infos about the file from which the doc was open *)

          tell application "System Events" to tell disk item docPath

                    set docFolder to path of container

                    set fileName to name

                    set theExt to name extension

          end tell

  (*

Build an unique name for the new document *)

          if theExt is not "" then

                    set newName to (text 1 thru -(2 + (length of theExt)) of fileName) & (do shell script "date +_%Y%m%d_%H%M%S.") & theExt

          else

                    set newName to fileName & (do shell script "date +_%Y%m%d_%H%M%S")

          end if

  (*

Create the new file *)

          tell application "System Events"

  make new file at end of folder docFolder with properties {name:newName}

          end tell

          set newPath to (docFolder & newName) as alias

  (*

Save a copy in the newly created file *)

          tell application "Pages"

  save document docName in newPath

          end tell

end run

 

--=====

--[/SCRIPT]

--{code}

 

 

 

Yvan KOENIG (VALLAURIS, France) vendredi 19 août 2011 22:15:39

iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0

My iDisk is : <http://public.me.com/koenigyvan>

  • Level 8 Level 8 (41,780 points)

    Here is an other one.

    Read carefully the explanations given at the very beginning, in French then in English.

    With this one you master the time when you save. CAUTION, Autosave is unable to do it's duty when you work on a document open with the script so don't forget to ask the script to save from time to time.

     

    --(SCRIPT Pages_without_autosave]

    (*

    Enregistrer le script en tant qu'application sur le Bureau.

     

    Glisser une icône de document Pages sur l'icône de l’application

    ouvre le document, le duplique, ferme l'original renomme la copie en ajoutant l'information date_heure

    crée un bloc texte afin d'y stocker le chemin d'accès au dossier source et le nom court du document original.

     

    Travailler sur le document et de temps à autres, cliquer sur l'cône du script application afin d'enregistrer

    le document et repartir avec une copie qui n'ayant jamais été enregistré ne sera pas autoenregistré.

    Bien entendu, quand vous aurez fini de travailler sur le document il vous faudra l'enregistrer comme autrefois.

     

    --=====

     

    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".

     

    +++++++++

     

    Save the script as an application on the Desktop.

     

    Drop a Pages doc icon on the application's one, open the document, duplicate it, save the original if it was created from a template,

    rename the duplicate inserting a date_time stamp, create a text box store the pathname to the source folder and the short name of the original.

     

    After that you may work upon the document. From time to time, click the script's icon to save the document

    and replace it by a fresh replicate. So this one which was never saved will not be autosaved.

    When you have finished to work upon the doc, save it as you did in the past.

     

    --=====

     

    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

     

    --=====

     

    Yvan KOENIG (VALLAURIS, France)

    2011/08/22

    *)

    property permitted : {"com.apple.iWork.Pages.pages", "com.apple.iWork.Pages.sffpages"}

    property modeles : {"com.apple.iWork.Pages.template", "com.apple.iWork.Pages.sfftemplate"}

    property boxName : "pathname_shortname_emantrohs_emanhtap"

     

    --=====

     

    on run

              local docName, path_infos, docFolder, nomCourt, docPath, nbd

              my controle_versions()

      (*

    Grab infos about the frontmost Pages document *)

              tell application "Pages"

                        set docName to name of document 1

                        tell document docName

                                  (*

    Try to extract the infos stored by the open handler          *)

                                  try

                                            set path_infos to object text of text box boxName

                                  on error

                                            if my parleAnglais() then

                                                      error "The document “" & docName & "”" & return & "wasn’t open with this script !"

                                            else

                                                      error "Le document « " & docName & " »" & return & "n’a pas été ouvert avec ce script !"

                                            end if

                                  end try

                        end tell -- document

              end tell -- Pages

      (*

    Split the extracted datas *)

              set {docFolder, nomCourt} to my decoupe(path_infos, tab)

              set docPath to docFolder & docName

              tell application "System Events"

      (*

    This test was useful during my tests, now it's useless *)

                        if not (exists disk item docPath) then

      make new file at end of folder docFolder with properties {name:docName}

                        end if

              end tell -- System Events

              set docPath to docPath as alias

      (*

    I don't understand why I must apply save - close - open          but without that it fails *)

              tell application "Pages"

      save document docName in docPath

      close document docName

      open docPath

                        set nbd to count of documents

              end tell

              my selectMenu("Pages", 3, 10) (* Duplicate *)

      (*

    Wait the availability of the duplicate *)

              tell application "Pages"

                        repeat 100 times

                                  if (get count of documents) > nbd then exit repeat

                                  delay 0.2

                        end repeat

      (*

    Now we may close the 'old' document *)

      close document docName without saving

              end tell -- Pages

      (*

    Build a date_time stamped name for next version *)

              set docName to nomCourt & my build_a_stamp() & ".pages"

     

              tell application "Pages"

                        set name of document 1 to docName

              end tell

    end run

     

    --=====

     

    on open (sel)

              local docPath, docFolder, fileName, theExt, typeID, nomCourt, nouveauNomCourt, newPath, oldUnits, docName, nbd

      (*

    Apply consistency checks *)

              my controle_versions()

              set docPath to sel's item 1 (* Here, docPath is an alias *)

      (*

    Extract some infos about the original document *)

              tell application "System Events" to tell disk item (docPath as text)

                        set docFolder to path of container

                        set fileName to name

      (*

    The try block may be useful if an user is fool enough to drag & drop a folder on the scrip’s icon *)

                        try

                                  set theExt to name extension

                        on error

                                  set theExt to ""

                        end try

                        try

                                  set typeID to type identifier

                        on error

                                  set typeID to ""

                        end try

              end tell

      (*

    Check that the dragged item is a Pages document *)

              if (typeID is not in permitted) and (typeID is not in modeles) then error number -128

      (*

    Strip the name extension if there is one *)

              if theExt is not "" then

                        set nomCourt to (text 1 thru -(2 + (length of theExt)) of fileName)

              else

                        set nomCourt to fileName

              end if

      (*

    If the original is a template, we will save the newly created file *)

              if typeID is in modeles then

                        set nouveauNomCourt to nomCourt & my build_a_stamp()

                        set fileName to nouveauNomCourt & ".pages"

      (*

    Create a new file *)

                        tell application "System Events"

      make new file at end of folder docFolder with properties {name:fileName}

                        end tell

                        set newPath to (docFolder & fileName) as alias

              end if

              tell application "Pages"

                        set oldUnits to ruler units

                        set ruler units to points

      open docPath

                        if typeID is in modeles then

                                  (*

    Original is a template so save the new doc once *)

      save document 1 in newPath

                                  set name of document 1 to fileName

                        end if

                        set docName to name of document 1

                        set nbd to count of documents

              end tell

     

              my selectMenu("Pages", 3, 10) (* Duplicate *)

      (*

    Wait the availability of the duplicate *)

              tell application "Pages"

                        repeat 100 times

                                  if (get count of documents) > nbd then exit repeat

                                  delay 0.2

                        end repeat

      (*

    Now may close the 'original' document *)

      close document docName without saving

              end tell

              set docName to nomCourt & my build_a_stamp() & ".pages"

     

              tell application "Pages"

                        set name of document 1 to docName

      (*

    CAUTION : due to a bug, whichever wrap property is asked, the object is defined with wrap = center *)

                        tell document docName

      make text box at front with properties {widthget width of page 1), name:boxName, object text:docFolder & tab & nomCourt, horizontal position:0.0, vertical position:0.0, height:15.0, placement:fixed, wrap:none}

                                  tell text box boxName to set properties of object text to {color:{65535, 65535, 65535}, font size:9.0, font name:"Helvetica"}

                        end tell -- document

                        set ruler units to oldUnits

              end tell

    end open

    (*

    {width:358.0,

    name:missing value,

    stroke type:none,

    object text:"Tapez pour saisir le texte",

    rotation:0.0,

    stroke width:missing value,

    locked:0,

    text fit:rectangle,

    end color:missing value,

    extra space:12.0,

    horizontal position:5.0,

    class:text box,

    id:5459273,

    color angle:missing value,

    shadow blur:missing value,

    stroke color:missing value,

    vertical position:5.0,

    height:20.0,

    shadow opacity:missing value,

    shadow color:missing value,

    placement:fixed,

    wrap:center,

    fill type:none,

    containing page:…

    shadow angle:missing value,

    image data:missing value,

    containing layer:foreground layer of page 1 of document id 1648067 of application "Pages",

    single color:missing value,

    shadow:missing value,

    image fill type:missing value,

    shadow offset:missing value,

    start color:missing value,

    opacity:100.0}

    *)

    --=====

     

    on build_a_stamp()

              return do shell script "date +_%Y%m%d_%H%M%S"

    end build_a_stamp

     

    --=====

     

    on controle_versions()

              local app_Version, sysv

              tell application "Pages" to set app_Version to version

              set sysv to system attribute "sysv"

              if (app_Version < "4.1") or (sysv < 4208) then error number -128

    end controle_versions

     

    --=====

     

    on parleAnglais()

              local z

              try

                        tell application "Pages" to set z to localized string "Cancel"

              on error

                        set z to "Cancel"

              end try

              return (z is not "Annuler")

    end parleAnglais

     

    --=====

     

    on decoupe(t, d)

              local oTIDs, l

              set oTIDs to AppleScript's text item delimiters

              set AppleScript's text item delimiters to d

              set l to text items of t

              set AppleScript's text item delimiters to oTIDs

              return l

    end decoupe

     

    --=====

     

    on recolle(l, d)

              local oTIDs, t

              set oTIDs to AppleScript's text item delimiters

              set AppleScript's text item delimiters to d

              set t to l as text

              set AppleScript's text item delimiters to oTIDs

              return t

    end recolle

     

    --=====

    (*

    my selectMenu("Pages",5, 12)

    ==== Uses GUIscripting ====

    *)

    on selectMenu(theApp, mt, mi)

              tell application theApp to activate

              tell application "System Events" to tell process theApp to tell menu bar 1 to tell menu bar item mt to tell menu 1 to click menu item mi

    end selectMenu

     

    --=====

    --[/SCRIPT]

    --{code}

     

    Yvan KOENIG (VALLAURIS, France) mardi 23 août 2011 11:24:09

    iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8 and 10.7.0

    My iDisk is : <http://public.me.com/koenigyvan>


    Please : Search for questions similar to your own before submitting them to the community

     

    To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !