Repository navigation
Expand file tree
/
Copy pathOpenChangeSaveClose.js
More file actions
241 lines (184 loc) · 8.44 KB
/
Copy pathOpenChangeSaveClose.js
File metadata and controls
241 lines (184 loc) · 8.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
//////////////////////////////////////////////////////////////////////////////
// Script: Open Save and Close Layouts
// Language: JavaScript
// Author: jag
// Company: WoodWing USA
// Date: 22 Feb 2023
// Version: 1.0.1
//////////////////////////////////////////////////////////////////////////////
// Description: Check files out and check back in. Can be used with InCopy and InDesign.
// Set the application name in the configuration below. Will create a new version of the
// opened files.
// NOTES: Be sure to modify the 'queryName' variable to match the user query created
// NOTE: This is script is not smart enough to identify the object type from the query
// So be sure to set the 'applicationName' variable and then set the saved user query to find
// just the appropriate objects (i.e. for instance 'Layouts')
//////////////////////////////////////////////////////////////////////////////
//=======================================
// Identify which application to use
//=======================================
var applicationName = "InDesign";
//=======================================
// Identify the desired query to use
//=======================================
var articleQueryName = "Layouts";
//=======================================
// Example for modifying metadata. In this case RouteTo
// This code is commmented out below so it is not on at the moment
//=======================================
var newRouteTo = "WoodWing";
//=======================================
// Set the user interaction level to 'never interact' this is set back to 'normal'
// at the end of the script
//=======================================
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.neverInteract;
//=======================================
// Begin the work of the script...do the query and get the results back...
//=======================================
try
{
//=======================================
// Send a query to Enterprise
//=======================================
var query = app.storedUserQuery(articleQueryName);
//=======================================
//Push the query results into an array using new lines as the value to split by
//=======================================
var stringToArray = new Array();
var stringToArray = query.split(/\n/g);
//=======================================
//Count the number of objects in the array
//=======================================
var countOfObjects = stringToArray.length;
//=======================================
// Iterate through the array, do a regular expression to find the records
// Remove the < and > characters and push the record into another array
//=======================================
if (countOfObjects > 0)
{
var b = 0;
var newArrayOfRecords = new Array();
for ( var a = 1; a < countOfObjects; ++a)
{
//=======================================
// Look for records in the query result
// This is done by looking at the returning query, parsing it by
// Regular Expression, pushing the found records into a new
// array and then grabbing the ID of each record
//=======================================
//=======================================
// Regular Expression looks for items surrounded by brackets
// The regular expression is pretty simple...look for anything surrounded by brackets
//=======================================
var recordExists = new RegExp("<.*>");
//=======================================
//Perform the regular expression search
//=======================================
var regExpSearchResult = recordExists.test(stringToArray[a]);
//=======================================
// If the RegEx search finds a result process it
//=======================================
if (regExpSearchResult == true)
{
//=======================================
//Set two variable for the brackets
//=======================================
var strOne="<";
var strTwo=">";
var finalValueA = stringToArray[a].replace(strOne, "");
var finalValueB = finalValueA.replace(strTwo, "");
//alert("This is final value b: " + finalValueB);
newArrayOfRecords[b] = finalValueB.split(",")
b = b + 1;
myRecord = newArrayOfRecords[b];
//alert("My Record"+myRecord);
}
else
{
//=======================================
// The result of the RegEx was false...
//=======================================
//alert("In the false regex area");
}
}
//=======================================
// Count the size of the newly created array
//=======================================
var countOfNewArrayOfRecords = newArrayOfRecords.length;
//=======================================
// If the count of the array is greater than zero...let's do our stuff
//=======================================
if (countOfNewArrayOfRecords > 0)
{
for ( var x = 0; x < countOfNewArrayOfRecords; ++x)
{
//=======================================
// Get the id of the file
//=======================================
var theFileID = newArrayOfRecords[x][0];
var openTheObject = app.openObject(theFileID);
var theOpenDoc = app.documents.item(0);
if (applicationName == "InDesign"){
// At this point the file has to be dirtied so that the close command will actually
// force Enterprise to create a new version of the file
//=======================================
//create a text frame
//=======================================
var myTextFrame = theOpenDoc.pages.item(0).textFrames.add();
//=======================================
//create a text frame
//=======================================
var deleteMyTextFrame = myTextFrame.remove();
//=======================================
// Uncomment the line below to close the document
//=======================================
var closeTheObject = theOpenDoc.close(SaveOptions.yes);
}
if (applicationName == "InCopy"){
//=======================================
// add and remove some text from the article
//=======================================
var listOfStories = theOpenDoc.stories;
var countOfStories = listOfStories.length;
var firstStory = listOfStories.item(1);
var contentsFirstStory = firstStory.paragraphs.item(0).contents;
var myInsertionPoint = firstStory.insertionPoints.item(0);
myInsertionPoint.contents = "";
//=======================================
// At this point the file has to be dirtied so that the close command will actually
// force Enterprise to create a new version of the file
//=======================================
//=======================================
// Modify metadata if necessary. Example below is 'RouteTo'
//=======================================
//var mdDoc = theOpenDoc.entMetaData;
//alert("Metadata: " + mdDoc);
//var countOfMetadata = mdDoc.length;
//alert("Count: " + countOfMetadata);
//var mdValue1 = mdDoc.item(0); // gets the first extrametadatavalue
//alert("Value 1: " + mdValue1);
//var articleRouteTo = mdDoc.get("RouteTo");
//alert("Value 1: " + articleRouteTo);
//var newRouteTo = mdDoc.set("RouteTo", "newRouteTo");
}
// Close the object
//var closeTheObject = theOpenDoc.close(SaveOptions.yes);
}
} else {
//=======================================
// oh...hey...the work is done and no results were found.
//=======================================
alert("No results were found...");
}
//=======================================
// Set the user interaction level
//=======================================
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.interactWithAll;
}
}
catch(e)
{
desc = e.description;
num = e.number;
alert( 'error ' + num + ': ' + desc );
}