script to delete all items on the pasteboard

working on a script to delete all items on the pasteboard of an InDesign file. I have the snippet of code as follows but it will not get all the page items in document and I just realized this will only work with a page that is 8 1/2 x 11. I need it to work on any sized page. Can anyone please help me with this???

tell myDocument
set allPageItems to all page items as list
set countItems to count of items in allPageItems
display dialog “There are " & countItems & " items in the document”
repeat with i from 1 to countItems
display dialog “Item #” & i
try
set theObject to page item i of myDocument
set theClass to class of theObject
set myBounds to geometric bounds of theObject
select page item i --of myDocument
repeat with x from 1 to 4
set thisBound to item x of myBounds
–display dialog "Item " & x & " of bounds is " & thisBound
if thisBound is less than 0 then --[y1, x1, y2, x2]
display dialog "Geometric bounds of item " & i & " includes " & thisBound
delete page item i
exit repeat
else if x is 2 then --[y1, x1, y2, x2]
if thisBound is greater than 51 then
display dialog "Geometric bounds of item (x1) " & i & " includes " & thisBound
delete page item i
exit repeat
end if
else if x is 4 then --[y1, x1, y2, x2]
if thisBound is greater than 51 then
display dialog "Geometric bounds of item (x2) " & i & " includes " & thisBound
delete page item i
exit repeat
end if
end if
end repeat
on error
display dialog “Could not set theObject to page item i of myDocument.”
end try
end repeat
end tell

I have figured out how to apply this to different sized pages (snippet is below). Now I just have to figure out why it won’t get page items. I am getting error numbers 1728 & 128.

tell document preferences of myDocument
	set pageWidth to page width
	set pageHeight to page height
	display dialog "Page width is " & pageWidth & " and page height is " & pageHeight
end tell

else if x is 2 then --[y1, x1, y2, x2]
	if thisBound is greater than pageWidth or pageHeight then
		display dialog "Geometric bounds of item (x1) " & i & " includes " & thisBound
		delete page item i
		exit repeat
	end if
else if x is 4 then --[y1, x1, y2, x2]
	if thisBound is greater than pageWidth or pageHeight then
		display dialog "Geometric bounds of item (x2) " & i & " includes " & thisBound
		delete page item i
		exit repeat
	end if
end if

Hi. Your task is actually simpler than the method you are trying, as the pasteboard is an element”a spread.

tell application "Adobe InDesign CS3"'s document 1 to delete (page items whose parent's class is spread)

Thanks Marc! But that deletes everything on the page as well! I just want to delete any items on the pasteboard (the area around the page that doesn’t print). But your script gave me an idea – I tried the script below but that didn’t work either. Oh, well, back to the drawing board!

forgot to put the code I used that didn’t work:

tell myDocument
	delete (page items whose parent's class is not page)
end tell

Hmm. That should’ve worked. Are any items grouped across the page boundary?

No, none of the items were grouped at all. That snippet just deleted everything, including the items on the page.

Please make a new document with one item in the pasteboard and one on the page. I would be interested to know what is returned from this statement:

tell application "Adobe InDesign CS3"'s document 1 to {all page items, page items's parent}

The fallback solution is to negatively check each item against the page area.

set withoutList to {}

tell application "Adobe InDesign CS3"'s document 1
	set theBounds to page 1's bounds
	set thelist to page items
	repeat with anitem in thelist
			tell anitem to if not (geometric bounds's item 1 ≥ theBounds's item 1 and ¬
				geometric bounds's item 2 ≥ theBounds's item 2 and ¬
				geometric bounds's item 3 ≤ theBounds's item 3 and ¬
				geometric bounds's item 4 ≤ theBounds's item 4) then
				set withoutList's end to its contents
			end if
	end repeat
	delete withoutList
end tell

Marc,

I ran this code and nothing happened.


	tell myDocument
		select {all page items, page items's parent}
	end tell

This was the result:


tell application "Adobe InDesign CS6"
	activate
	get active document
		--> document id 4
	select {all page items of document id 4, parent of every page item of document id 4}
		--> {}
end tell
Result:
{}

Marc,

Your other suggestion is basically what I’m doing. I am selecting each page item and getting its geometric bounds and determining if the object is on the pasteboard. The problem I’m having is that I cannot select each item. I am getting an error on some of them: error number -1728 – this error is “Can’t get «script».”

I’m going to try some troubleshooting this afternoon. Scripting is not my full-time job (unfortunately), so I have to work on it in my spare time.

I will keep you posted on my progress.

Thanks!

You altered the code I provided. It’s expected that the result of my one-liner is a list, which is not selectable. Please try both sets of code as written. Something has to be returned from the instruction, and I can’t help you without knowing what this is”even if it’s an error message.

Marc,

I’m so sorry! I thought I had the gist of what your code was doing and so I made some changes. So I ran this code:


tell application "Adobe InDesign CS6"'s document 1 to {all page items, page items's parent}

tell application "Adobe InDesign CS6"'s document 1
	set theBounds to page 1's bounds
	set thelist to page items
	repeat with anitem in thelist
		tell anitem to if not (geometric bounds's item 1 ≥ theBounds's item 1 and ¬
			geometric bounds's item 2 ≥ theBounds's item 2 and ¬
			geometric bounds's item 3 ≤ theBounds's item 3 and ¬
			geometric bounds's item 4 ≤ theBounds's item 4) then
			set withoutList's end to its contents
		end if
	end repeat
	delete withoutList
end tell

and got this result:


tell application "Adobe InDesign CS6"
	get all page items of document 1
		--> {rectangle id 219 of spread id 198 of document id 8, rectangle id 218 of spread id 198 of document id 8, rectangle id 217 of spread id 198 of document id 8}
	get parent of every page item of document 1
		--> {spread id 198 of document id 8, spread id 198 of document id 8, spread id 198 of document id 8}
	get bounds of page 1 of document 1
		--> {0.0, 0.0, 11.0, 8.5}
	get every page item of document 1
		--> {rectangle id 219 of spread id 198 of document id 8, rectangle id 218 of spread id 198 of document id 8, rectangle id 217 of spread id 198 of document id 8}
	get item 1 of geometric bounds of rectangle id 219 of spread id 198 of document id 8
		--> 2.631944444444
	get item 2 of geometric bounds of rectangle id 219 of spread id 198 of document id 8
		--> -4.763055555556
Result:
error "The variable withoutList is not defined." number -2753 from "withoutList"

I’m sorry Marc. I had missed the line where you set withoutList to {}. I added it and it worked!!! Thank you so much!!

Hello karensink

This list is fair enough to give us a tool allowing us to grab the posted script without missing something.

No need to copy-paste which is prone to errors. Just click the button [Open this Scriplet in your Editor:] to get . the complete script in our Editor.

Using this tool is a good way to spare asker’s and helper’s time.

Yvan KOENIG running El Capitan 10.11.6 in French (VALLAURIS, France) jeudi 25 aout 2016 13:51:09

Thank you, Yvan!! I was not aware of this tool. I will definitely use it in the future.

I found a way to select all the items on the pasteboard of the first spread. I noticed that items which are fully on the pasteboard have a parent page property of nothing. I tried using:


set ThePasteboard to every page item whose parent page is nothing

but this always resulted in an empty list. So I made a list of every page item whose parent page has a class of page (i.e. has a parent page). Then I made a new list of every page item that is not in the first list.


tell application "Adobe InDesign 2020"
	tell document 1
		tell spread 1
			set ThePageArea to every page item whose class of parent page is page
			set AllPageItems to every page item
			set ThePasteboard to {}
			repeat with i from 1 to (count AllPageItems)
				set ThisPageItem to item i of AllPageItems
				if ThisPageItem is not in ThePageArea then
					set the end of ThePasteboard to ThisPageItem
				end if
			end repeat
			select ThePasteboard
		end tell
	end tell
end tell

I tried missing value but this also resulted in an empty list. It may be a bug or limitation but the workaround does the job. Indesign can select a list when the list contains references to page items. The command “select ThePasteboard” (ThePasteboard is the list variable) results in all the objects on the pasteboard of spread 1 being selected.

Hi, David. Your version is significantly newer than mine or the OP’s, so the parent page property may not have existed when this question was posted, and the iteration in your code appears unnecessary; if something should be returned but isn’t, the culprit could be that the statement isn’t explicit enough and needs a get. Try these statements:

tell application "Adobe InDesign 2020"'s document 1
       tell spread 1
	   select (get page items whose parent page is nothing)
	 -- select (get page items whose parent page's class is nothing)
	 -- select (get page items whose parent page is not page)
	   end
	   end

Hi Marc, I’m happy to be corrected on this. Your solution sounds reasonable but in testing, the code did not work for me. For example, the following still results in an empty list:


tell application "Adobe InDesign 2020"
	tell document 1
		tell spread 1
			--set ThePasteboard to every page item whose parent page is nothing
			set ThePasteboard to (get page items whose parent page is nothing)
		end tell
	end tell
end tell

What is returned by :


tell application "Adobe InDesign 2020"
	tell document 1
		tell spread 1
			 (get parent page of every page items)
		end tell
	end tell
end tell

It may be helpful to build a valuable filter.

You must know that some applications don’t have implemented the whose filter.
You may try with the where filter which is recognized sometimes when the whose one isn’t.

Here is an example borrowed from AppleScript User Guide showing how to use it.

tell application "Finder"
    it --result: application "Finder" (target of tell statement)
    set myFolder to path to home folder
        --result: alias "Leopard:Users:myUser:"
    files in myFolder --result: a list of Finder document files
    files in myFolder where name of it contains "AppleScript"
    (* result: document file "AppleScriptLG.pdf" of folder "myUser"
        of folder "Users" of startup disk of application "Finder"}*)
    files in myFolder where name contains "AppleScript" -- same result
    every file in myFolder whose name contains "AppleScript" -- same result
    every file in myFolder where name of it contains "AppleScript" -- same result
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 9 avril 2020 12:01:04