Applescripting insert image with excel

I hope I am in the right place for this post, its regarding Applescript


I have an applescript that does work for the most part. It places an image into a cell in excel for me, goes down a cell and then puts another image in that cell. The problem is that as it continuses down rather then placing it in the top left corner of the cell as I the script tells it to it gradualy lowers the images placment in the cell. after about 30 images the image well out of alignment and I have to go back and manually correct.


below is the script

Does anyone know what I am doing wrong here?


--choose master ss

set masterss to (choose file with prompt "select the master ss file")

set file_name to name of (info formasterss)


--choose main folder where jpegs are and keep

set jpegdir to (choose folder with prompt "select folder of thumbs") as text



tell application "Microsoft Excel"


activate

set vfxnamecolumn to 4

set vfxnamerow to 3

repeat

set newvfxnamerow to (vfxnamerow + 1)

set vfxnamerow to newvfxnamerow

if value of cell vfxnamecolumn of row vfxnamerow = "" then exit repeat

set cellxx to (value of cellvfxnamecolumn of rowvfxnamerow of active sheet)

set image_file to (jpegdir & cellxx & ".jpg") as text


set newPic to makenewpictureat the beginning of worksheet 1 of workbookfile_namewith properties {file name:image_file}


set left position of newPic to left position of cell (vfxnamecolumn - 1) of row vfxnamerow


set top of newPic to top of cell (vfxnamecolumn - 1) of row vfxnamerow


set height of newPic to 140


set width of newPic to 280

end repeat

end tell

2.16 intel core duo macbook pro, Mac OS X (10.6.1)

Posted on Jan 22, 2012 3:00 PM

Reply
12 replies

Jan 25, 2012 8:42 PM in response to scumsuca

Hi,


scumsuca wrote:

Does anyone know what I am doing wrong here?



Because the contents of the variable newPic is not the correct reference to the picture, it's a bug.


Try this repeat

----------------------------------------

repeat

setvfxnamerowtovfxnamerow + 1

set {tTop, tLeft, cellxx} to {top, left position, value} ofcellvfxnamecolumnofrowvfxnamerowofactive sheet

if cellxx = "" then exit repeat


set image_file to (jpegdir & cellxx & ".jpg") as text

makenewpictureatthebeginningofactive sheetwith properties {file name:image_file, top:tTop, left position:tLeft, height:140, width:280}

endrepeat

----------------------------------------

Jan 30, 2012 12:39 PM in response to scumsuca

I have not worked through your script in detail, but I think the problem is that the images are not being inserted into the cells as such. What's happening is that the image is being created at the beginning of the worksheet and then moved into a position where its top, and top left, are relative to a referenced cell. Note, therefore, that when you move the cells the pictures will NOT move with it.


This is how I would do what I think you are trying to do, but if I'm wide of the mark do come back!


set imageFolder to (choose folder) --choose a folder containing the image files


tell application "Finder"

set imageList to files of folderimageFolder--assumes only image files in the folder

end tell


tell application "Microsoft Excel"

set rowHeight to standard height of worksheet 1 of workbook 1 --gets the row height from the frontmost worksheet of the frontmost workbook

set rowStep to round (140 / rowHeight) roundingup--calculates how many rows are needed to fit a picture height of 140

set thisStep to 0 --initiates the counter used to determine the row for the top of the picture

set theLeft to left position of cell 1 of row 1 --this is the left position of column 1, row 1. Change column to move pictures in.



repeat with imageFile in imageList--gets images from the list created earlier


set theTop to (top of cell 1 of row (1 + thisStep)) --calculates row for top of picture (starting at top of column 1, row 1)


set newPic to makenewpictureat beginning of worksheet 1 of workbook 1 with properties {file name: (imageFile as text), height:140, width:280, top:theTop, left position:theLeft} --creates the picture, sets height and width, and top and left positions


set thisStep to thisStep + rowStep--increments the row counter


end repeat


end tell



Message was edited by: Bernard Harte

Jan 31, 2012 2:22 PM in response to Arkouda

Thanks so much for you help but unfortunatly I get the same result.


gradually, with each picture that goes in to the spreadsheet the position is of each image is slightly higher then it should be and over the 60 rows its out by about half the height of the image.


altering the rows height after the script has run does not help in either mine or your script. When I change the row height excel adjusts the position of the images as well by the same factor. If I adjust the rows so that the last image is with in the cell (touching the bottom of the cell) then the top image is out.


your script is much neater then mine tho. I am a bit of a novice.


I am going to try another version of excel and see how that goes.

any other suggestions would be appriciated.

Jan 31, 2012 3:22 PM in response to scumsuca

Are you saying that you want the row height to be the same as the pictures themselves (and for each row to contain a picture)? (My script will place the image over several rows.)


Do you need empty rows between those containing pictures?


Do you have rows of equal height throughout the worksheet or do they vary?


A little more detail about your goal might help to give a workable solution.

Jan 31, 2012 3:54 PM in response to Arkouda

The job is to provide a spreadsheet which lists details relating to a visual effect in a tv show.

there are many VFX shots eg 60 plus

each row contains the details for 1 VFX shot. things like - a description of the work, where to find the data for the shot, director notes etc are in separate cells in the row. All these details are already being extracted via XML and an applescript I have.

However there also needs to be a thumbnail with each VFX shot. This is where this script comes in.


Row hieght does not need to be the same as the picture height. Row height could be larger then picture height. I just need each thumbnail to be properly aligned with the row of its VFX shot. At the moment as I scroll down the spreadsheet the images start to become misalligned and someone reading the spreadsheet will be confused as to which VFX shot the thumbnail relates to.


I dont need any empty rows.


I have rows of equal height, although I would have thought that as the picture has been placed in a cell then the row heights could vary and the picture would shift to realign.


I hope that makes a little more sence.

Jan 31, 2012 4:07 PM in response to scumsuca

It does make things clearer. Thanks.


The picture is not being placed in a cell, but is just being aligned with the left and top of a cell reference that you give it.


I am mystified why your pictures are going out of step with the rows, but I neglected to ask you which version you are using - I am on 2011 (14.1.4) - although I can't see how that would be causing the problem.


I have made some changes to insert the images in consecutive rows in a column and then to modify the height of each of the rows that contain an image. (I've also put a variable in for height and width so that you can adjust in one place if you need to.


One more thought: if this still gives you the same error, try quitting Excel altogether and making a completely new spreadsheet to try it on.


set imageFolder to (choose folder)


tell application "Finder"

set imageList to files of folderimageFolder

end tell


tell application "Microsoft Excel"

set imageHeight to 140

set imageWidth to 280

set imageCount to count of imageList

set theRange to range ("A1:B" & imageCount as string) of worksheet 1 of workbook 1

set thisStep to 0

set theLeft to left position of cell 1 of row 1



repeat with imageFile in imageList


set theTop to (top of cell 1 of row (1 + thisStep))


set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name: (imageFile as text), height:imageHeight, width:imageWidth, top:theTop, left position:theLeft}


set thisStep to thisStep + 1

end repeat

set row height of theRange to imageHeight

end tell

Jan 31, 2012 4:39 PM in response to Arkouda

Here's another thought: there's a picture placement setting that will move the pictures with cells but not resize them. That's what I have as default, but yours might be different. I can't reproduce the problem of going out of step with the row.


set imageFolder to (choose folder)


tell application "Finder"

set imageList to files of folderimageFolder

end tell


tell application "Microsoft Excel"

set imageHeight to 140

set imageWidth to 280

set imageCount to count of imageList

set theRange to range ("A1:B" & imageCount as string) of worksheet 1 of workbook 1

set thisStep to 0

set theLeft to left position of cell 4 of row 1



repeat with imageFile in imageList


set theTop to (top of cell 4 of row (1 + thisStep))


set newPic to make new picture at beginning of worksheet 1 of workbook 1 with properties {file name: (imageFile as text), height:imageHeight, width:imageWidth, top:theTop, left position:theLeft, placement:placement move}



set thisStep to thisStep + 1

end repeat

set row height of theRange to imageHeight

end tell

Jan 31, 2012 8:29 PM in response to Arkouda

Thanks so much,

both of the last 2 scripts work like a charm.


I am on excel 2008 v12.

This has been a problem I have had for some time so I have opened and closed excel plus made new docs.


I was about to say line

set row height of theRange to imageHeight

was the soulution but I took it out of the script and It works fine without that line. Out of interest, what do you think the key to it is?

Aug 2, 2013 11:10 AM in response to scumsuca

Hi Guys, thanks for the script, it works well.


The script sets the height and width of images in pixels, wich its not desireble when having many pictures of different sizes. In my case it's necessary to keep the aspect ratio of the images imported into excel.


Is there any way to set the size of the image in a percentage of the original instead of fixed pixels?


Thanks very much!


Francis

Aug 2, 2013 7:36 PM in response to Francis_Acea

I suppose you could get the dimensions and then do the maths on it and then set the dimensions


To get the dimensions something like this would work

then manipulate the output and set it to your image


tell application "QuickTime Player 7"

set mydocument to document 1 -- gets frontmost open movie

set myproperties to properties of mydocument-- gets properties of movie as list

tell mydocument-- each of the following gets an individual property

set mydimensions to dimensions

end tell

end tell

mydimensions


then manipulate the output and set it to your image.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescripting insert image with excel

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.