Adobe-CEP / CEP-Resources

Tools and documentation for building Creative Cloud app extensions with CEP
https://www.adobe.io/apis/creativecloud/cep.html
1.63k stars 828 forks source link

Cannot place Rich Text documents programmatically #135

Open AndrewMore1 opened 7 years ago

AndrewMore1 commented 7 years ago

Attempting to place a rich text file in Illustrator. I can get it to work correctly if I do it manually using File->Place, at which point it prompts me, then displays the text. However I get an error message "The file "x" is in a format which cannot be placed." if I attempt to do it programmatically.

Here's a code snippet: var doc = app.activeDocument;

        // bugs out when tying to add to a locked layer, so unlock first
        var layer = doc.activeLayer;
        var didUnlock=false;
        if(layer.locked) {
            layer.locked = false;
            didUnlock = true;
        }

        try {
            var newFile = new File(path);
        } catch (e) {
            return null;
        }

        if (!newFile.exists) {
            return null;
        }

        var image = doc.placedItems.add();
        try {
            image.file = newFile;
        } catch (e) {
            image.remove();
            return null;
        }
Silly-V commented 7 years ago

The scripting Place command is actually not quite the same as your UI Place command in Adobe Illustrator. What it does is have is a special behavior which handles some files in a different way than 'regular files' such as .png or .pdf files. For example, with AutoCad and SVG files, same thing will occur. I believe this is because when done via UI, the place command has the same steps as opening a file, but the user is spared the visuals when this is done. The same is happening with .rtf files, but as I have just tested out, Document.importFile(fileObj, false) function will have the effect of importing an .rtf file - so you may use this as an alternative. However, I'm not sure how to make this happen without the dialog box, which always pops up.

AndrewMore1 commented 7 years ago

That's very helpful, thank you. As it turns out you can usually suppress user interactivity.

Snippet: var interactionLevel = app.userInteractionLevel; app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

// Do the thing

app.userInteractionLevel = interactionLevel;    
Silly-V commented 7 years ago

You know what, I tried that, but I was in a hurry and had a syntax error which led me to believe the import dialog was not being suppressed. But you're exactly right, once I fixed it, it imports without issue.