JavaScripting InDesign  


A sporadic discussion of my scripting exploits using JavaScript to drive Adobe InDesign.


 

Sunday, September 09, 2007

     

Placing Snippets Inline/Anchored



I've found InDesign's snippets feature to be a constant source of frustration because you simply can't place them as inline or anchored objects. So much of my work involves inline/anchored objects that this makes snippets just about useless. But the alternative of using library items isn't that great either. Library panels take up space on screen and they clutter up the the Open Recent menu. Also, in the past, they've been a source of frustration when crashes occur and they have to be recovered. Untitled assets seem to pop into existence in a recovered library, or worse yet, a previously named asset suddenly loses its name.

Snippets do not take up space on screen, are impervious to crash/recovery issues, and they do not appear on the Open Recent menu. So, how to work around the inability to place them inline? Whats more, how to do it without using the clipboard which might or might not have important information on it at any point in time.

I came up with the idea of using a temporary library. Even on a G4, creating a library, putting something into it, pulling it back out again and deleting the library is pretty swift, while on a MacIntel it positively flies. So, I wrote this function to do the work:
function placeSnipInline( mySnipFile, text ) {
var myDoc = app.documents.add(false);
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
myDoc.pages[0].place(mySnipFile);
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
myLib = app.libraries.add(File("~/Desktop/templib.indl"));
myLib.store(myDoc.pageItems[0]);
myDoc.close(SaveOptions.no);
myLib.assets[0].placeAsset(text);
myLib.close();
File("~/Desktop/templib.indl").remove();
}
One odd thing here migh
t leap out at you. Why did I not take advantage of the returned object from the place() call to get a reference to the placed object rather than rely on myDoc.pageItems[0] in the store call? The reason is that I have just this morning discovered that if you place a snippet that consists of just a text frame, what's returned by place() is the story, not the text frame.

To turn the above function into a complete script I added this front-end:
//DESCRIPTION: Place Snippet Inline/Anchored

if (app.documents.length > 0 &&
app.selection.length > 0 &&
app.selection[0].hasOwnProperty("baseline")) {
placeSnippet(app.selection[0]);
} else {
alert("There must be a text selection to run this script");
}

function placeSnippet(sel) {
if (File.fs == "Windows") {
var Filter = "Snippet files: *.inds";
} else {
var xmlFilter = function(file) {
while(file.alias){
file = file.resolve();
if (file == null) return false;
}
if (file instanceof Folder) return true;
return (file.name.slice(file.name.lastIndexOf(".")).toLowerCase() == ".inds");
}
var Filter = xmlFilter
}
var myFile = File.openDialog("Choose a snippet file", Filter);
if (myFile == null) { return }
placeSnipInline(myFile, app.selection[0]);
}



# posted by Dave Saunders @ 8:42 AM 


Comments:  
Great post! This solves a lot of problems. Quick question, I'm trying to make this work such that I 'placeAsset' on a labeled TextFrame. I've tried getting the parentStory's text object and it doesn't work. What am I missing?

# posted by Blogger Chris : 1:30 PM 
   

Chris,

Sorry to take so long to reply. I had foolishly switched on comment moderation without notification. Aargh!

If you want to place an asset from the library into text, then: you must use placeAsset with text referenced. Here's a quick example:

myAsset = app.libraries[0].assets[3];
myText = app.documents[0].stories[0].insertionPoints[0];
myAsset.placeAsset(myText);

Dave

# posted by Blogger Dave Saunders : 12:26 PM 
   

Hello Dave,

speaking of Anchored Objects, have you found any workaround for linking inline text frames with overset text?

Peace

Joao

# posted by Anonymous Joao Doria : 7:06 PM 
   

Joao,

There is not easy workaround. The only thing you can do is create new objects, put them where you need them, and then manage the "text flow" by literally moving the overset text from the first anchored frame to the start of the next.

Forcing breaks to occur only at paragraph breaks makes managing this easier. But it's a tough job. I don't have general code that will do it.

Dave

# posted by Blogger Dave Saunders : 11:28 AM 
   

Hi Dave,

I am a new Indesign Script Developer from India. Your posts are very useful for me.

Can you help me to get current page number of active document?

Thank you in advance.

With Regards,
james

# posted by Anonymous James : 4:06 AM 
   

James,

This topic has been covered many times in the Adobe U2U forum on InDesign Scripting. Go there are do a search on findPage.

You might even try searching this blog on the same thing, although there have been recent developments so the U2U forum is more current.

# posted by Blogger Dave Saunders : 5:33 AM 
   

I have a datamerge, merging from a CSV. The CSV comes from a google doc form submission. I've then downloaded the .csv file. I've been successful in merging the data. my question is how to format the data after it's merged, or before its placed into its text box.


Basically, I have 15 options for a user to choose. So lets say the google form field options would be (option 1, option 2, option 3, etc.)


What I want to do is import a snippet according to which option they select.

so when the user selects "option 2" the script would compare against option 2 and would pull in Snippet2.idms.

I am wanting this to use javascript.

# posted by Anonymous caseyctg : 9:26 AM 
   
Post a Comment  

<< Home  

This page is powered by Blogger. Isn't yours?



My Photo
Name: 

Location: Ocean, New Jersey, United States 


 


Links


Google News

Browse Archived Scripts
 
Previous Posts


Overset Text

ScriptUI Dialog with drop-down

Selecting Paragraphs

ScriptUI Dialog with interacting buttons

Methods hit wall

Open a Copy

Tables and keystrokes

Active Document Gotcha.

Last shall be First versus NextItem

Adding a Column of Numbers
 
Featured Scripts Downloads


PopTabUnleashed

Make Booklet for CS3

Build Booklet for CS & CS2

Add Line to Group

Break Out Text Frame

Change Case of Selected Style

Divide Story into Two

Proxy Aware Align Graphic in Frame

Regex Tester

Smart Title Case

Text Styles Reports for CS2
 
Archives


August 2005
September 2005
October 2005
November 2005
December 2005
January 2006
February 2006
March 2006
April 2006
May 2006
June 2006
July 2006
August 2006
September 2006
October 2006
December 2006
January 2007
February 2007
April 2007
June 2007
July 2007
August 2007
September 2007
January 2008
July 2008
October 2008
January 2012