Skip to content

Instantly share code, notes, and snippets.

@benadamstyles
Last active June 21, 2018 13:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benadamstyles/ddb93fa3c68f2d64ee30 to your computer and use it in GitHub Desktop.
Save benadamstyles/ddb93fa3c68f2d64ee30 to your computer and use it in GitHub Desktop.
var uuidCount = 0
function uuid() {
return uuidCount++
}
function arrayFrom(arrayLike) {
var array = []
for (var i = 0; i < arrayLike.length; i++) {
array.push(arrayLike[i])
}
return array
}
function getDest(text, doc) {
var dests = arrayFrom(doc.hyperlinkTextDestinations)
var matcher = text.contents.toLowerCase().trim()
var filtered = dests.filter(function(dest) {
return ~dest.name.toLowerCase().indexOf(matcher)
})
if (filtered.length < 1) {
filtered = dests.filter(function(dest) {
return ~matcher.indexOf(dest.name.toLowerCase().trim())
})
}
if (filtered.length === 1) {
return filtered[0]
} else if (filtered.length > 1) {
var dialog = app.dialogs.add({
name: 'Choose Hyperlink Destination',
canCancel: true,
})
var column = dialog.dialogColumns.add()
var staticText = column.staticTexts.add({
staticLabel: 'for text: ' + text.contents,
})
var radioGroup = column.radiobuttonGroups.add()
filtered.forEach(function(item) {
radioGroup.radiobuttonControls.add({
staticLabel: item.name,
})
})
if (dialog.show() === true && radioGroup.selectedButton >= 0) {
return filtered[radioGroup.selectedButton]
} else {
throw new Error('Cancelled')
}
dialog.destroy()
} else {
throw new Error(
'No Hyperlink Destinations found for text: ' + text.contents
)
}
}
function singleLink(doc, text) {
var dest = getDest(text, doc)
var source = doc.hyperlinkTextSources.add(text)
try {
doc.hyperlinks.add(source, dest, {
name: text.contents,
})
} catch (err) {
alert(err)
doc.hyperlinks.add(source, dest, {
name: text.contents + ' ' + uuid(),
})
}
}
function main() {
var doc = app.activeDocument
if (app.documents.length > 0) {
var text = app.selection[0]
if (text.length < 1) return alert('No text selected')
try {
if (text.paragraphs.count() > 1) {
if (confirm('Should I create multiple hyperlinks?')) {
arrayFrom(text.paragraphs).forEach(function(para) {
singleLink(doc, para)
})
} else {
singleLink(doc, text)
}
} else {
singleLink(doc, text)
}
} catch (e) {
alert(e + '\n\nScript stopped at this point.')
}
} 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