Skip to main content
Participating Frequently
May 30, 2022
Question

How to write to a relative path with PS actions

  • May 30, 2022
  • 5 replies
  • 1987 views

Hi,

What is the way to do the above? PS always looks for an absolute path, therefore all my actions break.
Any easy fix?

Thanks

5 replies

Participant
October 9, 2025

If you mean building an absolute path from a relative path, I can offer a method. You first need to build the relative paths relative to the very script you are gonna run with. Then do these: 

1. Use `$fileName` to get the full path with file name of the current script. We have a base now.

2. Use `new File($fileName)` or `File($fileName)` to get a file object, on which we can use `.parent` to get the absolute path.

3. Use ` '/' ` and `+` to connect the absolute path and your relative path. And use `new Folder` or `Folder` (if your relative path is a folder), or use `new File` or `File` (if you relative path contains the file name).

 

So it would look like this at last: `new File(new File($.fileName).parent +  relativePath (eg. '/../../Template/YourPSFile.psd') `. This gets you an absolute path at runtime, like you can use `app.open(new File(new File($.fileName).parent +  relativePath)` to open the file.

Genius
October 9, 2025

Yeah, that's not how it works.

If a document is open, we can get that path easily.

Keep in mind that this is a three year old post.

c.pfaffenbichler
Community Expert
Community Expert
May 31, 2022

Please post a screenshot of the fully expanded Action in the Actions Panel and describe what you want it to achieve differently. 

c.pfaffenbichler
Community Expert
Community Expert
May 30, 2022

This Script would save a jpg in the same Folder as the active Document (and if that has no path yet to the Desktop). 

Save the code as a txt-file, change the extension to .jsx and copy it into Photoshop’s Presets/Scripts-Folder; after restarting Photoshop it should be available under File > Scripts and can be assigned a Shortcut or used in an Action. 

// saves jpg into same folder;
// be advised: this  overwrites existing jpgs of the same name without prompting. 
// 2010, use it at your own risk;
#target photoshop;
if (app.documents.length > 0) {
var thedoc = app.activeDocument;
// getting the name and location;
var docName = thedoc.name;
if (docName.indexOf(".") != -1) {var basename = docName.match(/(.*)\.[^\.]+$/)[1]}
else {var basename = docName};
// getting the location, if unsaved save to desktop;
try {var docPath = thedoc.path}
catch (e) {var docPath = "~/Desktop"};
// jpg options;
var jpegOptions = new JPEGSaveOptions();
jpegOptions.quality = 9;
jpegOptions.embedColorProfile = true;
jpegOptions.matte = MatteType.NONE;
//save jpg as a copy:
thedoc.saveAs((new File(docPath+'/'+basename+'.jpg')),jpegOptions,true);
//that’s it; thanks to xbytor;
};

 

thanuleeAuthor
Participating Frequently
May 30, 2022

Thanks so this is for writing specifically a jpeg?
It is not what I need though, I would need a script to just say "write whatever I define  in the relative path"

I would just need the "Save as" part but to a relative path. I use this for many different extensions. So i guess its not possible and requires different scripts each time?

c.pfaffenbichler
Community Expert
Community Expert
May 30, 2022

The Script is for jpg-creation. 

 

I don’t seem to understand what you are trying to automate. 

If you want to give manual input in the Save As-dialog anyway what is the problem to begin with? 

Maybe you could to explain the issue, possibly post meaningful screenshots or sketches to illustrate? 

Stephen Marsh
Community Expert
Community Expert
May 30, 2022

AFAIK, you don't/can't do so with an action... Where are you using the absolute path in the action? In a save step, elsewhere?

 

You can turn on the modal control for an interactive override in the action.

 

Or you can use the "override action step" in Batch for recorded open or save commands.

 

Or you can use the "Insert menu item" command to place a generic open or save step into an action.

 

Otherwise, this is available via scripting, however it depends on the context of the step in the action on where/how you would use a scripted step recorded into the action.

thanuleeAuthor
Participating Frequently
May 30, 2022

I only use it during save yes.
So whats the way of doing that without code (if possible)? thanks

c.pfaffenbichler
Community Expert
Community Expert
May 30, 2022

Will you please explain the exact scenario already? 

Save a Copy or Save As (or even Export …), file format, target position relative to original file, …? 

c.pfaffenbichler
Community Expert
Community Expert
May 30, 2022

Are you familiar with JavaScript? 

thanuleeAuthor
Participating Frequently
May 30, 2022

No but if there is an easy fix i ll take it

c.pfaffenbichler
Community Expert
Community Expert
May 30, 2022

I see no immediate, realistic option except Scripting and that is best done with JavaScript, but Visual Basic and AppleScript could also work. 

 

What is the exact relationship of the file’s position and the target location? 

What is the target file format?