apply a style to some characters in Pages

Here is a script which I wrote few minutes ago.
It achieve the wanted goal but I’m not satisfied.

I wished to drop the loop scanning every characters to apply a style to the underlined ones but I failed to find a syntax using a whose instruction.

Maybe somebody here will be more efficient.


--{code}
--[SCRIPT underline_to_Style]
(*
Enregistrer le script en tant que Script : underline_to_Style.scpt
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.
 
Ouvrir un document Pages contenant des caractères soulignés
Aller au menu Scripts , choisir Pages puis choisir "underline_to_Style"
Le script appliquera le style "Souligné" aux caractères soulignés
 
--=====
 
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: underline_to_Style.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.
 
Select a Pages document embedding underlined characters
Go to the Scripts Menu, choose Pages, then choose "underline_to_Style"
The script will apply the named style "Underlined" to the underlinrd characters.
 
--=====
 
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)
2011/11/13
*)
--=====
 
on run
          set Underlined_loc to my getLocalizedStyleName("Pages", "Blank.template", "STYLE_Underline")
          tell application "Pages" to tell document 1
                    set mon_style to first character style whose name is Underlined_loc
                    set nbcar to count of characters
                    repeat with i from 1 to nbcar
                              if (get underline type of character i) as text is not "none" then
                                        set character style of character i to mon_style
                              end if
                    end repeat
          end tell
end run
 
--=====
(*
Example
set UNTITLED_loc to my getLocalizedStyleName("Pages", "STYLE_Heading 8")
 
Requires :
getLocalizedName()
*)
on getLocalizedStyleName(theApp, tName, x)
          local path2app, p2bndl
          tell application theApp to activate
 
          tell application "System Events"
                    set path2app to (application file of application process theApp as text)
                    set p2bndl to path2app & "Contents:Resources:Templates:" & tName & ":Contents:Resources:"
                    return my getLocalizedName(theApp, x, p2bndl)
          end tell
end getLocalizedStyleName
 
--=====
 
on getLocalizedName(a, x, f)
          tell application a to return localized string x from table "Localizable" in bundle file f
end getLocalizedName
 
--=====
--[/SCRIPT]
--{code}

Yvan KOENIG (VALLAURIS, France) dimanche 13 janvier 2011 18:11:00

Pages seems to have a bug in that it returns strike-though types for the underline types of characters. However, this works as the run handler for me:

on run
	set Underlined_loc to my getLocalizedStyleName("Pages", "Blank.template", "STYLE_Underline")
	tell application "Pages" to tell document 1
		set character style of characters whose underline type is single underline or underline type is double underline to character style Underlined_loc
	end tell
end run

Thanks Nigel

I think that I’m on the track of the problem.
You saw that my run handler is :


on run
	set Underlined_loc to my getLocalizedStyleName("Pages", "Blank.template", "STYLE_Underline")
	tell application "Pages" to tell document 1
		set mon_style to first character style whose name is Underlined_loc
		set nbcar to count of characters
		repeat with i from 1 to nbcar
			if (get underline type of character i) as text is not "none" then
				set character style of character i to mon_style
			end if
		end repeat
	end tell
end run

I used that because the logical one :


on run
	set Underlined_loc to my getLocalizedStyleName("Pages", "Blank.template", "STYLE_Underline")
	tell application "Pages" to tell document 1
		set mon_style to first character style whose name is Underlined_loc
		set nbcar to count of characters
		repeat with i from 1 to nbcar
			if (get underline type of character i) is not none then
				set character style of character i to mon_style
			end if
		end repeat
	end tell
end run

apply the style to every characters.
It appears that the ‘none’ extracted from the character’s properties is not the same object that the ‘none’ used by AppleScript.

If I use :


on run
	set Underlined_loc to my getLocalizedStyleName("Pages", "Blank.template", "STYLE_Underline")
	tell application "Pages" to tell document 1
		set mon_style to first character style whose name is Underlined_loc
		set numbersNone to get underline type of character 1
		set nbcar to count of characters
		repeat with i from 1 to nbcar
			if (get underline type of character i) is not numbersNone then
				set character style of character i to mon_style
			end if
		end repeat
	end tell
end run

on a document whose first character is not underlined, it behaves flawlessly.

Yvan KOENIG (VALLAURIS, France) dimanche 13 janvier 2011 19:44:51