• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

AS: get all textframes within selection

Advocate ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

Hey, anyone still scripting with applescript? Your help is needed:


I have a selection of items, which can be everything…even groups.
If I select a textbox and a group, I with
set myStuff to all page items of selection

display dialog "" & count of myStuff
get 2 as result. 
If I ungroup the groups before, i get 9 as result.

 

How do i target every item within a selection, whithout having to recursesive cycle thru every group within a group within a contain which is anchored in a textframe which is pasted in a oval which is a group of a group of a group…?

 

 




TOPICS
Scripting

Views

836

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

Hi Jan,

I can give you an idea with ExtendScript.

 

1. You need to loop all selected items.

2. You need to loop all allPageItems arrays of all selected items.

 

Regards,
Uwe Laubender

( ACP )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

all page items gets groups and items inside of groups, so I do a single repeat and check for the class. This displays the number of rectangles in a selection containing multiple nested groups.

 

 

tell application "Adobe InDesign 2020"
	set api to all page items of selection
	set cnt to 0
	repeat with x in api
		if class of x is rectangle then
			--do something to the rectangles
			set cnt to cnt + 1
		end if
	end repeat
	display dialog "There are " & cnt & " rectangles in the selection"
end tell

 

 

Here there are 10 rectangles, but the count of all page items is 14

Screen Shot 15.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

Hey Rob and Uwe,

thank you both for your help! Rob is on the right track here. But if your selection is more than 1, or a textframe in oval, the result is 0.

 

I tried to cycle thru the items of selection if > 1, but that doesnt work either.

	if (count of api) > 1 then
		repeat with y in api
			set sub_api to all page items of item y of api
			repeat with x in sub_api
				if class of x is text frame then
					--do something to the rectangles
					set cnt to cnt + 1
				end if
			end repeat
		end repeat
	end if

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

Sorry, I’m not taking the time to thoroughly testing before I post.

 

If the selection is not a group, but a mix of page items including groups then this should find the text frames within the selection:

 

tell application "Adobe InDesign 2020"
	set cnt to 0
	set sel to selection
	repeat with x in sel
		if class of x is group then
			set api to all page items of x
			repeat with i in api
				if class of i is text frame then
					set cnt to cnt + 1
				end if
			end repeat
		else if class of x is text frame then
			set cnt to cnt + 1
		end if
	end repeat
	display dialog "There are " & cnt & " text frames in the selection"
end tell

 

 

 

Screen Shot 18.png

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

Depending on what you are trying to do to the page items, a try statement can also work:

 

tell application "Adobe InDesign 2020"
	set api to all page items of selection
	repeat with x in api
		try
			set fill color of x to "Black"
		end try
	end repeat
end tell

 

Here there is a mix of groups, rectangles, and text frames, and the try statement skips the groups because a group doesn‘t have a fill color property:

Screen Shot 16.pngScreen Shot 17.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

To make it a bit clearer, where i want to head:

I just want to grep-search my seletion. The selection can be everything – groups, just a text frame, a text frame in a container, maybe another group, a combination of group and no grouped object – you never know if your textframe is like a Matryoshka or if theres any. 

 

But if everyone screams "do javascript" I must say, I cant wrap the rest of the script into JS, im just not good enough to start that from scratch – so I have to rely on applescript, to make further additions.

 

I have a script that takes the seletion, then tries to find (via grep) a specific text \d%, then throw some dialogs asking for LastPrice and CurrentPrice, and then it change the text. This does only work for a text frame or a level 1 group.
Today I had the idea to advance that script: If I select a price-textbox and the corresponding product/text-group, I can pre-read the PriceBefore (\d+|\d+,\d{2})€(?=~>) to prefill the default text of the dialog, and being able to edit CurrPrice and PrePrice in that process. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

ScreenFlow.gif

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

One note on the first script I posted—it would only work if the selection is a group with nested groups.

 

To get all the text frames within any selection something like this, where I have a function to handle the grep search: my GrepSearch("\\t+", "") The 2 parameters are the search and change grep strings, this one would find and delete tabs. Note that backslashes have to be escaped with backslashes

 

EDIT: The tell txt block also has to be in a try statement

 

tell application "Adobe InDesign 2020"
	
	set sel to selection
	repeat with x in sel
		set api to all page items of x
		if class of x is group then
			set api to all page items of x
			repeat with i in api
				try
					set txt to parent story of i
				end try
			end repeat
		else
			try
				set txt to parent story of x
			end try
		end if
		try
			tell txt
				--in AppleScript back slashes have to be escaped with a back slash
				--so it is "\\t+" and not "\t+"
				my GrepSearch("\\t+", "")
			end tell
		end try
	end repeat
end tell


--a grep search handler function
--parameters are the grep search string and the grep change string
on GrepSearch(f, c)
	tell application "Adobe InDesign 2020"
		set find grep preferences to nothing
		set change grep preferences to nothing
		set find what of find grep preferences to f
		set change to of change grep preferences to c
		change grep
	end tell
end GrepSearch

 

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2020 Apr 01, 2020

Copy link to clipboard

Copied

Also, do you really need to work on selections? Using the grep handler function you can string together has many grep searches as you want. This runs 4 searches on the entire document:

 

 

 

tell application "Adobe InDesign 2020"
	activate

	--removes any white space at the start of a paragraph including multiple returns
	my GrepSearch("^\\s*", "")
	--replaces multiple spaces with one space
	my GrepSearch("\\p{zs}{2,}", "\\s")
	--replaces soft return with a hard return
	my GrepSearch("\\s?\\n", "\\r")
	--replaces non breaking spaces with spaces
	my GrepSearch("~s|~S", "\\s")
	
end tell

on GrepSearch(f, c)
	tell application "Adobe InDesign 2020"
		set find grep preferences to nothing
		set change grep preferences to nothing
		set find what of find grep preferences to f
		set change to of change grep preferences to c
		change grep
	end tell
end GrepSearch

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

First of all, heavy thanks to you Rob, this seems very promising!

But im still struggeling getting this to run in my script. The problem is, i dont just want to find/change – I need to read and change the values later…and I cant get a grip on the results. 

I modiefied the function, but that doesnt work somehow just as simple as I thought.

on GrepFound(g)
	tell application "Adobe InDesign CC 2018"
		set f to {}
		set find grep preferences to nothing
		set find what of find grep preferences to g
		set f to find grep
                return f
	end tell
end GrepFound

 

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

Also I guess set XYZ to find grep does not exclusivly search the var txt.
If I just have a page, with no matching text to my grep, I actually do get something like this…

find grep current application

--> {text from character 8 to character 10 of text flow id 5418 of document id 1, text from character 8 to character 10 of text flow id 5493 of document id 1, text from character 8 to character 10 of text flow id 5546 of document id 1, text from character 8 to character 10 of text flow id 5719 of document id 1, text from character 8 to character 10 of text flow id 5829 of document id 1, text from character 8 to character 10 of text flow id 5973 of document id 1, text from character 8 to character 10 of text flow id 6301 of document id 1, text from character 8 to character 10 of text flow id 6403 of document id 1, text from character 67 to character 70 of text flow id 6459 of document id 1, text from character 8 to character 10 of text flow id 6587 of document id 1, text from character 8 to character 10 of text flow id 6659 of document id 1, text from character 120 to character 123 of text flow id 6692 of document id 1, text from character 219 to character 222 of text flow id 6692 of document id 1, text from character 25 to character 28 of text flow id 6817 of document id 1, text from character 8 to character 10 of text flow id 6871 of document id 1, text from character 8 to character 10 of text flow id 7005 of document id 1, text from character 8 to character 10 of text flow id 7481 of document id 1, text from character 8 to character 10 of text flow id 7542 of document id 1, text from character 8 to character 10 of text flow id 7629 of document id 1, text from character 8 to character 10 of text flow id 8143 of document id 1, text from character 8 to character 10 of text flow id 8224 of document id 1, text from character 8 to character 10 of text flow id 8314 of document id 1, text from character 8 to character 10 of text flow id 8453 of document id 1, text from character 8 to character 10 of text flow id 8877 of document id 1, text from character 8 to character 10 of text flow id 9149 of document id 1, text from character 8 to character 10 of text flow id 9276 of document id 1, text from character 36 to character 39 of text flow id 9368 of document id 1, text from character 8 to character 10 of text flow id 9713 of document id 1, text from character 33 to character 36 of text flow id 9831 of document id 1, text from character 38 to character 41 of text flow id 10108 of document id 1, text from character 8 to character 10 of text flow id 10208 of document id 1, text from character 8 to character 10 of text flow id 10467 of document id 1, text from character 23 to character 26 of text flow id 10537 of document id 1, text from character 19 to character 22 of text flow id 10588 of document id 1, text from character 8 to character 10 of text flow id 10622 of document id 1, text from character 8 to character 10 of text flow id 10733 of document id 1, text from character 22 to character 25 of text flow id 10903 of document id 1, text from character 8 to character 10 of text flow id 11077 of document id 1, text from character 27 to character 30 of text flow id 11107 of document id 1, text from character 8 to character 10 of text flow id 11492 of document id 1, text from character 36 to character 39 of text flow id 11515 of document id 1, text from character 8 to character 10 of text flow id 11692 of document id 1, text from character 8 to character 10 of text flow id 11767 of document id 1, text from character 8 to character 10 of text flow id 11819 of document id 1, text from character 8 to character 10 of text flow id 11860 of document id 1, text from character 8 to character 10 of text flow id 12001 of document id 1, text from character 8 to character 10 of text flow id 12092 of document id 1, text from character 75 to character 78 of text flow id 12147 of document id 1, text from character 8 to character 10 of text flow id 12326 of document id 1, text from character 78 to character 81 of text flow id 12354 of document id 1, text from character 8 to character 10 of text flow id 12544 of document id 1, text from character 8 to character 10 of text flow id 12682 of document id 1, text from character 8 to character 10 of text flow id 12818 of document id 1, text from character 8 to character 10 of text flow id 13126 of document id 1, text from character 126 to character 129 of text flow id 13317 of document id 1, text from character 8 to character 10 of text flow id 13411 of document id 1, text from character 84 to character 86 of text flow id 13592 of document id 1, text from character 100 to character 102 of text flow id 13592 of document id 1, text from character 8 to character 10 of text flow id 13678 of document id 1, text from character 8 to character 10 of text flow id 13735 of document id 1, text from character 84 to character 87 of text flow id 14061 of document id 1, text from character 257 to character 260 of text flow id 14061 of document id 1, text from character 293 to character 296 of text flow id 14061 of document id 1, text from character 8 to character 10 of text flow id 14192 of document id 1, text from character 33 to character 36 of text flow id 14335 of document id 1, text from character 101 to character 104 of text flow id 14335 of document id 1, text from character 8 to character 10 of text flow id 14467 of document id 1, text from character 68 to character 71 of text flow id 14521 of document id 1, text from character 104 to character 107 of text flow id 14521 of document id 1, text from character 8 to character 10 of text flow id 14615 of document id 1, text from character 42 to character 45 of text flow id 14650 of document id 1, text from character 66 to character 69 of text flow id 14650 of document id 1, text from character 80 to character 83 of text flow id 14698 of document id 1, text from character 8 to character 10 of text flow id 14808 of document id 1, text from character 8 to character 10 of text flow id 15017 of document id 1, text from character 111 to character 114 of text flow id 15083 of document id 1, text from character 8 to character 10 of text flow id 15196 of document id 1, text from character 56 to character 59 of text flow id 15273 of document id 1, text from character 91 to character 94 of text flow id 15273 of document id 1, text from character 8 to character 10 of text flow id 15349 of document id 1, text from character 54 to character 57 of text flow id 15428 of document id 1, text from character 78 to character 81 of text flow id 15428 of document id 1, text from character 8 to character 10 of text flow id 15516 of document id 1, text from character 81 to character 84 of text flow id 15581 of document id 1, text from character 154 to character 157 of text flow id 15581 of document id 1, text from character 8 to character 10 of text flow id 15656 of document id 1, text from character 8 to character 10 of text flow id 15767 of document id 1, text from character 66 to character 68 of text flow id 15790 of document id 1, text from character 81 to character 83 of text flow id 15790 of document id 1, text from character 8 to character 10 of text flow id 15987 of document id 1, text from character 57 to character 60 of text flow id 16105 of document id 1, text from character 59 to character 62 of text flow id 16142 of document id 1, text from character 8 to character 10 of text flow id 16253 of document id 1, text from character 8 to character 10 of text flow id 16316 of document id 1}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

Your grep handler is returning a list of found items, so you’ll have to loop through the returned list in order to do anything with the items. Also, if you want to limit the search to a story, I think you’ll have to pass in the text:

 

 

tell application "Adobe InDesign 2020"
	
	set sel to selection
	set txt to parent story of item 1 of sel
	set f to my GrepFound("hello", txt)
	
	repeat with i from 1 to count of f
		set contents of item i of f to "there"
	end repeat
end tell


on GrepFound(g, t)
	tell application "Adobe InDesign 2020"
		set find grep preferences to nothing
		set find what of find grep preferences to g
		tell t
			find grep
		end tell
	end tell
end GrepFound

 

Screen Shot 33.pngScreen Shot 34.png

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

Im on it, still struggeling with groups tho, but overall it seems to work/come to life.
Ill report tomorrow, have a nice evening Rob!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 02, 2020 Apr 02, 2020

Copy link to clipboard

Copied

LATEST

One thing to note about groups is the all page items property gets all items inside of a group including other groups and their page items. So if you select 3 items, loop through the 3 items, and check to find a group class, you can use all page items to get everything inside of that group—it doesn’t matter how many nested groups there are inside of that group.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines