Copy link to clipboard
Copied
Hi
I need a script to add a certain line of text to wherever the cursor is, then I'll use a shortcut to this script. to speed up copying and pasting some lines all over the book.
Can someone make it, please?
Thank you.
Thank you for your effort, I found an easy script.
app.selection[0].insertionPoints[-1].contents = 'ADDYOURTEXTHERE';
then assign a shortcut to this script, I hope this will help someone else.
Copy link to clipboard
Copied
Hi @AK09M, I have written a script that might suit your purpose. My script will locate snippet files (.idms) inside a folder you specify (or, by default, in the current document's parent folder) and let you choose which one to place, or you can specify the exact snippet to place and it won't ask you anything.
So you could do something like this:
1. Save your "line of text" as a snippet.
2. Configure the script to point to that snippet file, eg.
var settings = {
snippetFile: '~/Documents/My Snippets/mySnippet-1.idms',
};
3. Save the script as "Place snippet 1.js"
4. If you want to have different scripts place different snippets, then repeat steps 1 to 3 for each snippet you wanted to place and name them differently.
5. In indesign, set a keyboard shortcut for the script or scripts (optional).
I'm not sure if you have used snippet files before... To create a snippet file, do this:
(a) type the text in Indesign, and adjust to your liking
(b) select the text frame (or it can be a group, or multiple objects)
(c) choose menu File > Export
(d) export as Indesign Snippet (.idms) file.
IMPORTANT: snippets are not linked, so if you edit the original snippet, it will not update anywhere else. Also you can edit a placed snippet so you can change the text if necessary.
If you do not specify the snippetFile, it will search in the document's parent folder and give you a choice:
Is this something that would suit your situation? I guessed it was, but it might need configuring a little.
- Mark
/**
* @file Place Snippet.js
*
* Script to place pre-saved snippets onto current page.
*
* Different modes:
*
* 1. Specify the snippet file:
* Place the snippet immediately.
*
* var settings = {
* snippetFile: '~/Documents/My Snippets/mySnippet.idms',
* };
*
* 2. Specify a particular folder for snippets:
* Will show menu of all snippets found.
*
* var settings = {
* folder: '~/Documents/My Snippets',
* };
*
* 3. Specify nothing:
* Will look inside the active document's parent folder for snippets.
*
* var settings = {};
*
* The position of the snippet will be:
* (a) the position embedded in the snippet (the position it was exported from)
* (b) specified position, eg. settings.position = [10,30];
* (c) to match the current selection, add something like this to settings:
* position: (
* undefined != doc.selection[0]
* && doc.selection[0].geometricBounds &&
* [doc.selection[0].geometricBounds[1], doc.selection[0].geometricBounds[0]]
* ),
*
* @author m1b
* @discussion https://community.adobe.com/t5/indesign-discussions/a-script-to-add-a-line-of-text/m-p/14465768
*/
function main() {
var doc = app.activeDocument;
if (!doc.isValid)
return alert('Please open a document and try again.');
try {
// you can configure this object
var settings = {
// snippetFile: '~/Documents/mySnippet.idms',
// folder: '~/Documents/Snippets',
};
} catch (error) {
return alert('Error in settings object:\n(' + error.message + ')');
}
var doc = app.activeDocument;
if (!doc.isValid)
return alert('Please open a document and try again.');
if (undefined == settings.snippetFile) {
// where to search for snippets
if (settings.folder == undefined) {
settings.folder = doc.saved
? doc.fullName.parent
: Folder.selectDialog();
}
else if ('String' == settings.folder.constructor.name)
settings.folder = Folder(settings.folder);
if (!settings.folder)
return;
if (!settings.folder.exists)
return alert('Could not locate folder at "' + settings.folder + '".');
if (
undefined == settings.snippetFiles
|| 0 === settings.snippetFiles.length
)
settings.snippetFiles = getFilesOfFolder({
folder: settings.folder,
maxDepth: 2,
filterRegex: /\.idms$/,
}).sort();
// no snippets found
if (0 === settings.snippetFiles.length)
return alert('No snippets were found inside the document\'s folder.');
// ask user which snippet to place
else if (
settings.snippetFiles.length > 1
&& ui(settings) == 2
)
// user cancelled UI
return;
}
if (
undefined != settings.snippetFile
&& 'String' === settings.snippetFile.constructor.name
)
settings.snippetFile = File(settings.snippetFile);
try {
// place snippet
doc.layoutWindows[0].activePage.place(settings.snippetFile || settings.snippetFiles[0], settings.position);
} catch (error) {
return alert(error);
}
};
app.doScript(main, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, 'Place Snippet');
/**
* Collects all files inside a Folder, recusively searching in sub-folders.
* If no folder is supplied, will ask user. Search can be filtered using
* `filterRegex`, `fileFilter` or `folderFilter`, and also limited to `maxDepth`.
* @author m1b
* @version 2024-02-21
* @param {Object} options - parameters
* @param {Folder} [options.folder] - the folder to look in (default: ask).
* @param {RegExp} [options.filterRegex] - regex to match file/folder's name (default: no filter).
* @param {Function} [options.fileFilter] - function, given a File, must return true (default: no filter).
* @param {Function} [options.folderFilter] - function, given a Folder, must return true (default: no filter).
* @param {Boolean} [options.returnFirstMatch] - whether to return only the first found file (default: false).
* @param {Number} [options.maxDepth] - deepest folder level (recursion depth limit) (default: 99).
* @param {Boolean} [options.includeFiles] - whether to include files (default: true).
* @param {Boolean} [options.includeFolders] - whether to include folders (default: false).
* @param {Boolean} [options.includeHiddenItems] - whether to include hidden items (default: false).
* @param {Number} [depth] - the current depth (private param).
* @returns {Array|File} - all the found files, or the first found file if `returnFirstMatch`.
*/
function getFilesOfFolder(options, depth) {
// defaults
options = options || {};
var found = [],
folder = options.folder;
if ((depth || 0) === 0)
// once-off initialization
// to keep the object clean
if (!(options = initializeOptions()))
return [];
// get files from this level
var files = folder.getFiles(),
hiddenFile = /^[\.\~]/;
filesLoop:
for (var i = 0; i < files.length; i++) {
if (excludeFilter(files[i]))
// file/folder excluded!
continue filesLoop;
if (includeFilter(files[i])) {
// file/folder matched!
found.push(files[i]);
if (true === options.returnFirstMatch)
break filesLoop;
}
if (
!(files[i] instanceof Folder)
|| depth >= options.maxDepth
)
// we only want folders
continue filesLoop;
// set up for the next depth
options.folder = files[i];
// look inside the folder
found = found.concat(getFilesOfFolder(options, depth + 1));
}
// this level done
if (
options.returnFirstMatch
&& 0 === depth
)
// just return the first found file/folder
return found[0];
else
// return the whole array
return found;
/**
* Returns true when the file/folder should be excluded.
* @param {File|Folder} file
* @returns {Boolean}
*/
function excludeFilter(file) {
return (
// is hidden
(
false == options.includeHiddenItems
&& hiddenFile.test(file.name)
)
// an ignored folder
|| (
file instanceof Folder
&& !(
undefined == options.folderFilter
|| options.folderFilter(file)
)
)
);
};
/**
* Returns true when the file/folder should be included.
* @param {File|Folder} file
* @returns {Boolean}
*/
function includeFilter(file) {
return (
(
// file passes filter
(
options.includeFiles
&& file instanceof File
)
// folder passes filter
|| (
options.includeFolders
&& file instanceof Folder
)
)
// match regex
&& (
undefined == options.filterRegex
|| options.filterRegex.test(decodeURI(file.name))
)
// pass filter
&& (
undefined == options.fileFilter
|| options.fileFilter(file)
)
);
};
/**
* Returns the initialised `options` object.
* @returns {Object}
*/
function initializeOptions() {
// first level of search
depth = 0;
// make a new object, so we don't pollute the original
var newOptions = {
filterRegex: options.filterRegex,
fileFilter: options.fileFilter,
folderFilter: options.folderFilter,
returnFirstMatch: options.returnFirstMatch,
maxDepth: options.maxDepth,
includeFiles: options.includeFiles !== false,
includeFolders: true == options.includeFolders,
includeHiddenItems: true == options.includeHiddenItems,
};
if (
undefined == options.maxDepth
|| !options.maxDepth instanceof Number
)
newOptions.maxDepth = 99;
if (
undefined != folder
&& 'String' == folder.constructor.name
&& File(folder).exists
)
folder = File(folder);
else if (
undefined != folder
&& 'String' == folder.constructor.name
)
folder = undefined;
if (undefined == folder)
// ask user for folder
folder = Folder.selectDialog("Select a folder:");
if (
null === folder
|| !(folder instanceof Folder)
)
// folder bad or user cancelled
return;
// update folder in the *original* object
options.folder = folder;
return newOptions;
};
};
/**
* The UI for this script.
* @author m1b
* @param {Object} settings - the settings object for the UI.
* @returns {Number} - ScriptUI result code.
*/
function ui(settings) {
var snippetNames = [];
for (var i = 0; i < settings.snippetFiles.length; i++)
snippetNames[i] = decodeURI(settings.snippetFiles[i].name);
var w = new Window('dialog', 'Place Snippet'),
group = w.add('group {orientation:"row", alignment:["left","center"], alignChildren:["left","center"], spacing:12, preferredSize: [120,undefined], margins:[10,10,10,10] }'),
label = group.add('statictext', undefined, 'Place snippet: '),
menu = group.add('dropdownlist', [20, 20, 130, 29], snippetNames),
okayButton = w.add('button', undefined, 'Place', { name: 'ok' });
menu.selection = 0;
menu.addEventListener('change', function (ev) {
settings.snippetFile = settings.snippetFiles[menu.selection.index];
});
menu.active = true;
okayButton.onClick = function () { w.close(1) };
return w.show();
};
Copy link to clipboard
Copied
Thank you for your effort, I found an easy script.
app.selection[0].insertionPoints[-1].contents = 'ADDYOURTEXTHERE';
then assign a shortcut to this script, I hope this will help someone else.
Copy link to clipboard
Copied
Ah! Haha, I see you really mean just some text. 🙂
Glad you solved it!
Copy link to clipboard
Copied
Thank you @m1b