Author Topic: How would you?  (Read 7566 times)

0 Members and 1 Guest are viewing this topic.

Offline AutoFetishist

  • Sr. Member
  • ****
  • Posts: 397
  • Karma: 2
  • Gender: Male
Re: How would you?
« Reply #20 on: May 10, 2010, 08:26:31 AM »
Alright, back in Quark on a Monday...

I made the fixes you recommended.
But I'm noticing that this code

                         set theFirstTabCount to (offset of first text where it is (ASCII character 9) in currentparagraph) as integer

isn't returning the proper value. Its actually giving me the last tab character in the paragraph, not the first.

Weird. Would QX be counting from the right instead of the left?

Offline cdms

  • Full Member
  • ***
  • Posts: 111
  • Karma: 6
  • Gender: Male
Re: How would you?
« Reply #21 on: May 10, 2010, 09:54:34 AM »
I am using 8.0.2 and I can't get your bit to work at all.
However doing it as

tell currentparagraph
set theFirstTabCount to (offset of first text where it is (ASCII character 9)
end tell

seems to work. ???  It should be the same but these are the vagaries of Applescript dictionaries that keep us amused. :P

Offline AutoFetishist

  • Sr. Member
  • ****
  • Posts: 397
  • Karma: 2
  • Gender: Male
Re: How would you?
« Reply #22 on: May 10, 2010, 10:31:51 AM »
AAAAAAAAGH!!

When I try your way, I get "xxx corporation    6,566    abb   $17.00    $32.45     14.8   11.5   0.0%     42.9" (the content of the string)
 doesn't understand the ASCII character message.

Offline cdms

  • Full Member
  • ***
  • Posts: 111
  • Karma: 6
  • Gender: Male
Re: How would you?
« Reply #23 on: May 10, 2010, 12:07:44 PM »
OK, my fault. I have just gone back and read your code again. We need to be more careful with using your currentparagraph variable. This is a copy of the actual paragraph as a string.
try using my last post with tell paragraph i NOT tell currentparagraph.

Offline AutoFetishist

  • Sr. Member
  • ****
  • Posts: 397
  • Karma: 2
  • Gender: Male
Re: How would you?
« Reply #24 on: May 10, 2010, 12:45:13 PM »
Here's a Quark file to test on:
http://dl.dropbox.com/u/499885/test.zip

When you run the script on it, do you get the offset of the first tab being returned as 89?


Offline cdms

  • Full Member
  • ***
  • Posts: 111
  • Karma: 6
  • Gender: Male
Re: How would you?
« Reply #25 on: May 10, 2010, 01:37:46 PM »
Right I can see what is happening now. Quark have hijacked the offset command. This means that using offset inside a tell application "QuarkXpress" block does not behave the same as using it outside that block. This is more than a little naughty. The only thing that comes to mind at the moment is to find the suspect paragraphs. Work out the space positions outside Quark and then go back in to apply the returns.

this looks like;


Code: [Select]
tell document 1 of application "QuarkXPress"
activate
tell current box
tell story 1
set theParagraphs to {}
repeat with i from 1 to count of paragraphs
if name of style sheet of paragraph i is "Company Table" then
set end of theParagraphs to text of paragraph i
end if
end repeat
end tell
end tell
end tell


set thePositionList to {}
repeat with eachParagraph in theParagraphs
set theFirstTabCount to offset of tab in eachParagraph
--if the tab's offset is greater than 35 continue, otherwise skip to next paragraph
if theFirstTabCount < 34 then
set end of thePositionList to 0
else
--chop out characters 35 to the first tab as a separate string
set theSearchBit to (characters 35 thru theFirstTabCount of eachParagraph) as string
--find the offset of the first space in this bit and add back on the "skipped" 34 characters
set end of thePositionList to (offset of " " in theSearchBit) + 34
end if
end repeat

tell document 1 of application "QuarkXPress"
activate
tell current box
tell story 1
set j to 0
repeat with i from 1 to count of paragraphs
if name of style sheet of paragraph i is "Company Table" then
set j to j + 1
if item j of thePositionList is not equal to 0 then
set character (item j of thePositionList) of paragraph i to (ASCII character 7)
end if
end if
end repeat
end tell
end tell
end tell

It appears to work on your document but it is not quick and there is a big wait before anything happens at all.

I'm off home now. See you tomorrow.

Offline AutoFetishist

  • Sr. Member
  • ****
  • Posts: 397
  • Karma: 2
  • Gender: Male
Re: How would you?
« Reply #26 on: May 10, 2010, 04:03:51 PM »
It works!

Intermittently.

I'll write back tomorrow.

Offline AutoFetishist

  • Sr. Member
  • ****
  • Posts: 397
  • Karma: 2
  • Gender: Male
Re: How would you?
« Reply #27 on: May 11, 2010, 08:40:09 AM »
cdms,

Its working!
Of course now I see that unless using a monospace font, attempting to use character count as a method of measuring horizontal distance is going to be a crapshoot.

But it was fascinating at any rate.

What was Quark doing wrong?

Offline Publi-Script

  • Administrator
  • Hero Member
  • *****
  • Posts: 596
  • Karma: 16
  • Gender: Male
  • Automate your Life!
    • Publi-Script.com
Re: How would you?
« Reply #28 on: May 11, 2010, 10:52:45 AM »
You can calculate EXACTLY how wide variable width text will be by asking quark to return the width of each characters.

Here some food for tought:
Quote
001     property CharWidth : {}
002     property AsciiSpace : 32

003     my GetCharWidths() -- Run this only once prior to running your final script
004     set ColumnTexts to my getColumnText("Trailers gossips noisily. Five putrid wart hogs quite annoyingly fights umpteen progressive tickets. Putrid chrysanthemums drunkenly kisses one mat, even though umpteen aardvarks grew up very quickly, but two purple Klingons bought umpteen trailers. Five progressive tickets auctioned off dwarves.
005     Darin slightly drunkenly kisses umpteen very quixotic poisons, because five dogs annoyingly fights umpteen dwarves. The schizophrenic cat gossips, then umpteen subways auctioned off Springfield. The chrysanthemum gossips.
006     Five bureaux quite cleverly tickled Jupiter, because two speedy elephants abused umpteen schizophrenic subways, and two aardvarks laughed, because umpteen speedy bureaux comfortably untangles two silly chrysanthemums, but one partly obese orifice tickled umpteen televisions, because schizophrenic chrysanthemums bought Darin.
007     Five mostly quixotic mats laughed. Putrid subways grew up cleverly, and umpteen obese Klingons gossips partly quickly. Two aardvarks towed the progressive trailer. One Macintosh gossips. Five sheep grew up, even though Quark tickled Tokyo, although dwarves mostly drunkenly perused the quixotic wart hogs. Almost bourgeois poisons laughed, but umpteen partly speedy botulisms tastes the mat. Jabberwockies grew up comfortably. Two quite schizophrenic poisons kisses umpteen cats. Two chrysanthemums sacrificed five almost quixotic lampstands, then the televisions marries mostly schizophrenic mats. The almost bourgeois pawnbrokers grew up drunkenly, however five progressive Jabberwockies tastes one orifice, and umpteen irascible dogs bought two mostly purple subways. Umpteen sheep gossips, because the orifices quickly abused one quixotic ticket. Two dwarves drunkenly bought umpteen trailers. Two poisons gossips almost noisily. One bourgeois Macintosh sacrificed Mark.
008     Umpteen botulisms tastes two chrysanthemums. One partly putrid fountain marries umpteen Macintoshes, yet two speedy fountains grew up mostly comfortably. Umpteen dwarves perused progressive elephants, although the quite bourgeois aardvarks gossips, and speedy cats noisily tastes five schizophrenic Klingons.", 300)

009     set OldDelims to AppleScript's text item delimiters
010     set AppleScript's text item delimiters to {return}
011     set TheText to ColumnTexts as string
012     set AppleScript's text item delimiters to OldDelims

013     tell application "QuarkXPress Passport 7.x"
014          activate
015          set DocName to name of document 1
016          tell document DocName
017               tell text box 1
018                    set story 1 to TheText
019               end tell
020          end tell
021     end tell

022     on GetCharWidths()
023          tell application "QuarkXPress Passport 7.x"
024               activate
025               set DocName to name of document 1
026               tell document DocName
027                    tell text box 1
028                         -- Pad the beginning of the list to allow direct acces with asciiNum later
029                         repeat with a from 1 to 31
030                              set end of CharWidth to 0
031                         end repeat
032                         
033                         -- Populate the rest of the list with actual charcater width of the cuurent font
034                         repeat with a from 32 to 255
035                              set story 1 to ASCII character a
036                              set TheWidth to width of story 1
037                              set end of CharWidth to TheWidth as real
038                         end repeat
039                    end tell
040               end tell
041          end tell
042     end GetCharWidths

043     on getTextWidth(TheText)
044          -- TheText: The string to calculate the total width of
     
045          set TextWidth to 0
046          repeat with ThisChar in TheText
047               set TextWidth to TextWidth + (item (ASCII number of ThisChar) of CharWidth)
048          end repeat
049          return TextWidth
050     end getTextWidth

051     on getColumnText(TheText, ColumnWidth)
052          local TextColumns, AllWords, ThisColumnText, CurWidth
053          -- TheText: The text to separate into visual columns
054          -- ColumnWidth: the width of the column in pts
     
055          set TextColumns to {}
056          -- First, let's separate our text into words
057          set AllWords to my GetTextItem(TheText, space, 0)
     
058          set CurWidth to 0
059          set ThisColumnText to ""
060          repeat with w from 1 to count of AllWords
061               set ThisWordWidth to my getTextWidth(item w of AllWords)
062               if (CurWidth + ThisWordWidth) > ColumnWidth then
063                    set end of TextColumns to ThisColumnText
064                    set ThisColumnText to ""
065                    set CurWidth to 0
066                    set w to w - 1
067               else
068                    set CurWidth to CurWidth + (item AsciiSpace of CharWidth) + ThisWordWidth
069                    set ThisColumnText to ThisColumnText & space & (item w of AllWords) as text
070               end if
071          end repeat
     
072          return TextColumns
073     end getColumnText

074     on GetTextItem(ThisString, ThisDelim, ThisItem)
075          -- ThisString -> String to look in
076          -- ThisDelim -> Text element that delimit the string
077          -- ThisItem -> Number of the element to return
078          copy the text item delimiters to OldDelims
079          set the text item delimiters to ThisDelim
080          if class of ThisItem is list then
081               set fromItem to (item 1 of ThisItem) as integer
082               set toitem to (item 2 of ThisItem) as integer
083               set arrItem to (text items fromItem thru toitem of ThisString)
084          else
085               set arrItem to every text item of ThisString
086          end if
087          set the text item delimiters to OldDelims
     
     
088          if class of ThisItem is list then
089               return arrItem as text
090          else
091               if ThisItem is not 0 then
092                    return (item ThisItem of arrItem) as text
093               else
094                    return arrItem -- return every items
095               end if
096          end if
097     end GetTextItem

This script will break your original TEXT into paragraphs of a certain length based on the supplied character widths.

To make use of this script:
  • first created a new Quark document containing a single text box.
  • Set the font and size of the text to the same attributes of your table text
  • Run ONLY the GetCharWidths handler of this script
  • Copy the resulting CharWidths property (the property call and its current values) to your final script
  • Copy the other handlers and the AsciiSpace property to your final script


This script returns a list of lines of text, in this example, On line 010, I inserted a paragraph return between each line but you can put whatever you want.

HTH
Michel Lemieux
Forum Administrator
-------------------------
OS X 10.6.4
Quark 6.5 / 7.5 / 8.1.6
Adobe Design Premium CS / CS2 / CS3 / CS4 / CS5
Script Debugger 4.5

Offline cdms

  • Full Member
  • ***
  • Posts: 111
  • Karma: 6
  • Gender: Male
Re: How would you?
« Reply #29 on: May 11, 2010, 10:53:47 AM »
Quark documents use 'offset' as a property of various elements to do with where things are aligned on a grid. Hence, when we are inside the tell "QuarkXpress" block, and try to use offset as an applescript command, Quark is grabbing it and trying to return the relative position on the base grid of where ever the character is that we have found.
This is really not good.

The works 'intermittently' affect you were seeing is due to some of the exceptions we discussed earlier. The main culprit being no space in the string between position 35 and the tab character. Also a slight problem was arising where the tab was character 36. Just for completeness, the following code resolves the last option and has performance enhancements by storing the paragraph numbers and accessing them directly the second time, rather than re-looping through then all again.

Code: [Select]
tell document 1 of application "QuarkXPress"
activate
tell current box
tell story 1
set theParagraphs to {}
repeat with i from 1 to count of paragraphs
if name of style sheet of paragraph i is "Company Table" then
set end of theParagraphs to {text of paragraph i, i}
end if
end repeat
end tell
end tell
end tell


set thePositionList to {}
repeat with eachPair in theParagraphs
set eachparagraph to item 1 of eachPair
set theFirstTabCount to offset of tab in eachparagraph
--if the tab's offset is greater than 35 continue, otherwise skip to next paragraph
if theFirstTabCount > 35 then
--chop out characters 35 to the first tab as a separate string
set theSearchBit to (characters 35 thru theFirstTabCount of eachparagraph) as string
--find the offset of the first space in this bit and add back on the "skipped" 34 characters
set theSpacePosition to ((offset of " " in theSearchBit) + 34)
-- check not 34 as this means tab was next character (or no space found)
if theSpacePosition > 35 then
set end of thePositionList to {theSpacePosition, (item 2 of eachPair)}
end if
end if
end repeat

tell document 1 of application "QuarkXPress"
activate
tell current box
tell story 1
set j to 0
repeat with eachEntry in thePositionList
set theSpacePosition to item 1 of eachEntry
set theParagraph to item 2 of eachEntry
set character theSpacePosition of paragraph theParagraph to (ASCII character 7)
end repeat
end tell
end tell
end tell

Maybe a table?  :P