Manipulating Selection and Clipboard Items

I’m attempting to paste an image into Keynote and set its position and size. Here is my code:

 tell application "Keynote"
	activate
	tell application "System Events" to keystroke "v" using command down
end tell
tell application "Keynote"
	set card to selection
end tell

The image pastes succefully.
Keynote throws an error 1728 - “can’t get selection”. Similar failures occur with System Events with either selection or clipboard.

Model: Mini 5,3
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.13

First of all selection is available in a document

tell application "Keynote"
	tell document 1
		set card to selection
	end tell
end tell

Second of all Keynote is pretty well scriptable. How about placing and positioning an image directly

tell application "Keynote"
	tell document 1
		tell current slide
			set newImage to make new image with properties {file:"SSD:Users:myself:Pictures:theGreatPicture.jpg"}
			set position of newImage to {20, 20}
		end tell
	end tell
end tell

Yes, I was aware of that, but there are two problems with that approach: (1) this method only works if the image already exists in the Keynote slide. If it is not, Keynote throws an error and (2) I do not have a path to the image - it is copied from a container field in a Filemaker Pro database.
So, back to the original question: is it possible to create a variable from the clipboard or from the currently selected object?

Hi, I think, stable GUI solution should be like this:


-- activate the Keynote and bring to front
tell application "Keynote" to activate

-- wait for window appearing
tell application "System Events" to tell process "Keynote"
	repeat until window 1 exists
		delay 0.02
	end repeat
end tell

-- paste the image to the selected slide
tell application "System Events" to keystroke "v" using command down

-- change the size and position of pasted image
tell application "Keynote" to tell (current slide of document 1) to tell its (image -1)
	set position to {1000, 237}
	set height to 443
	set width to 200
	set card to it
end tell

NOTE: your variable card in the my script is last image of current slide of document 1 of application “Keynote”. If you need it later.

Thanks. This got me off the ground. Now, though I am unable to pass a variable to the x-coordinate of the images to distribute them. The script runs with no error, but the horizontal position does not change. I tried passing a variable to the y-coordinate and it works.


tell application "Keynote" to activate

-- wait for window appearing
tell application "System Events" to tell process "Keynote"
	repeat until window 1 exists
		delay 0.02
	end repeat
end tell

-- distribute images horizontally

tell application "Keynote" to tell (current slide of document 1)
	set cardspacing to 50
	set cardcount to count of its images
	repeat with i from 1 to cardcount
		set cardspacing to cardspacing + 150
		delay (0.5)
		tell its (image i)
			
			set its position to {cardspacing, 50}
		end tell
	end repeat
end tell

Hi,

What you does in your last script successfully changes the X position (horizontal position. You name it somewhat wrongly cardspacing) of each image. The Y position of each image in your script is constant as I see (50). One note only:

incrementing of X position of next image of repeat loop should be placed after setting the X position of previous image:


tell application "Keynote" to activate

-- wait for window appearing
tell application "System Events" to tell process "Keynote"
	repeat until window 1 exists
		delay 0.02
	end repeat
end tell

-- distribute images horizontally

tell application "Keynote" to tell (current slide of document 1)
	set cardspacing to 0
	repeat with i from 1 to (count of its images)
		delay 0.5
		tell its (image i) to set its position to {cardspacing, 50}
		set cardspacing to cardspacing + 150 --  HERE
	end repeat
end tell

Setting the position of the image has no effect on the x-coordinate, but it works on the y-coordinate. I created a simpler version for just one image. When it runs, the y-position moves to 50, but the x-position does not change. Is this an issue with Keynote? Here is the simplified code:



tell application "Keynote" to activate

-- wait for window appearing
tell application "System Events" to tell process "Keynote"
	repeat until window 1 exists
		delay 0.02
	end repeat
end tell

tell application "Keynote" to tell (current slide of document 1)
	tell its (image 1) to set its position to {0, 50}
end tell

It seems as bug on your KeyNote version. As I sad on my system the script successfully arranges the images to incremented X positions and to Y position = 50.

As you see I use the Catalina, and my KeyNote is updated to newest version.

Try to put its position in the ( ). Maybe, it will help:


tell application "Keynote" to tell (current slide of document 1)
   tell its (image 1) to set (its position) to {0, 50})
end tell

A system update brought Keynote up to version 9.1 but the problem remained. I found a way to work around it. First I tried resetting the value of the text area of the “X” entry of the “Arrange” button. That didn’t work either, but I was able to get its value and set it to a variable. I then told System Events to Repeat (variable minus 10) times Keystroke Left Arrow. Crude, but I’ll take it.

Hi again,

The fixed version begins with 9.2 version of Keynote, and It requires at least OS 10.14