Skip to main content
John_Kordas
Inspiring
June 14, 2009
Answered

JS CS3 Relink image to new path

  • June 14, 2009
  • 1 reply
  • 2271 views

I've put a script together that resizes images in a doc. What I've been able to do is rename the resized image  and then relink to the renamed images. I'd like to be able to have all the resized images saved into the folder using the doc name which is not hard. What I am finding difficult is setting the path to that folder for the relink.

for example:

var myLinks = app.activeDocument.allGraphics;
var myFpath = myLinks[0].itemLink.filePath
var myFname = myLinks[0].itemLink.name

$.write(myFpath.replace(myFname,app.activeDocument.name+"/"+myFname) +"\r");

returns C:\hold\Untitled-1/file_24791_Scaled.tif

if I try and change +"/"+ to +"\"+ the remaining code is commented out I've also tried "/\/" which returned //

should I try using replace to return /C/hold/Untitled-1/file_24791_Scaled.tif which should work on both PC and Mac?

Any suggestions would be appreciated.

Cheers, John.

This topic has been closed for replies.
Correct answer Kasyan Servetsky

It's not totally tested -- don't have a Mac right now, but I would use the following approach:

var myGraphics = app.activeDocument.allGraphics;
var aLink = myGraphics[0].itemLink;
var myFname = aLink.name
var myFpath = aLink.filePath;
var aFile = new File(myFpath);
var aFolder = aFile.parent;
var aFolderPath = aFolder.absoluteURI;
if (File.fs == "Windows") {
    var aNewFolder = aFolderPath + "/" + app.activeDocument.name.match(/(.*)\.[^\.]+$/)[1] +  "/" + myFname;
}
else if (File.fs == "Macintosh") {
    var aNewFolder = aFolderPath + ":" + app.activeDocument.name.match(/(.*)\.[^\.]+$/)[1] +  ":" + myFname;
}
alert(aNewFolder);

Hope this helps.

Kasyan

1 reply

Kasyan Servetsky
Kasyan ServetskyCorrect answer
Legend
June 14, 2009

It's not totally tested -- don't have a Mac right now, but I would use the following approach:

var myGraphics = app.activeDocument.allGraphics;
var aLink = myGraphics[0].itemLink;
var myFname = aLink.name
var myFpath = aLink.filePath;
var aFile = new File(myFpath);
var aFolder = aFile.parent;
var aFolderPath = aFolder.absoluteURI;
if (File.fs == "Windows") {
    var aNewFolder = aFolderPath + "/" + app.activeDocument.name.match(/(.*)\.[^\.]+$/)[1] +  "/" + myFname;
}
else if (File.fs == "Macintosh") {
    var aNewFolder = aFolderPath + ":" + app.activeDocument.name.match(/(.*)\.[^\.]+$/)[1] +  ":" + myFname;
}
alert(aNewFolder);

Hope this helps.

Kasyan

John_Kordas
Inspiring
June 15, 2009

Thanks Kasyan that did the job.

I did have to make a slight change for the Mac process. My script works on resizing tif files so for the PC path I had to use .tif and for the Mac I used .tiff.

I also found that the ":" did not work but "/"did.

if (File.fs == "Windows") {
    var aNewFolder = aFolderPath + "/" + app.activeDocument.name.match(/(.*)\.[^\.]+$/)[1] +  "/" + myFname+"_Scaled.tif";
    }
    else if (File.fs == "Macintosh") {
        var aNewFolder = aFolderPath + "/" + app.activeDocument.name.match(/(.*)\.[^\.]+$/)[1] +  "/" + myFname+"_Scaled.tiff";
    }

Much appreciated for your help.

Kasyan Servetsky
Legend
June 15, 2009

I've written a script that resizes images images too:

http://kasyan.ho.com.ua/resize.html

Check it out, if you are interested.

Kasyan