Scripting Preview .. Translate from 10.6.8 to 10.7.5

Here is a script that will save and re-name PDFs sequentially from Preview. It works perfectly from 10.6.8 but the UI elements don’t translate for 10.7.5. Will someone give me a hand?

Select a folder that has several subfolders, each containing some pdfs.

tell application "Finder" to set subFolders to folders of (choose folder) as alias list
if application "Preview" is running then tell application "Preview" to quit

set deleteList to {}
repeat with aFolder in subFolders
	tell application "Finder" to set latestFiles to reverse of (sort (get files of folder aFolder whose name extension is "pdf") by creation date)
	set counter to 0
	repeat with aFile in latestFiles
		set counter to counter + 1
		set aFile to aFile as text
		set end of deleteList to aFile
		tell application "Preview"
			activate
			open file aFile
		end tell
		
		delay 0.5
		tell application "System Events"
			tell process "Preview"
				
				-- open save as
				keystroke "S" using {shift down, command down}
				repeat until (exists sheet 1 of window 1)
					delay 0.2
				end repeat
				
				-- name the file 
				set value of text field 1 of sheet 1 of window 1 to (counter & ".pdf" as text)
				-- save file
				click button "Save" of sheet 1 of window 1
				
				--replace if file exists
				if exists sheet 1 of sheet 1 of window 1 then
					click button "Replace" of sheet 1 of sheet 1 of window 1
				end if
				
				repeat until not (exists sheet 1 of window 1)
					delay 0.2
				end repeat
				
				-- close file
				activate
				set windowName to value of static text 1 of window 1
				click button 1 of window windowName
				repeat until not (exists window windowName)
					delay 0.2
				end repeat
			end tell
		end tell
	end repeat
end repeat

tell application "Finder" to delete deleteList

As far as I remember, there is no Save As. feature in Preview under 10.7.x

You must replace the command by

Duplicate
Save

Alas Duplicate has no built in shortcut.
You may :
(a) create a custom one thru the old standard scheme (in System Preference Pane > Keyboard).
I used control + option + command + S
or
(b) trigger the menu item Duplicate

Under 10.8.x, the behavior is slightly different.
Save As. is back with the shortcut shift + option + command + S

so I guess that the script would work with this simple change :


  keystroke "s" using {shift down, command down}

as


  keystroke "s" using {shift down, command down, option down}

Yvan KOENIG (VALLAURIS, France) vendredi 25 janvier 2013 20:56:21

Hi Yvan Koenig,

I’m trying to remember where I remember your name from. It’s been a long time since I’ve been on the internet. You’re the AppleWorks guru right?

You may say that but it’s a feature from the past.
(1) AppleWorks is really dead now.
(2) As I refused to obey to moderators I’m banned from Apple discussions forum.:stuck_out_tongue:

Yvan KOENIG (VALLAURIS, France) samedi 26 janvier 2013 10:35:45

Ha! What did they want you to do/not to do ?!?

Can anyone running 10.7 confirm save as has been replaced with duplicate and give me the UI Path?

According to several sources I’ve found on the Web this morning, the new Save As. saves any changes back to the original file as well as creating the new one! :o

If you want details, send a mail to :
koenig yvan sfr fr


my activateGUIscripting()

set sysv to get system attribute "sysv"
# 4144 = 10.3.0 = 4096 + 3*16 + 0
# 4153 = 10.3.9 = 4096 + 3*16 + 9
# 4160 = 10.4.0 = 4096 + 4*16 + 0
# 4171 = 10.4.11 = 4096 + 4*16 + 11
# 4176 = 10.5.0 = 4096 + 5*16 + 0
# 4184 = 10.5.8 = 4096 + 5*16 + 8
# 4192 = 10.6.0 = 4096 + 6*16 + 0
# 4200 = 10.6.8 = 4096 + 6*16 + 8
# 4208 = 10.7.0 = 4096 + 7*16 + 0
# 4213 = 10.7.5 = 4096 + 7*16 + 5
# 4224 = 10.8.0 = 4096 + 8*16 + 0
# 4226 = 10.8.2 = 4096 + 8*16 + 2
# 4240 = 10.9.0 = 4096 + 9*16 + 0


if sysv < 4144 then
	# it's OS X 10.2.x or older
else if sysv < 4160 then
	# it's OS X 10.3.x
else if sysv < 4176 then
	# it's OS X 10.4.x
else if sysv < 4192 then
	# it's OS X 10.5.x
else if sysv < 4208 then
	# it's OS X 10.6.x
else if sysv < 4224 then
	# it's OS X 10.7.x
	my select_Menu("Preview", 3, 9) # Duplicate
	(* {
01 - "Créer à partir du presse-papiers", 
02 - "Ouvrir.", 
03 - "Ouvrir l'élément récent", 
04 - missing value, 
05 - "Fermer", 
06 - "Tout fermer", 
07 - "Fermer l'image sélectionnée", 
08 - "Enregistrer une version", 
09 - "Dupliquer", 
10 - "Exporter.", 
11 - "Faire revenir le document à une version précédente...",  # CAUTION this one use three dots when others use ellipsis !
12 - missing value, 
13 - "Envoyer l'image sélectionnée", 
14 - "Saisir le mot de passe.", 
15 - missing value, 
16 - "Importer depuis l'appareil photo.", 
17 - "Importer depuis le scanner", 
18 - "Effectuer une capture d'écran",
19 - missing value, 
20 - "Imprimer."}
*)
else if sysv < 4240 then
	# my select_Menu("Preview", 3, 9) # Export.
	# it's OS X 10.8.x
	(* {
01 - "Créer à partir du presse-papiers", 
02 - "Ouvrir.", 
03 - "Ouvrir l'élément récent", 
04 - missing value, 
05 - "Fermer", 
06 - "Tout fermer", 
07 - "Fermer l'image sélectionnée", 
08 - "Enregistrer", 
09 - "Exporter.", 
10 - "Revenir à la version enregistrée.", 
11 - missing value,
12 - "Envoyer l'image sélectionnée", 
13 - "Saisir le mot de passe.", 
14 - missing value, 
15 - "Importer depuis la caméra.", 
16 - "Importer depuis le scanner", 
17 - "Effectuer une capture d'écran",
19 - missing value, 
20 - "Imprimer."}
*)
	activate application "Preview"
	tell application "System Events" to tell application process "Preview"
		tell menu bar 1 to tell menu bar item 3 to tell menu 1 to click menu item 9 # Exporter.
		repeat
			delay 0.1
			if exists sheet 1 of window 1 then exit repeat
		end repeat
		keystroke "d" using {command down} # to save on the Desktop
		tell sheet 1 of window 1
			# If the default filename ends with .pdf, there is no need to trigger the pop up menu
			if value of text field 1 does not end with ".pdf" then
				tell group 1
					class of UI elements
					--> {pop up button, static text, pop up button, static text, text field, static text, text field, static text, checkbox}
					value of pop up button 1
					--> "JPEG"
					click pop up button 1
					tell pop up button 1
						name of menu items of menu 1
						--> {"JPEG", "JPEG-2000", "OpenEXR", "PDF", "PNG", "TIFF"}
						click menu item "PDF" of menu 1
					end tell # pop up button 1
				end tell # group 1
			end if
			# Define the name of the PDF
			set value of text field 1 to "nouveau PDF.pdf"
			keystroke return # Save
		end tell # sheet 1 of window 1
	end tell # System Events
end if




#=====

on activateGUIscripting()
	(* to be sure than GUI scripting will be active *)
	tell application "System Events"
		if not (UI elements enabled) then set (UI elements enabled) to true
	end tell
end activateGUIscripting

#=====
(*
my selectMenu("Preview", 3, 9)
==== Uses GUIscripting ====
*)
on selectMenu(theApp, mt, mi)
	activate application theApp
	tell application "System Events" to tell application process theApp to tell menu bar 1 to ¬
		tell menu bar item mt to tell menu 1 to click menu item mi
	
end selectMenu

#=====
(*
useful to get the indexs of the triggered item
my select_Menu("Preview", 3, 9) (* File > Duplicate *)
==== Uses GUIscripting ====
*)
on select_Menu(theApp, mt, mi)
	activate application theApp
	tell application "System Events" to tell process theApp to tell menu bar 1
		get name of menu bar items
		(*{
01 - "Apple", 
02 - "Numbers", 
03 - "Fichier", 
04 - "Édition", 
05 - "Présentation", 
06 - "Aller", 
07 - "Outils", 
08 - "Signets", 
09 - "Fenêtre", 
10 - "Aide"}
*)
		get name of menu bar item mt
		--> {"Fichier"}
		tell menu bar item mt to tell menu 1
			get name of menu items
			
			get name of menu item mi
			--> {"Dupliquer"}
			#click menu item mi
		end tell -- menu bar.
		
	end tell -- System Events
end select_Menu

#=====

As you see, my memory failed about 10.8 behavior.
Save As. isn’t back in Preview (it’s in TextEdit, Keynote, Numbers, Pages and it’s available in iBooks Autor and Script Editor).
So I gave two ways to replace the missing feature.

For 10.7.x, I gave the code triggering Duplicate leaving you the task to code : save the replicate
For 10.8.x, I gave the entire code using Export.

In fact, you may use the Export. scheme in both cases.

Of course, you may remove the instructions which are coded to explain the tools behavior.

I forgot to give the code moving the newly saved file from the Desktop to the wanted final location.
You will have to add it.
My best choice to do that is « do Shell script “mv .” » or ASObjC Runner’s manage file feature.

Yvan KOENIG (VALLAURIS, France) dimanche 27 janvier 2013 12:26:53

Hello Nigel

It’s perfectly logical.

If you made some changes, they are automatically saved in the original document.
Triggering Save As. will just ave the modified doc in an other file but the dialog give you the ability to reset the original in it’s original state.
But, I never checked exactly which version is restored.
My preferred choice is to duplicate before editing or, use what was the main reason why I was banned, I save quite oll my documents as templates/stationaries so, when I open one, I work in fact on an original.

(1) I was wrong, Save As. is not back in Preview.
(2) even if it was, the OP didn’t say that the PDFs were edited. Just that he wanted to save them with an other name.

In my late message, I forgot to give the code moving the newly saved file from the Desktop to the wanted final location.

Yvan KOENIG (VALLAURIS, France) dimanche 27 janvier 2013 12:32:07

Yvan … that was very helpful, Thank you!

Good to know. :expressionless:

I repeat that it’s not really the correct description of the behaviour.
When a document is open, every change is automatically saved. No need to a specific action.
This is true under Mountain Lion. It was not in every case when running Lion.
When we issue the command « Save », we force changes to be saved, which is redondant but we also force the application to « save datas in the hidden folder dedicated to Versions » . In fact, Save is the new name for « Save a Version » which from my point of view would be named « Store a Version »

When we issue the command « Save as. », we save the document in a new file and we may ask the application to reset the original file (in which every changes were automatically saved without any user requirement).
My understanding is that this reset the document according to the late stored Version.

I take care to use two different verbs :
– Save to write datas in the document’s file
– Store to write datas in the database embedded in the hidden folder “.DocumentRevision-V100” available at root of every mounted local volume.

You may find details about this hidden database in :

https://www.box.com/s/nrpt1bejserlzda3iq49

Yvan KOENIG (VALLAURIS, France) lundi 28 janvier 2013 15:07:14

It is truly good to know how stuff works. Knowing that you have to duplicate before you start changing something is a boon! :slight_smile:

By the way, I have soon something finished, that I want you to try: I have localized a puny unix program to creates small text databases and sorts them accordingly to the LC_ALL variable. :slight_smile: It uses UTF-8 so you can enter Etiènne and such, (and have it sorted correctly in French too).

I’ll be in touch per email, when the last bugs are squashed!