Saturday, January 16, 2021

#1 2021-01-08 06:59:31 am

farmingmoney
Member
Registered: 2020-01-13
Posts: 20

paragraph color for a specific font size in the Mac page document

I want to change the paragraph color for a specific font size in the Mac page document.
This method works fine, but it takes quite a bit of time to process if there are many document pages.
I would like to know if this method is best.
Could it be further optimized?

Applescript:

tell application "Pages"
   tell document 1
       if document body is false then error number -128
       repeat with pagenum from 1 to count of page
           tell page pagenum
               set myList to shapes
               repeat with thisShape in myList
                   repeat with p in every paragraph of object text of thisShape
                       -- if font size is 12pt, then change the font color to red
                       if (size of p) is equal to 12 then
                           set the color of p to {65535, 0, 0}
                       end if
                   end repeat
               end repeat
           end tell
       end repeat
   end tell
end tell

Offline

 

#2 2021-01-08 08:29:43 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1537

Re: paragraph color for a specific font size in the Mac page document

1 repeat loop is sufficient:

Applescript:


tell application "Pages" to tell document 1
   repeat with theShape in (get shapes)
       tell (every paragraph of object text of theShape whose size is 12)
           set its color to {65535, 0, 0}
       end tell
   end repeat
end tell

Last edited by KniazidisR (2021-01-08 08:32:53 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2021-01-08 06:03:38 pm

farmingmoney
Member
Registered: 2020-01-13
Posts: 20

Re: paragraph color for a specific font size in the Mac page document

KniazidisR wrote:

1 repeat loop is sufficient:

Applescript:


tell application "Pages" to tell document 1
   repeat with theShape in (get shapes) — :rolleyes: timeout occur in this line
       tell (every paragraph of object text of theShape whose size is 12)
           set its color to {65535, 0, 0}
       end tell
   end repeat
end tell


Awesome!!! Your source code runs much faster(x7.2) than the code I wrote.
However, if the pages document exceeds 1000 pages, the following error occurs.

error "Pages got an error: AppleEvent timed out." number -1712

Is there a good solution?

Offline

 

#4 2021-01-09 06:01:27 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1537

Re: paragraph color for a specific font size in the Mac page document

farmingmoney wrote:

However, if the pages document exceeds 1000 pages, the following error occurs.

error "Pages got an error: AppleEvent timed out." number -1712

Is there a good solution?


Default value for timeout of each code line is 2 minutes (120 seconds). The solution is changing the default value to some bigger timeout:

Applescript:


with timeout of 6000 seconds -- 100 minutes, for example
   tell application "Pages" to tell document 1
       repeat with theShape in (get shapes)
           tell (every paragraph of object text of theShape whose size is 12)
               set its color to {65535, 0, 0}
           end tell
       end repeat
   end tell
end timeout

Last edited by KniazidisR (2021-01-09 06:33:27 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2021-01-10 06:57:07 am

farmingmoney
Member
Registered: 2020-01-13
Posts: 20

Re: paragraph color for a specific font size in the Mac page document

KniazidisR wrote:


Applescript:


with timeout of 6000 seconds -- 100 minutes, for example
   tell application "Pages" to tell document 1
       repeat with theShape in (get shapes) — it goes to lockup state
           tell (every paragraph of object text of theShape whose size is 12)
               set its color to {65535, 0, 0}
           end tell
       end repeat
   end tell
end timeout


Your script falls into an infinite loop
A document size of up to 100 pages is fine, but if it is more than 200 pages, this phenomenon is found.
If I find the cause, I will write it again here.
KniazidisR! Your answers have been a great help to me. Thank you!

Offline

 

#6 2021-01-10 07:36:08 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1537

Re: paragraph color for a specific font size in the Mac page document

"it goes to lockup state"

It is not clear what you mean by this
The loop in the script can't be infinite, since the number of shapes in the document is finite. You should probably be patient and wait for the script to finish.

It is unlikely that you will need as much time as in the specified timeout, but it can be several minutes if there are a lot of shape objects.

I tested on my Mac with 2240 shapes. Getting this shapes took on my Mac about 5 min 16 sec

It's hard to do anything about it. I just want to point out that getting the shapes list only takes 1/4 of the entire script execution time.

In terms of paragraph handling, I would improve the script aesthetically by providing a progress bar to loop. So that you no longer think that the script is "frozen":

Applescript:


with timeout of 600 seconds -- 10 minutes, for example
   -- this tooks about 5 minutes for 2000 shapes
   tell application "Pages" to tell document 1 to set theShapes to shapes
end timeout

set shapesCount to count of theShapes
set progress total steps to shapesCount

using terms from application "Pages"
   repeat with i from 1 to shapesCount
       set theShape to item i of theShapes
       tell me to set progress description to "Shape " & i & " of " & shapesCount
       tell (every paragraph of object text of theShape whose size is 12)
           set its color to {65535, 0, 0}
       end tell
       tell me to set progress completed steps to i
   end repeat
end using terms from

Last edited by KniazidisR (2021-01-10 09:24:20 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#7 2021-01-11 05:08:06 am

farmingmoney
Member
Registered: 2020-01-13
Posts: 20

Re: paragraph color for a specific font size in the Mac page document

KniazidisR wrote:


with timeout of 600 seconds -- 10 minutes, for example
    -- this tooks about 5 minutes for 2000 shapes
    tell application "Pages" to tell document 1 to set theShapes to shapes — roll timeout here!!!!!
end timeout



I'm not sure either.
But, what is certain is the fact that a timeout occurs
I sent a sample file to your email address. I wish you refer to that
I apologize if it was unpleasant for me to send you an email without your consent

When testing with the sample file I sent, you need to test the font size as 10 instead of 12 as shown below.
tell (every paragraph of object text of theShape whose size is 10)

Thank you.

Offline

 

#8 2021-01-12 01:19:14 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1537

Re: paragraph color for a specific font size in the Mac page document

Hello farmingmoney again.

I received your sample by email. Unfortunately my Pages couldn't open it. I do not know why. But I found the best way to solve your problem. Instead of getting a list of shapes, it's better to handle each page directly. This makes it possible to avoid a timeout.

And, as I found, counting by pages is 3 times faster than counting by shapes. So, the following script is 3 times faster than my previous suggestions, and  at least 6 times faster than your original script

I again leave the progress bar for user friendliness of the interface. It will also help the user to see how many pages are in total and approximately how long the script will need:

Applescript:

-- about 7 minutes for 400 pages with 2000 shapes
tell application "Pages" to tell document 1 to set thePages to pages

set countPages to count thePages
set progress total steps to countPages

repeat with i from 1 to countPages
   using terms from application "Pages"
       tell (item i of thePages)
           set (color of every paragraph of object text of every shape whose size is 12) to {65535, 0, 0}
       end tell
   end using terms from
   set progress completed steps to i
end repeat

And, the shortest working solution, but without progress bar is this:

Applescript:


tell application "Pages" to tell document 1
   with timeout of 6000 seconds
       set (color of paragraphs of object text of shapes of pages whose size is 12) to {65535, 0, 0}
   end timeout
end tell

Last edited by KniazidisR (2021-01-12 09:41:51 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#9 2021-01-12 09:00:52 am

farmingmoney
Member
Registered: 2020-01-13
Posts: 20

Re: paragraph color for a specific font size in the Mac page document

Hello KniazidisR
Your new code helps with this problem right now. No more time-outs
I learned a lot of things through this case
The way to debug using "Displaying Progress" was particularly impressive
Your active activities on macscripter.net has been a great help to all.
I give you respect and thanks  wink

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)