zlep01
Goto Top

Javascript für InDesign (Automatisch neuen Verknüpfungspfad aktualisieren) - Bräuchte Hilfe bei der Anpassung eines vorhandenen Scripts

Hallo,

ich versuche derzeit die Verknüpfungen meiner Bilder in InDesign mithilfe eines Scripts automatisch zu aktualisieren. Im Moment sind diese auf

C:\Artikelbilder\Bilder\Artikelnummer\Bild.psd

verknüpft.

Nun möchte ich den Pfad aller Bilder vollständig ändern. Gleich bleibt der eigentliche Dateiname, also:

D:\Artikelbilder\Artikelnummer\Bild.psd

Ich habe bereits ein Script gefunden, das funktioniert, allerdings nur, wenn die Datei nicht noch in einem weiteren Unterordner liegt, also so: "D\Artikelbilder\Bild.psd":

//Relink2NewPath.jsx 
 
var myDoc = app.documents;  
var myFolder = Folder.selectDialog("Neuer Pfad zu dem Bildern")+"";     
myLinks = myDoc.links;  
for (oneLink=myLinks.length-1;oneLink>-1;oneLink--) {  
	myLink = myLinks[oneLink];  
	myName = String(File.encode(myLink.name));  
	myNewLink = File(myFolder + "/" + myName);    
	try{  
		myLink.relink(myNewLink);  
		myLink.update();  
	}  
	catch(e){}  
}

Gibt es eine Möglichkeit dieses Script so anzupassen, dass es auch Unterordner durchsucht, also ""D\Artikelbilder\Artikelnummer\Bild.psd"?

Vielen Dank bereits im Voraus.

Content-Key: 309071

Url: https://administrator.de/contentid/309071

Printed on: April 27, 2024 at 00:04 o'clock

Mitglied: 129813
129813 Jul 06, 2016 at 06:36:45 (UTC)
Goto Top
Hi.
you should define a variable which is the current rootpath of the links:
var oldroot = 'd:\Artikelbilder';  
Then do a "replace" of this folder in the filePath property of the links
myNewLink = File(myLink.filePath.replace(oldroot, myFolder) + "/" + myName);     
Regards
Member: zlep01
zlep01 Jul 06, 2016 at 09:52:39 (UTC)
Goto Top
Hi, thanks. I tried this as you recommended:

<script>
//Relink2NewPath.jsx

var myDoc = app.documents;
var oldroot = 'D:\Artikelbilder';
var myFolder = Folder.selectDialog("Neuer Pfad zu dem Bildern")+"";
myLinks = myDoc.links;
for (oneLink=myLinks.length-1;oneLink>-1;oneLink--) {
myLink = myLinks[oneLink];
myName = String(File.encode(myLink.name));
myNewLink = File(myLink.filePath.replace(oldroot, myFolder) + "/" + myName);
try{
myLink.relink(myNewLink);
myLink.update();
}
catch(e){}
}
</script>

Unfortunately it did not work. Not sure what I did wrong but it seems that it does still not search the subfolders in the new directory.
Any help would be appreciated. Thanks in advance.
Mitglied: 129813
129813 Jul 06, 2016 updated at 10:50:06 (UTC)
Goto Top
Ok, i see the problem, the paths in Indesign are written a little bit different, you have to write the old path in this format:
var oldPath = '/d/Artikelbilder';
This worked with the active document without problems:
var myDoc = app.activeDocument;
var oldPath = '/d/Artikelbilder';  
var myFolder = Folder.selectDialog("Neuer Pfad zu dem Bildern");     
for(var i = 0;i < myDoc.links.count();i++){
    var myLink = myDoc.links[i];
    var currentPath = File(myLink.filePath).path;
    var myNewLink = File(currentPath.replace(oldPath,myFolder) + "/" + myLink.name);  
    try{
        myLink.relink(myNewLink);
	myLink.update();
     }catch(e){
       alert(e);
    }
}
Member: zlep01
zlep01 Jul 06, 2016 at 12:20:48 (UTC)
Goto Top
Hi, thanks.

Might be my document (too many images?) but unfortunately it's still not working for me. The script crashed after a while.
Do I really need the "var oldPath"? Because the original script worked with all images which aren't in subfolders. I thought it would be possible to just change the script a little bit so that it also searches for images in subfolders.

Thanks again for your help.
Mitglied: 129813
129813 Jul 06, 2016 updated at 12:36:16 (UTC)
Goto Top
Zitat von @zlep01:
Might be my document (too many images?) but unfortunately it's still not working for me. The script crashed after a while.
Do I really need the "var oldPath"?
Yes you need it ...
Because the original script worked with all images which aren't in subfolders.
Yes but this is another condition, the script now needs to know which part of the path it needs to replace with the new one now, guy!
I thought it would be possible to just change the script a little bit so that it also searches for images in subfolders.
Nothing is "searched" in the script, the old path is only interchanged with a the new one you choose! But if you have subfolders then the script needs to know which is the root of the old folder.

Put all other commands also in the try catch part then you see why it is not working with your document.

var myDoc = app.activeDocument;
var oldPath = '/d/Artikelbilder';  
var myFolder = Folder.selectDialog("Neuer Pfad zu dem Bildern");     
for(var i = 0;i < myDoc.links.count();i++){
    try{
        var myLink = myDoc.links[i];
        var currentPath = File(myLink.filePath).path;
        var myNewLink = File(currentPath.replace(oldPath,myFolder) + "/" + myLink.name);  
        myLink.relink(myNewLink);
	myLink.update();
     }catch(e){
       alert(e);
    }
}
Member: zlep01
zlep01 Jul 06, 2016 at 13:59:07 (UTC)
Goto Top
I guess I'm doing something wrong . If my old path is a little bit more complicated than my example above, how do I have to write it in the script exactly?

My images are in "P:\02_Kataloge\01_Gesamtkataloge\Katalog Nr 9\InDesign\Firma 2015\01 Thema_2015\Links" at the moment. That's the old path.

I tried this:

var oldPath = '/p/02_Kataloge\01_Gesamtkataloge\Katalog Nr 9\InDesign\Firma 2015\01 Thema_2015\Links';

as well as

var oldPath = '/p/02_Kataloge/01_Gesamtkataloge/Katalog Nr 9/InDesign/Firma 2015/01 Thema_2015/Links';

Both did not work. There's no error message.

My new path is "P:\00_Artikelbilder" and then obviously a subfolder with "Artikelnummer\Artikelnummer.psd" -> P:\00_Artikelbilder\Artikelnummer\Artikelnummer.psd
Mitglied: 129813
129813 Jul 06, 2016 updated at 17:19:49 (UTC)
Goto Top
As i said file paths have another format especially when using special chars like spaces. Encode the file path:
var oldPath = String(File.encode('/p/02_Kataloge/01_Gesamtkataloge/Katalog Nr 9/InDesign/Firma 2015/01 Thema_2015/Links'));  
or also this is possible:
var oldPath = String(File.encode('P:\\02_Kataloge\\01_Gesamtkataloge\\Katalog Nr 9\\InDesign\\Firma 2015\\01 Thema_2015\\Links'));  
Have a look at the reference!
http://jongware.mit.edu/idcs5js/pc_File.html

If you want you can also replace this static variable by the same dialog for choosing the target folder, like in this example:
var myDoc = app.activeDocument;
var myCurrentRoot = Folder.selectDialog("Jetziger Pfad zu den Bildern");  
var myTargetRoot = Folder.selectDialog("Neuer Pfad zu dem Bildern");     
for(var i = 0;i < myDoc.links.count();i++){
    try{    
        var myLink = myDoc.links[i];
        var currentPath = File(myLink.filePath).path;
        var myNewLink = File(currentPath.replace(myCurrentRoot,myTargetRoot) + "/" + myLink.name);   
	myLink.relink(myNewLink);
	myLink.update();
    }catch(e){
        alert(e);
    }
}
This all works without problems and is tested locally with InDesign CS6.

I have said all now, i'm out. If you need more support you can contact me via PM but that's not for free anymore.

Happy coding face-wink

Regards
highload
Member: zlep01
zlep01 Jul 07, 2016 updated at 06:11:42 (UTC)
Goto Top
Still not working for subfolders.
Thanks anyway.
Mitglied: 129813
129813 Jul 07, 2016 updated at 06:51:08 (UTC)
Goto Top
I tested it, 100% working,sorry !!! I can show you a video that it's working if you wish....
This script is only replacing the old root path with the new one, so nothing special and very easy !! The rest of the path part (the subfolders) remain the same, so exactly what you want.
You should take a JavaScript course to understand the script.
Member: zlep01
zlep01 Jul 07, 2016 at 07:21:52 (UTC)
Goto Top
It's working if the images are not in subfolders, but that's not what I need (see post 1).
My images are now in subfolders. Before, they alll were in one folder.

BEFORE:
P:\xxxx/xxxx/xxxx/xxxxx/xxxx/image1234.psd
P:\xxxx/xxxx/xxxx/xxxxx/xxxx/image2345.psd
P:\xxxx/xxxx/xxxx/xxxxx/xxxx/image3456.psd


NOW:
P:\images/image1234/image1234.psd
P:\images/image2345/image2345.psd
P:\images/image3456/image3456.psd

Your script is working perfectly, if the images are in ONE folder but not if they are in different subfolders.
Mitglied: 129813
129813 Jul 07, 2016 updated at 08:18:14 (UTC)
Goto Top
Zitat von @zlep01:

It's working if the images are not in subfolders, but that's not what I need (see post 1).
I know i tested it explicitly with this condition!
My images are now in subfolders. Before, they alll were in one folder.

BEFORE:
P:\xxxx/xxxx/xxxx/xxxxx/xxxx/image1234.psd
P:\xxxx/xxxx/xxxx/xxxxx/xxxx/image2345.psd
P:\xxxx/xxxx/xxxx/xxxxx/xxxx/image3456.psd
You are mixing backslash and slash !

NOW:
P:\images/image1234/image1234.psd
P:\images/image2345/image2345.psd
P:\images/image3456/image3456.psd
Man this is a totally different story!!!!! You want the script to use the image name as the subfolder in the target ?? I was thinking the subfolder structure stays the same in the target folder !! This script replaces only parts of the path it does not search files.

So please describe your intention exactly, before i'm again writing scripts for nothing face-sad, thanks....
Mitglied: 129813
129813 Jul 08, 2016 at 10:57:14 (UTC)
Goto Top
So what's the job now ?