Saturday, January 16, 2021

#1 2021-01-08 06:37:06 am

4CardsMan
Member
Registered: 2020-12-29
Posts: 5

Manipulating Selection and Clipboard Items

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

Applescript:

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

Offline

 

#2 2021-01-08 09:01:32 am

StefanK
Member
From:: St. Gallen, Switzerland
Registered: 2006-10-21
Posts: 11714
Website

Re: Manipulating Selection and Clipboard Items

First of all selection is available in a document

Applescript:

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

Applescript:

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


regards

Stefan

Offline

 

#3 2021-01-08 09:15:38 am

4CardsMan
Member
Registered: 2020-12-29
Posts: 5

Re: Manipulating Selection and Clipboard Items

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?

Offline

 

#4 2021-01-08 09:29:30 am

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

Re: Manipulating Selection and Clipboard Items

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

Applescript:


-- 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.

Last edited by KniazidisR (2021-01-08 10:00:20 am)


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

Offline

 

#5 2021-01-08 03:59:46 pm

4CardsMan
Member
Registered: 2020-12-29
Posts: 5

Re: Manipulating Selection and Clipboard Items

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.

Applescript:


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

Offline

 

#6 2021-01-09 06:30:32 am

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

Re: Manipulating Selection and Clipboard Items

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:

Applescript:


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

Last edited by KniazidisR (2021-01-09 07:21:31 am)


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

Offline

 

#7 2021-01-12 07:19:33 am

4CardsMan
Member
Registered: 2020-12-29
Posts: 5

Re: Manipulating Selection and Clipboard Items

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:

Applescript:



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

Offline

 

#8 2021-01-12 07:49:28 am

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

Re: Manipulating Selection and Clipboard Items

4CardsMan wrote:

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?


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:

Applescript:


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

Last edited by KniazidisR (2021-01-12 09:11:53 am)


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

Offline

 

#9 2021-01-12 03:27:42 pm

4CardsMan
Member
Registered: 2020-12-29
Posts: 5

Re: Manipulating Selection and Clipboard Items

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.

Offline

 

#10 2021-01-12 11:54:49 pm

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

Re: Manipulating Selection and Clipboard Items

Hi again,

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


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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)