Script to place image multiple times and create different hyperlinks on from txt file...need help

Explorer ,
May 12, 2021 May 12, 2021

Copy link to clipboard

Copied

hi

I have script which create hyperlinks over placed images from txt file. image.png(tab)httplink.

thanks again Kasyan!!!

1.png https://www.yahoo.com
2.png https://www.google.hr

 

/* Copyright 2018, Kasyan Servetsky
July 29, 2018
Written by Kasyan Servetsky
http://www.kasyan.ho.com.ua
e-mail: askoldich@yahoo.com */


//======================================================================================
var scriptName = "Make web-hyperinks",
debugMode = true, // for debugging only
count = 0,
doc = app.activeDocument;

PreCheck()
/*
var myPDFExportPreset = app.pdfExportPresets.item("Za sve namjene - 300ppi")
app.activeDocument.exportFile(ExportFormat.pdfType, app.activeDocument.filePath.fsName +"\\", true, myPDFExportPreset);
*/

//===================================== FUNCTIONS ======================================
function Main() {
	var link, url, name,
	//dataFile = File.openDialog("Locate the .txt file", "Txt files:*.txt, All files:*.*");
    dataFile = new File ("\\\\tartar\\_Razmjena\\_Zappar-kodovi\\_zappar-kodovi_database.txt");

    var log=["Linkovi koji nemaju URL:"];
	
	if (dataFile != null) {
		var data = GetData(dataFile);
		
		var links = doc.links;
		
		var progressWin = new Window("window", scriptName);
		var progressBar = progressWin.add("progressbar" , undefined, 0, links.length);
		progressBar.preferredSize.width = 300;
		var progressTxt = progressWin.add("statictext", undefined,  "");
		progressTxt.alignment = "fill";
		progressWin.show();
		
		for (var i = 0; i < links.length; i++) {
			link = links[i];
			name = link.name;
			progressBar.value = (i + 1);
			progressTxt.text = name;
			url = GetUrl(name, data);
			
			if (url != null) {
                if (url != "") {
				MakeWebHyperlink(link.parent, url, name);
                } else {
                   log.push("\n" + name) 
                }
            }
		}
		
		progressWin.close();
		var report = count + " hyperlink" + ((count == 1) ? " was" : "s were") + " created.";
		alert(report, scriptName);
         MakeLogFile(log)
		
	}
	
}

//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetData(dataFile) {
	
	if (dataFile.exists) {
		dataFile.open("r");
		var n = 0;
		var textLine;
		var data = [];
        
        
		while (!dataFile.eof) {
			textLine = dataFile.readln().replace (/ *\t */, "\t")
			data.push(textLine.split("\t"))
			}
		}	
		dataFile.close();
	return data;
}


//--------------------------------------------------------------------------------------------------------------------------------------------------------
function GetUrl(name, data) {
	var url = null;
	for (x in data) {
		if (name.toLowerCase() == data[x][0].toLowerCase()) {
			url = data[x][1];
		}
	}

	return url;
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function MakeWebHyperlink(container, url, name) {
	var hasHyperlink = false
	for (var i = 0; i <= doc.hyperlinks.length-1; i++) {
		if (doc.hyperlinks[i].source == "[object HyperlinkPageItemSource]") {
			if (doc.hyperlinks[i].source.sourcePageItem == container) {
				hasHyperlink = true	
				doc.hyperlinks[i].destination.destinationURL = url
				count++;
			}		
		}
	}
	
	if (hasHyperlink == false) {

		try {
			var oriName = name;

			if (doc.hyperlinks.itemByName(name) != null) {
				
				var increment = 1;
				while (doc.hyperlinks.itemByName(oriName) != null) {
					oriName = oriName + " (" + increment++ + ")";
				}
				//doc.hyperlinks.itemByName(name).destination.destinationURL = url
				//count++;
			
			} 
		
			var source = doc.hyperlinkPageItemSources.add(container);
			var destination = doc.hyperlinkURLDestinations.add(url, {hidden: true}); // 'hidden: true' -- to turn 'shared hyperlink' off
			var hyperlink = doc.hyperlinks.add(source, destination);

			try {
				hyperlink.name = oriName;
			}
			catch(err) {
				if (debugMode) $.writeln(err.message + ", line: " + err.line);				
				hyperlink.name = name + "[" + GetRandomInt(1000000000) + "]";
				if (debugMode) $.writeln("New hyperlink.name: " + hyperlink.name);	
			}
			
			if (hyperlink.isValid) {
				count++;
			}
			
		}
		catch(err) {
			if (debugMode) $.writeln(err.message + ", line: " + err.line + " | URL: " + url);
			//ErrorLog(err.message + ", line: " + err.line + " | URL: " + url);
		}
	}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function PreCheck() {
	if (app.documents.length == 0) ErrorExit("Please open a document and try again.", true);
	doc = app.activeDocument;
	if (doc.converted) ErrorExit("The current document has been modified by being converted from older version of InDesign. Please save the document and try again.", true);
	if (!doc.saved) ErrorExit("The current document has not been saved since it was created. Please save the document and try again.", true);
	if (File.fs == "Macintosh") ErrorExit("This script for Windows only.", true);
	Main();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function ErrorExit(error, icon) {
	alert(error, scriptName, icon);
	exit();
}
//--------------------------------------------------------------------------------------------------------------------------------------------------------
function MakeLogFile(log) {
	logFile=new File (app.activeScript.parent + "/log.txt")
    logFile.open("w");
    logFile.write(log)
    logFile.execute()
}

 

Can someone help to modify this script.

What I need is this...txt file look like this:

httplink;nameoflink;pageinindesign;iconimagetype.png

www.google.hr;google;35;word.png

www.cnn.hr;cnn;67;excel.png

 

Possible that i have more word or excel icons (or other types, up to 15) on same page which need to be placed one bellow other in upper left (odd page) or upper right corner (even pages) on page, on new layer with name "ikone"...example is in attach...icone image is always the same (word.png) but hyperlink change...everytime when script is run, layer "ikone" is deleted and script create all again...this is because i need to update documents very often.

many thanks

TOPICS
Scripting

Views

98

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation