AppleScript needed for moving images depending on a table's cell content

Hi everyone,


I already posted a similar question and everything I've read about indicates that this should be possible through AppleScript, so I'm posting again with a new title in hope that some AppleScript wizard can help me (I've tried and failed to do it myself, I barely know any AppleScript).


What I'm trying to do is to move an already present image in the document (on another page) to a specified location in the first page, depending on wether a string is present in a cell.


It should be something like this:


For the front document of Pages,

If in "B2" of "Tabla 5-2" there is the string "Sunny", move "sun.png" to X=13,97cm Y=4,44cm of page 1

If in "B2" of "Tabla 5-2" there is the string "Windy", move "wind.png" to X=13,97cm Y=4,44cm of page 1

If in "B2" of "Tabla 5-2" there is the string "Cloudy", move "cloud.png" to X=13,97cm Y=4,44cm of page 1

If in "B2" of "Tabla 5-2" there is the string "Rainy", move "rain.png" to X=13,97cm Y=4,44cm of page 1

If in "B2" of "Tabla 5-2" there is the string "Stormy", move "storm.png" to X=13,97cm Y=4,44cm of page 1


Some notes:

  • The Table's name and cell are accurate for my document (table name is in spanish, I don't know if this is important for AppleScript) (By the way, is there an easier way to find a table's name rather than trying writing a formula from scratch?)
  • The image file names are accurate and are images already present in the document, in another page.
  • The X,Y coordinates are accurate but I don't know if AppleScript takes values in cm (I guess not), and they are based on setting the origin of the ruler (0) at the center.


Hopefully someone can help me. I would be eternally grateful



Thanks!


iMac 27", macOS 10.13

Posted on Apr 16, 2019 1:42 AM

Reply
8 replies

Apr 16, 2019 4:38 AM in response to paubalague

You cannot do what you want to do with Pages v5 thru v8.0 because Apple has severely restricted the AppleScript scripting dictionary and accessible document object properties. One has no ability to jump around in the document based on page number, and because of that, one cannot move objects between pages.


Although one can tell an individual image to move its relative position (in pixels) on the current page using AppleScript, it does not contain internal properties that allow one to set its page number.


Game over.



Apr 16, 2019 6:10 AM in response to paubalague

Images in a Pages document have no name property, so one cannot directly delete them by name. One can make a list of all images in the document, and do the following:


tell application "Pages"
	tell front document
		set iLst to every image of it
		tell (item 2 of iLst) to set its position to {256, 256}
		delay 2
		delete (item 2 of iLst)
	end tell
end tell


Pages has two layers: foreground and background. The Pages scripting dictionary makes no distinction, or permits manipulation of an image between foreground or background status.


From what you have described, Pages and AppleScript are not going to provide the solution flexibility that you seek.

Apr 16, 2019 4:47 AM in response to VikingOSX

Thanks for your reply!


Possible workarounds I'm thinking about, if you would be so kind as to steer me in the correct direction or tell me if it's possible:

  • Can AppleScript delete named images from a document? If so, I could have all weather icons one on top of the other, in their final position, and delete all icons that are NOT what is in the B2 cell I mentioned.
  • Having all icons to the side and moving them to their final location in the same page. If Applescript can delete de rest of the images, perfect. If not, the user could do it, but because they're off to one side, it would be less prone to mess with the design accidentally. Not the perfect solution, though.


Thanks again for your reply and hope to hear from you again! Thanks!

Apr 16, 2019 4:49 AM in response to VikingOSX

Another option: If AppleScript could change the relative position of objects from the foreground to the background, I could have all icons one on top of the other in their final position, hidden by a white square on top of them all, and only bring the correct icon to the front. I'm guessing this can't be done through AppleScript, but just to make sure.


Thanks!

Apr 16, 2019 6:30 AM in response to VikingOSX

If I could do this example but conditional on the content of de B2 cell (which I don't know how to syntax properly), I don't know why this couldn't work. I don't really care if AppleScript lets me call a specific image by name, as long as the results from that list are always repeatable, and since the number or position of the images in the document will never change before the execution of the script, I don't see why this wouldn't work. It will be a pain in the *** to try and "discover" which list item it would be, but if it's repeatable and consistent it should work wouldn't it? Specifically, the option of having all icons on top of each other in the final position and deleting every image not related.


What do you think?

Apr 16, 2019 7:15 AM in response to VikingOSX

I'm doing tests and it looks very promising with your script! If you would be so kind as to show me how it would work to include the IF condition and making it a loop for all possibilities, I think we may have a winner! It's obviously not the perfect solution, and if AppleScript had a more extensive disctionary it would be more elegant, but you have to work with what you have, and this may be just good enough!

Apr 16, 2019 7:20 AM in response to VikingOSX

Ok, thanks anyway. I'm not sure why you would even spend ANY time helping someone if you don't follow through to the end, but obviously I respect your decision.


Thanks. If you change your mind, or even if you would be so kind as to COPY the code of a similar Script with the IF statement syntax, I'll be here trying to figure things out.


And if anyone out there would help me with this I'll be very grateful.


Thanks

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.

AppleScript needed for moving images depending on a table's cell content

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