Created
November 23, 2015 16:24
-
-
Save benadamstyles/35bad0defa421657198c to your computer and use it in GitHub Desktop.
InDesign: CreateCrosslink
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crosslinkStyleCached | |
function getCrosslinkStyle(doc) { | |
if (!crosslinkStyleCached) crosslinkStyleCached = doc.paragraphStyles.item('crosslink') | |
return crosslinkStyleCached | |
} | |
function createHyperlink(dest, name, doc) { | |
var ip = app.selection[0], | |
story = ip.parentStory, | |
startOfName = story.characters[ip.index + name.length - 1 + 2]; | |
ip.contents = '(' + name + ')\r' | |
app.select(story.characters.itemByRange(startOfName, ip)) | |
var text = app.selection[0], | |
source = doc.hyperlinkTextSources.add(text); | |
text.applyParagraphStyle(getCrosslinkStyle(doc), true) | |
doc.hyperlinks.add(source, dest) | |
} | |
function process(name) { | |
var doc = app.activeDocument, | |
dests = doc.hyperlinkTextDestinations, | |
arr = [], | |
radioGroup; | |
for (var i = 0; i < dests.length; i++) { | |
arr.push(dests[i]) | |
} | |
var filtered = arr.filter(function(dest) { | |
return ~dest.name.toLowerCase().indexOf(name.toLowerCase()) | |
}) | |
if (filtered.length === 1) { | |
createHyperlink(filtered[0], name, doc) | |
} else if (filtered.length > 1) { | |
var dialog = app.dialogs.add({ | |
name: 'Choose Hyperlink Destination', | |
canCancel: true | |
}), | |
column = dialog.dialogColumns.add(); | |
radioGroup = column.radiobuttonGroups.add() | |
filtered.forEach(function(item) { | |
var button = radioGroup.radiobuttonControls.add({ | |
staticLabel: item.name | |
}) | |
}) | |
if (dialog.show() === true) { | |
createHyperlink(filtered[radioGroup.selectedButton], name, doc) | |
} | |
dialog.destroy() | |
} else alert('No Hyperlink Destinations found') | |
} | |
function main() { | |
if (app.documents.length > 0) { | |
var dialog = app.dialogs.add({ | |
name: 'Hyperlink Destination Name input', | |
canCancel: true | |
}), | |
column = dialog.dialogColumns.add(), | |
myTextEditField = column.textEditboxes.add({ | |
// editContents: "", | |
minWidth: 180 | |
}); | |
if (dialog.show() === true) { | |
var HyperlinkDestinationName = myTextEditField.editContents | |
dialog.destroy() | |
process(HyperlinkDestinationName) | |
} else dialog.destroy() | |
} else alert("No documents are open. Please open a document and try again.") | |
} | |
if (!Array.prototype.forEach) { | |
Array.prototype.forEach = function(callback, thisArg) { | |
var T, k; | |
if (this == null) { // jshint ignore:line | |
throw new TypeError(' this is null or not defined'); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof callback !== "function") { | |
throw new TypeError(callback + ' is not a function'); | |
} | |
if (arguments.length > 1) { | |
T = thisArg; | |
} | |
k = 0; | |
while (k < len) { | |
var kValue; | |
if (k in O) { | |
kValue = O[k]; | |
callback.call(T, kValue, k, O); | |
} | |
k++; | |
} | |
}; | |
} | |
if (!Array.prototype.map) { | |
Array.prototype.map = function(callback, thisArg) { | |
var T, A, k; | |
if (this == null) { // jshint ignore:line | |
throw new TypeError(' this is null or not defined'); | |
} | |
var O = Object(this); | |
var len = O.length >>> 0; | |
if (typeof callback !== 'function') { | |
throw new TypeError(callback + ' is not a function'); | |
} | |
if (arguments.length > 1) { | |
T = thisArg; | |
} | |
A = new Array(len); | |
k = 0; | |
while (k < len) { | |
var kValue, mappedValue; | |
if (k in O) { | |
kValue = O[k]; | |
mappedValue = callback.call(T, kValue, k, O); | |
A[k] = mappedValue; | |
} | |
k++; | |
} | |
return A; | |
}; | |
} | |
if (!Array.prototype.find) { | |
Array.prototype.find = function(predicate) { | |
if (this === null) { | |
throw new TypeError('Array.prototype.find called on null or undefined'); | |
} | |
if (typeof predicate !== 'function') { | |
throw new TypeError('predicate must be a function'); | |
} | |
var list = Object(this); | |
var length = list.length >>> 0; | |
var thisArg = arguments[1]; | |
var value; | |
for (var i = 0; i < length; i++) { | |
value = list[i]; | |
if (predicate.call(thisArg, value, i, list)) { | |
return value; | |
} | |
} | |
return undefined; | |
}; | |
} | |
if (!Array.prototype.filter) { | |
Array.prototype.filter = function(fun/*, thisArg*/) { | |
'use strict'; | |
if (this === void 0 || this === null) { | |
throw new TypeError(); | |
} | |
var t = Object(this); | |
var len = t.length >>> 0; | |
if (typeof fun !== 'function') { | |
throw new TypeError(); | |
} | |
var res = []; | |
var thisArg = arguments.length >= 2 ? arguments[1] : void 0; | |
for (var i = 0; i < len; i++) { | |
if (i in t) { | |
var val = t[i]; | |
if (fun.call(thisArg, val, i, t)) { | |
res.push(val); | |
} | |
} | |
} | |
return res; | |
}; | |
} | |
if (!Object.keys) { | |
Object.keys = (function() { | |
'use strict'; | |
var hasOwnProperty = Object.prototype.hasOwnProperty, | |
hasDontEnumBug = !({ toString: null }).propertyIsEnumerable('toString'), | |
dontEnums = [ | |
'toString', | |
'toLocaleString', | |
'valueOf', | |
'hasOwnProperty', | |
'isPrototypeOf', | |
'propertyIsEnumerable', | |
'constructor' | |
], | |
dontEnumsLength = dontEnums.length; | |
return function(obj) { | |
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) { | |
throw new TypeError('Object.keys called on non-object'); | |
} | |
var result = [], prop, i; | |
for (prop in obj) { | |
if (hasOwnProperty.call(obj, prop)) { | |
result.push(prop); | |
} | |
} | |
if (hasDontEnumBug) { | |
for (i = 0; i < dontEnumsLength; i++) { | |
if (hasOwnProperty.call(obj, dontEnums[i])) { | |
result.push(dontEnums[i]); | |
} | |
} | |
} | |
return result; | |
}; | |
}()); | |
} | |
if (!String.prototype.trim) { | |
String.prototype.trim = function () { | |
return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, ''); | |
}; | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment