Quark misbehaviour with selected text boxes

Hi folks,
I wonder if anyone else has encoutered this behaviour, and if they found a work-around.

Basically I have a script that copies selected text boxes onto a newly created layer. However this doesn’t work perfectly. If I have four text boxes, the loop will iterate four times but frequently a text box will be ‘found’ twice. One of the reasons I set a text box to be ‘unselected’ is an attempt to prevent this duplicated selection.

There’s a number of display dialog statements in this code, as I’m trying to understand why Quark is being so mischievous. At a guess maybe the transfer to another layer is causing the problems but it does seem a bit silly to have two loops, one loop to find the selected boxes, and another to move these selected boxe to the new layer.

Any thoughts, or advice, gratefully received.




local numSelected
local strLayer, strSelected
set newLayer to "Battenburg Cake"

tell application "QuarkXPress"
	
	tell document 1
		-- Find if any text boxes are selected
		set numSelected to (count of (text boxes where selected is true))
		if (numSelected is less than 1) then -- if a text box isn't selected, display this warning, and return.
			display dialog "You must select one text box"
			return
		else -- we have text boxes selected	
			make new layer at end with properties {name:newLayer}
			--display dialog ("We selected " & numSelected & " boxes ")
			repeat with tb from 1 to (text box count)
				tell text box tb
					--	set strValue to story 1
					--	display dialog (strValue)
					if (selected is true) then
						--display dialog ("This text box " & tb & " is selected")
						set strLayer to layername
						set strSelected to "yes"
						set selected to false
					else
						set strLayer to "none"
						set strSelected to "no"
					end if
				end tell
				
				--display dialog ("Details: " & tb & ": " & strLayer & ": " & strSelected & " text box count " & (text box count))
				if strSelected is equal to "yes" then
					move text box tb of layer strLayer to beginning of layer newLayer
				end if
			end repeat
		end if
		
	end tell
end tell -- end of the work with the application


The stupidity continues… i’ve tried altering the script so as it works with Unique Ids, but this doesn’t seem to work properly either. BoxList does hold the Ids of the selected boxes, and the first loop does seem to know the correct Id, however Quark moves the wrong box.

Occasionally this works, but most of the time this script doesn’t. Yet the logic seems straight forward, and the debug dialogs confirm what I’d expect, but despite all this nine times out of ten, Quark moves the wrong text box.

Just in case you were wondering. I created a simple Quark document with four text boxes. Scripts I’ve developed before work fine with one box, but are not reliable with multiple selects. One of the peculiarities I’ve noticed is that although Quark knows there are four text boxes in a document it doesn’t seem to select each one.:frowning:



local numSelected
local strLayer, strSelected
set newLayer to "Cherry Cake"

tell application "QuarkXPress"
	
	tell document 1
		-- Find if any text boxes are selected
		set numSelected to (count of (text boxes where selected is true))
		if (numSelected is less than 1) then -- if a text box isn't selected, display this warning, and return.
			display dialog "You must select one text box"
			return
		else -- we have text boxes selected	
			make new layer at end with properties {name:newLayer}
			
                       set BoxList to (uniqueID of every text box whose selected is true) as list
			
			repeat with i from 1 to (BoxList count)
				display dialog ("item " & item i of BoxList)
				move text box id i of layer "Default" to beginning of layer newLayer
			end repeat
			
			--Now see how badly it has messed up
			tell layer 2 -- our newly created layer
				repeat with tb from 1 to (text box count)  -- the text boxes on this layer.
					tell text box tb
						set boxName to uniqueID
						display dialog ("look what has happened " & boxName)
					end tell
				end repeat
			end tell
		end if
		
	end tell
end tell -- end of the work with the application


Just experimented with this for a while and couldn’t get it to work. It always moved two of the four boxes.
Then I realized that when you have a group selected you can tell the whole group to do something. Turns out it is a pretty simple script:

set newLayer to "Battenburg Cake"

tell application "QuarkXPress"
	activate
	tell document 1
		tell page 1
			set numSelected to (count of (text boxes where selected is true))
			if (numSelected is less than 1) then
				display dialog "You must select one text box"
				return
			else
				make new layer at end with properties {name:newLayer}
				move current box to beginning of layer newLayer
			end if
		end tell
	end tell
end tell

I’ve only been working with Applescript for three weeks. It just goes to show a little knowledge is a dangerous thing :slight_smile:

Thanks for that, it works fine, and is rather simple. I didn’t suspect that current box implied a group!

By the way I did spot the bug in the second script I submitted. The value of i isn’t the value of the item is it! If we have four items in a list, then the iterator will have a maximum value of 4.

However, that still didn’t get the script working because I got a silly error saying Quark couldn’t get the text box id 38 on document 1 etc: even though it had done that in other versions of the script. I only mention this as extra info.

Northernstar, i think the issue is with “i” being your count and not the ID from the list. This works fine for me.

local numSelected
local strLayer, strSelected
set newLayer to "Cherry Cake"
--
tell application "QuarkXPress"
	tell document 1
		-- Find if any text boxes are selected
		set numSelected to (count of (text boxes where selected is true))
		if (numSelected is less than 1) then -- if a text box isn't selected, display this warning, and return.
			display dialog "You must select one text box"
			return
		else -- we have text boxes selected	
			make new layer at beginning with properties {name:newLayer}
			--
			set BoxList to (every text box whose selected is true) as list
			repeat with thisBox in BoxList
				move thisBox to beginning of layer "Cherry Cake"
			end repeat
		end if
	end tell
end tell -- end of the work with the application

Hi Mark,
Yes, in my own clumsy way that was what I was trying to say.

I’ve not tried your script, so I don’t know if it will work or not, I suspect it will. The only reason for my caution is that Quark has crashed about four times this afternoon.

I do know that some of the scripts I developed before seemed to work initially but the more I tested them, the more they began to produce errors. The logic in them seemed to make sense, but Quark/Applescript’s execution didn’t.

This is the final version that I’ve tested and it seems to work OK.
Thanks to both of you for taking the trouble to reply, and write scripts.



local numSelected
set newLayer to "Chocolate Cake" -- notice the theme here!

tell application "QuarkXPress"
	
	tell document 1
		
		repeat with pg from 1 to (page count)
			tell page pg
				-- Find if any text boxes are selected on this page
				set numSelected to (count of (text boxes where selected is true))
				if (numSelected is greater than 0) then -- A selection has been made. 
					if (not (exists layer newLayer)) then -- If this layer doesn't exist, create it.
						make new layer at end with properties {name:newLayer}
					end if
					move current box to beginning of layer newLayer -- move the selection to the new layer
				end if
			end tell -- end of working with the page
		end repeat -- end of the loop through the pages
		
	end tell -- end of the work with the document
end tell -- end of the work with the application


Northernstar, not sure if this is going to have any effect on your artwork but moving the current selection box will break the stacking order if your text boxes overlap and have background colours. When moving items though layers I use a repeat with move to end to correct this. Here is code that may help you. No document needed.

tell application "QuarkXPress"
	activate
	tell default document 1
		set page height to "100 mm"
		set page width to "100 mm"
		set left margin to "10 mm"
		set right margin to "10 mm"
		set top margin to "10 mm"
		set bottom margin to "20 mm"
		set automatic text box to false
		set guides showing to true
		set guides in front to true
		set horizontal measure to millimeters
		set vertical measure to millimeters
	end tell
	make document at beginning
	tell page 1 of master document 1
		make picture box at beginning with properties ¬
			{name:"M1 Pict", bounds:{"10 mm", "10 mm", "60 mm", "90 mm"}, color:"Magenta"}
		make picture box at beginning with properties ¬
			{name:"M1 Pict", bounds:{"20 mm", "20 mm", "50 mm", "80 mm"}, color:"Yellow"}
		make picture box at beginning with properties ¬
			{name:"M1 Pict", bounds:{"30 mm", "30 mm", "40 mm", "70 mm"}, color:"Black"}
	end tell
	tell page 1 of master document 1
		make text box at beginning with properties ¬
			{name:"M1 Text", bounds:{"60 mm", "10 mm", "80 mm", "90 mm"}, color:"Cyan"}
	end tell
	tell master document 1
		make new page at end
	end tell
	tell page 2 of master document 1
		make picture box at beginning with properties ¬
			{name:"M2 Pict", bounds:{"10 mm", "10 mm", "60 mm", "90 mm"}, color:"Black"}
		make picture box at beginning with properties ¬
			{name:"M2 Pict", bounds:{"20 mm", "20 mm", "50 mm", "80 mm"}, color:"Magenta"}
		make picture box at beginning with properties ¬
			{name:"M2 Pict", bounds:{"30 mm", "30 mm", "40 mm", "70 mm"}, color:"Cyan"}
	end tell
	tell page 2 of master document 1
		make text box at beginning with properties ¬
			{name:"M2 Text", bounds:{"60 mm", "10 mm", "80 mm", "90 mm"}, color:"Yellow"}
	end tell
	set MasterA to object reference of page 1 of master document 1
	set MasterB to object reference of page 2 of master document 1
	tell document 1
		set master spread of (make page at end of spread 1) to MasterB
		repeat with n from 2 to 8
			set master spread of (make page at end) to MasterA
			set master spread of (make page at end of spread n) to MasterB
		end repeat
		make new layer at beginning with properties ¬
			{name:"Master A Text", visible:true, locked:false, color:{0 * 655.35, 255 * 655.35, 255 * 655.35}, keep runaround:true, suppress print:false}
		make new layer at beginning with properties ¬
			{name:"Master A Pics", visible:true, locked:false, color:{255 * 655.35, 0 * 655.35, 255 * 655.35}, keep runaround:true, suppress print:false}
		make new layer at beginning with properties ¬
			{name:"Master B Text", visible:true, locked:false, color:{100 * 655.35, 100 * 655.35, 255 * 655.35}, keep runaround:true, suppress print:false}
		make new layer at beginning with properties ¬
			{name:"Master B Pics", visible:true, locked:false, color:{200 * 655.35, 0 * 655.35, 200 * 655.35}, keep runaround:true, suppress print:false}
		set pageCnt to count of pages
		repeat with i from 1 to count of pages
			set current page to page i
			tell page i
				set boxListA to (every picture box whose name is "M1 Pict")
				repeat with thisBoxA in boxListA
					move thisBoxA to end of layer "Master A Pics"
				end repeat
				set boxListB to (every text box whose name is "M1 Text")
				repeat with thisBoxB in boxListB
					move thisBoxB to end of layer "Master A Text"
				end repeat
				set boxListC to (every picture box whose name is "M2 Pict")
				repeat with thisBoxC in boxListC
					move thisBoxC to end of layer "Master B Pics"
				end repeat
				set boxListD to (every text box whose name is "M2 Text")
				repeat with thisBoxD in boxListD
					move thisBoxD to end of layer "Master B Text"
				end repeat
			end tell
		end repeat
		set current page to page 1
	end tell
end tell