Applescript insert images into .Pages document

applescriptautomationautomatorpages

I've tried numerous examples online, but all don't seem to work.
The image is received via Automator and each file is dispensed incrementally and ran through the following Applescript:

on run {input, parameters}
   set img to item 1 of input

   tell application "Pages"
       set thisDocument to make new document

       tell thisDocument
          make new image with properties {image img}
       end tell

   end tell
end run

It should insert every image into the same Pages document, but instead I'm getting 'Can't make image {image (alias: "certain path") with document id: "certain id"} into type properties of image'.

Can someone point out what I'm doing wrong?


Automator workflow
Automator Workflow

Best Answer

Since I do not have the Dispense Items Incrementally Action what I'm going to present is an alternate way of achieving what I understand your end goal to be, at least in part anyway. The code presented below is not polished or complete in that it simply preforms the insertion of the number of images in the selected folder into the document without regard for placement location in the document. You'll have to modify the code to meet your wants/needs. However, if it's not acceptable to you, I do not expect a down dote as I did go to the trouble of trying to offer a reasonable solution, so simply let me know in a comment that it's not the direction you want to go and I'll delete the Answer.

In Automator, I created a Workflow using Ask for Finder Items, setting the Type: to Folders while not checking Allow Multiple Selections, > Get Folder Contents > Run Apple Script and a Folder in Finder containing two .png images named as shown in the Results of your Get Folder Contents Action (although I used PNG files it will work with JPG or any type graphic images supported in Pages).

I then modified your AppleScript code to include the item mentioned in my comment and a repeat statement along with a counter.

I was then able to insert the two images into a Pages document, which happened lexicographically as that's apparently the default in a scenario such as this. So this seems to negate the need for the Dispense Items Incrementally Action, although not having that Action I'm not able to test the difference between the two Workflows.

AppleScript code:

on run {input, parameters}
    set repeatCount to input count
    set n to 1
    tell application "Pages"
        set thisDocument to make new document
        tell thisDocument
            repeat repeatCount times
                set img to item n of input
                make new image with properties {image data:img}
                set n to n + 1
            end repeat
        end tell
    end tell
end run

As previously mentioned, this just inserts the number of images in the selected folder into the document without regard for placement location in the document and you'll need to determine what code is necessary to achieve proper location placement in the document and because this is looping you'll probably need to increment the location position for each image you insert in the document as well.

Even though it's installed I personally do not use Pages, I use LibreOffice, however having searched the Internet a bit I saw example code that dealt with placement location in the document and I would also open the Script Editor app and then open the Pages Dictionary to see what it exposes so you can determine what you have to work with.