Search code examples
applescriptadobeadobe-indesigncreative-cloud

How do you target the InDesign place gun with Applescript?


I am new to Applescript so thanks a bunch for your patience! I am trying to load a JPG into the place gun with Applescript for Adobe InDesign. The purpose would be to learn the rudimentary action for use in more complex scripts.

The InDesign Applescript dictionary shows load place gun as a command that allows any file input (see image at bottom), but when I run the following script it throws this error,"Adobe InDesign 2024 got an error: "/Users/michael/Desktop/BCC_portfolio.jpg" doesn’t understand the “load place gun” message."

on run
    set filePath to "/Users/michael/Desktop/BCC_portfolio.jpg"
    tell application id "com.adobe.indesign"
        activate
        load place gun filePath
    end tell
end run

I came across one semi-related forum that listed the following as a way to place items using the choose file command (which works on my system), but when I change the choose file command to a simple path, it throws the same error from before.

set loadFiles to "/Users/michael/Desktop/BCC_portfolio.jpg"

tell application id "com.adobe.indesign"

  activate

  tell active document

   if loaded of place gun 1 is false then

     tell place gun 1

       repeat with loadFile in loadFiles

         load place gun loadFile without showing options

       end repeat

     end tell

   end if

  end tell

end tell

This leads me to believe there's something I'm doing wrong in the handling of the file path, but other commands in inDesign/Photoshop seem to work fine with the normal POSIX path.

I haven't found any pertinent info online, in the inDesign SDK, or Applescript Dictionary, but if if someone could point me to the correct resources, or knows the solution, it would be greatly appreciated!

Load Place Gun entry in the InDesign AS Dictionary


Solution

  • In order for the steps in your script to work, the variable loadFiles must be a list and that list must contain a file object.

    The following uses {} to ensure that loadFiles is a list and POSIX file to convert the POSIX path into the HSF path needed for the file object.

    set loadFiles to {POSIX file "/Users/michael/Desktop/BCC_portfolio.jpg"}
    
    tell application id "com.adobe.indesign"
        
        activate
        
        tell active document
            
            if loaded of place gun 1 is false then
                
                tell place gun 1
                    
                    repeat with loadFile in loadFiles
                        
                        load place gun loadFile without showing options
                        
                    end repeat
                    
                end tell
                
            end if
            
        end tell
        
    end tell