Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to open a file from file path using action

Participant ,
Sep 05, 2023 Sep 05, 2023

hi all,
how can we open the file from the file path using script or action.
please help me to get this.
Thank You

TOPICS
Actions and scripting
1.1K
Translate
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

correct answers 1 Correct answer

Community Expert , Sep 06, 2023 Sep 06, 2023

@Mahesh12 

 

Then you need to loop over the input files.

 

I'll post some code as an example...

 

EDIT: here is one way to open a folder of files and filter by file type and sort or reverse sort:

 

 

#target photoshop

// Set the input folder
var inputFolder = Folder.selectDialog("Please select the input folder:");

// Limit the input files, add or remove extensions as required
var fileList = inputFolder.getFiles(/\.(webp|tif|tiff|jpg|jpeg|psd|psb|png)$/i);

//fileList.sort().reverse();
fileList
...
Translate
Adobe
Community Expert ,
Sep 05, 2023 Sep 05, 2023

For an action, just record the step of opening a file. That being said, actions are tricky when it comes to file paths. They are platform dependent and often dependent on the computer recording the action.


One way to open from a static path using scripting:

 

var theFilePath = "~/Desktop/image001.jpg";
open(File(theFilePath));

// or

open(File("~/Desktop/image001.jpg"));

 

Or via a dialog:

 

var selectFile = File.openDialog("Please select the file:");
open(selectFile);

// or

var selectFile = app.openDialog();
open(File(selectFile));


// or

executeAction(stringIDToTypeID("open"), new ActionDescriptor(), DialogModes.NO);

 

 

Translate
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
Participant ,
Sep 05, 2023 Sep 05, 2023

hi @Stephen Marsh 
what i want is i have one folder which have 100 document. so it should automatically open the file and close the file without saving.

Thank You

 

Translate
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
Community Expert ,
Sep 05, 2023 Sep 05, 2023
quote

hi @Stephen Marsh 
what i want is i have one folder which have 100 document. so it should automatically open the file and close the file without saving.

Thank You

 


By @Mahesh12

 

 

Script automation generally requires you to "do something" in-between opening and closing a file... Otherwise, why bother?

 

To close an open file without saving (wrapped in a try/catch so that a conditional check for an open doc isn't required):

 

try {
    activeDocument.close(SaveOptions.DONOTSAVECHANGES);
} catch (e) {}

 

So I don't know what else you need, this isn't really complete code.

 

What do you need to do between opening a file and closing it without saving?

Translate
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
Participant ,
Sep 05, 2023 Sep 05, 2023

hi @Stephen Marsh  
when i open the document i will retrieve some data from the active document and after retrieving i want to close the the document without saving.

function dupComponentChannels(name) {
try {
var idDplc = charIDToTypeID("Dplc");
var desc8 = new ActionDescriptor();
var idnull = charIDToTypeID("null");
var ref4 = new ActionReference();
var idChnl = charIDToTypeID("Chnl");
var idOrdn = charIDToTypeID("Ordn");
var idTrgt = charIDToTypeID("Trgt");
ref4.putEnumerated(idChnl, idOrdn, idTrgt);
desc8.putReference(idnull, ref4);
var idNm = charIDToTypeID("Nm ");
desc8.putString(idNm, name);
executeAction(idDplc, desc8, DialogModes.NO);
} catch (e) {
$.writeln("dupComponentChannels => Line #:" + e.line + ", Message:" + e);
}
}
the above code will duplicate the channel.
so can i get code like above to open and close the document .
Thank you
Translate
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
Community Expert ,
Sep 06, 2023 Sep 06, 2023
LATEST

@Mahesh12 

 

Then you need to loop over the input files.

 

I'll post some code as an example...

 

EDIT: here is one way to open a folder of files and filter by file type and sort or reverse sort:

 

 

#target photoshop

// Set the input folder
var inputFolder = Folder.selectDialog("Please select the input folder:");

// Limit the input files, add or remove extensions as required
var fileList = inputFolder.getFiles(/\.(webp|tif|tiff|jpg|jpeg|psd|psb|png)$/i);

//fileList.sort().reverse();
fileList.sort();

var savedDisplayDialogs = app.displayDialogs;
app.displayDialogs = DialogModes.NO;

// Set the file processing counter
var fileCounter = 0;

// Forward loop over the input files
for (var i = 0; i < fileList.length; i++) {

    open(fileList[i]);

    /*
    YOUR CODE HERE
    */

    activeDocument.close(SaveOptions.DONOTSAVECHANGES);
    fileCounter++;
}

app.displayDialogs = savedDisplayDialogs;
alert('Script completed!' + '\n' + fileCounter + ' files processed!');

 

This variation allows one to manually select one or more files to process (instead of processing a folder):

 

#target photoshop

(function () {

    // Select the files
    var selectFiles = File.openDialog("Please select the file or files:", Multiselect = true);
    if (selectFiles === null) {
        //alert("Script cancelled!");
        return;
    }

    //selectFiles.sort().reverse();
    selectFiles.sort();

    var savedDisplayDialogs = app.displayDialogs;
    app.displayDialogs = DialogModes.NO;

    // Set the file processing counter
    var fileCounter = 0;

    // Forward loop over the input files
    for (var i = 0; i < selectFiles.length; i++) {

        open(selectFiles[i]);

        /*
        YOUR CODE HERE
        */

        activeDocument.close(SaveOptions.DONOTSAVECHANGES);
        fileCounter++;
    }

    app.displayDialogs = savedDisplayDialogs;
    alert('Script completed!' + '\n' + fileCounter + ' files processed!');

})();

 

Translate
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
Community Expert ,
Sep 05, 2023 Sep 05, 2023

Have you considered using scripts like Image Processor Pro to make your life easier? It can open each file from a folder without a single line of code. During batch processing, you can run actions to accomplish whatever task you want and close the file without saving.

 

Maybe we can better assist you if you can explain entire process and scope of batch.

Translate
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
Community Expert ,
Sep 05, 2023 Sep 05, 2023

Please don’t forget to mark @Stephen Marsh ’s post as »Correct Answer« or, if something should be missing, elaborate on the exact task. 

Translate
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