Change color of texts with a specific character spec in Quark

Hi,

I am trying to change the color of characters (or text) with a specific character spec called “7 Text 4 PREIS”.

My applescript so far:


tell application "QuarkXPress"
	tell document 1
		set cstyles to name of every character spec
		display dialog (item 15 of cstyles)
		if "7 TEXT 4 PREIS" is in cstyles then
			set color of character properties of character spec (item 15 of cstyles) to {color:"red"}
		end if
	end tell
end tell

I get this error message:

I get the same message if I try character attributes instead of properties:


tell application "QuarkXPress"
	tell document 1
		set cstyles to name of every character spec
		display dialog (item 15 of cstyles)
		if "7 TEXT 4 PREIS" is in cstyles then
			set color of character attributes of character spec (item 15 of cstyles) to {color:"red"}
		end if
	end tell
end tell

Can anyone help, please?

Thanks in advance.

Model: Mac Pro (Early 2009)
AppleScript: 2.2.1
Operating System: Mac OS X (10.7)


tell application "QuarkXPress"
   tell document 1
set properties of characters of story 1 of text box 1 to {color:color spec:"red"}
-- or
set properties of characters of story 1 of text box 1 to {character style:character spec "7 Text 4 PREIS"}
end tell
end tell

Thanks DJ Bazzie Wazzie for helping me (again ;))

I tried this version of the script:


tell application "QuarkXPress"
	tell document 1
		set properties of characters of story 1 of text box 1 to {color:color spec "red"}
	end tell
end tell

If I execute the script I get the following error message:

What I want to achieve is to change color of all texts in a QuarkXPress document to red which have the character style “7 Text 4 PREIS”!

Hi! This script shall change the color at the root. So any occurrence of character whose style is applied to will be changed instantly.

tell application "QuarkXPress9"
	tell document 1
		set cstyles to name of every character spec
		-- don't know why the Display dialog was here. put it back if you need it
		if "7 TEXT 4 PREIS" is in cstyles then
			set color of character spec "7 TEXT 4 PREIS" to "red"
		end if
	end tell
end tell

HTH.

Do you have an color spec named “red”? By default an new project doesn’t have a color spec named “red” and you need to create it first.

tell application "QuarkXPress"
	tell document 1
		if "red" is not in name of color specs then
			-- if color not exists, create one
			make new color spec at beginning with properties {name:"Red", RGB color value:{65534, 0, 0}}
		end if
		set properties of characters of story 1 of text box 1 to {color:color spec "Red"}
	end tell
end tell

Thanks CMYS for helping :slight_smile:

I tried your suggestion:


tell application "QuarkXPress"
	tell document 1
		set cstyles to name of every character spec
		-- the Display dialog is not necessary
		if "7 TEXT 4 PREIS" is in cstyles then
			set color of character spec "7 TEXT 4 PREIS" to "red"
		end if
	end tell
end tell

I get this error:

Sorry I forgot to mention that it must work with QuarkXPress 8.5.1.

It is not a new project, I have to change an existing project with several color specs.

No I have not a color spec “red”. I have several color specs and need only to change color for text with the color spec “7 Text 4 PREIS” to red.

The problem with the error is that the color spec with the name red doesn’t exist. Just change it to an existing name or create a color spec like in my last example or by hand.

The script from CMYS doesn’t work because you can only set color to color specs references not their direct name.

tell application "QuarkXPress9"
   tell document 1
       set cstyles to name of every character spec
       -- don't know why the Display dialog was here. put it back if you need it
       if "7 TEXT 4 PREIS" is in cstyles then
           set color of character spec "7 TEXT 4 PREIS" to color spec "red"
       end if
   end tell
end tell

But again, the updated CMYS script above here requires that the color spec exists.

Well nothing new in v9 regarding all this actually.
But to what I read in other posts you’d need to first create a color spec “red” prior to change the color in the character spec.

tell application "QuarkXPress"
	tell document 1
		if not (exists (color spec "red")) then
			make color spec at end with properties {name:"red", CMYK color value:{0, 65535, 65535, 6554}, separation:true} -- 0% = 0 | 100% = 65535
		end if
		do updates
		set cstyles to name of every character spec
		-- the Display dialog is not necessary
		if "7 TEXT 4 PREIS" is in cstyles then
			set color of character spec "7 TEXT 4 PREIS" to "red"
		end if
	end tell
end tell

Is this fixing it?

DJ Bazzie Wazzie I understand and you are right.

GREAT :slight_smile: :slight_smile: :slight_smile:

With Both of your hints it works!!!



on processColoredText()
	tell application "QuarkXPress"
		tell document 1
			if not (exists (color spec "preisgruen")) then
				make color spec at end with properties {name:"preisgruen", CMYK color value:{45875, 0, 65535, 0}, separation:true}
				-- 0% = 0 | 100% = 65535
			end if
			do updates
			set cstyles to name of every character spec
			if "7 TEXT 4 PREIS" is in cstyles then
				set color of character spec "7 TEXT 4 PREIS" to "preisgruen"
			end if
			if "5,5 Text 4 PREIS " is in cstyles then
				set color of character spec "5,5 Text 4 PREIS " to "preisgruen"
			end if
		end tell
	end tell
end processColoredText


I really thank you both for your great help… THANKS :smiley:

Here is my complete script so far:


set theFolder to (choose folder) as string
set theFiles to paragraphs of (do shell script "ls " & quoted form of POSIX path of theFolder)
repeat with i from 1 to count theFiles
	repeat 1 times --to simulate continue
		set theFile to theFolder & item i of theFiles as string
		if theFile does not end with ".qxd" then
			exit repeat -- simulate a continue command
		end if
		openFile(theFile)
		-- if number of document = 0 the file isn't opened
		if nrOfOpenDocuments() is 0 then
			exit repeat -- simulate a continue command
		end if
		processOpenDocument()
		processColoredText()
		closeAndSaveDocument(theFile)
	end repeat
end repeat

on processOpenDocument()
	repeat with storyIndex from 1 to countStories()
		set theStory to getStoryContents(storyIndex)
		set priceOffsets to AST find regex "[0-9]{0,1}[.]{0,1}[0-9X]{1,3},[0-9X]{2}" in string theStory with returning offsets
		if priceOffsets is not missing value then
			-- reverse the list, edit from end to front so the offsets stays correct
			set priceOffsets to reverse of priceOffsets
			repeat with priceRange in priceOffsets
				set foundString to getRangeOfStory(storyIndex, rm_so of priceRange, rm_eo of priceRange)
				setRangeOfStory(storyIndex, rm_so of priceRange, rm_eo of priceRange, "xx,xx")
			end repeat
		end if
	end repeat
end processOpenDocument

on processColoredText()
	tell application "QuarkXPress"
		tell document 1
			if not (exists (color spec "preisgruen")) then
				make color spec at end with properties {name:"preisgruen", CMYK color value:{45875, 0, 65535, 0}, separation:true}
				-- 0% = 0 | 100% = 65535
			end if
			do updates
			set cstyles to name of every character spec
			if "7 TEXT 4 PREIS" is in cstyles then
				set color of character spec "7 TEXT 4 PREIS" to "preisgruen"
			end if
			if "5,5 Text 4 PREIS " is in cstyles then
				set color of character spec "5,5 Text 4 PREIS " to "preisgruen"
			end if
		end tell
	end tell
end processColoredText
-------------------------------------------------------------
-- QXP HANDLERS
-------------------------------------------------------------

on nrOfOpenDocuments()
	tell application "QuarkXPress" to return count of documents
end nrOfOpenDocuments

on openFile(thePath)
	tell application "QuarkXPress" to open alias thePath with Suppress All Warnings
end openFile

on closeAndSaveDocument(thePath)
	tell application "QuarkXPress" to close document 1 with saving
end closeAndSaveDocument

on countStories()
	tell application "QuarkXPress"
		tell document 1 to return count of stories
	end tell
end countStories

on getStoryContents(i)
	tell application "QuarkXPress"
		tell document 1 to return contents of story i
	end tell
end getStoryContents

on setRangeOfStory(i, s, e, t) -- index, start range, end range, text
	tell application "QuarkXPress"
		tell document 1 to set text of characters s thru e of story i to t
		-- tell document 1 to set properties of characters s thru e of story i to {color:red}
	end tell
end setRangeOfStory

on getRangeOfStory(i, s, e) -- index, start range, end range
	tell application "QuarkXPress"
		tell document 1 to return characters s thru e of story i
	end tell
end getRangeOfStory

-------------------------------------------------------------
-- MISCELLANEOUS HANDLERS
-------------------------------------------------------------
on getPathsFromFolder()
	set rawData to do shell script "ls " & quoted form of POSIX path of inPath
	set theFileNames to AST find regex "([^" & return & "]+)($|" & return & ")" in string rawData regex group 2
	
	repeat with x from 1 to count theFileNames
		set item x of theFileNames to inPath & item x of theFileNames
	end repeat
	return theFileNames
end getPathsFromFolder

You’re welcome and nice to hear you have it all working. That’s the goal we’re trying to achieve :cool: