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

Photoshop Javascript - file exists...

Engaged ,
Oct 20, 2021 Oct 20, 2021

Copy link to clipboard

Copied

Hey

 

I just can't get it to work. Which mean its either a bug, or I'm missing a Elephant in my room.

Neither of these commented out commands work >

 

function getUniqueSavePath(path, name) {
    var filePath = path+name+".psb"
    filePath = filePath.replace("//","/")
    //filePath = filePath.replace("/","\\")
    var docFile = File(filePath)
    var inx = 0
    while (docFile.exists) { // docFile.exists// docFile.constructor == File
        inx = inx + 1
        filePath = path + name + "_" + String(inx) + ".psb"
        filePath = filePath.replace("//","/")
        //filePath = filePath.replace("/","\\")
        docFile = File(filePath) // Folder
    }
    if (inx > 0) {
        return name + "_" + String(inx)
    }
    return name
}

 

I'm on windows
I tried Fle, Folder, contrusctor, etc etc. Everything return False. Why is it so hard?
TOPICS
Actions and scripting , Windows

Views

688

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
Adobe
LEGEND ,
Oct 20, 2021 Oct 20, 2021

Copy link to clipboard

Copied

Assuming you want to replace every backslash with a forward slash, I'm not sure what you are trying to accomplish though

 

h = h.replace(/\\/g, '/');

Votes

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
Engaged ,
Oct 20, 2021 Oct 20, 2021

Copy link to clipboard

Copied

Hey, Yeah I though he would replace all // with /, not sure, I never really pay too much attention to slashes, they usually work for me, or the simple repalce I did above does the trick. I mostly do c++/python tho.Thank you for the snippet above!

Votes

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
Guide ,
Oct 20, 2021 Oct 20, 2021

Copy link to clipboard

Copied

What are you passing to the function as path and name? If path is a Folder object, then it doesn't have a slash in the end, and when you combine it with name, you get the wrong path. May be this is better:

 

var filePath = decodeURI(path) +'/' +name+".psb"

 


P.S if you have not saved the file with the new name, why are you wondering that docFile.exist == false?

Votes

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
Engaged ,
Oct 20, 2021 Oct 20, 2021

Copy link to clipboard

Copied

Path was just:

g:/test/some/folder/lala + name +".psb"

So final string was something like:

"g:/test/some/folder/lala/compName.psb" = network folder location

neither File nor Folder are able to detect it properly.

This is automated saving system, but a file may exist from early saves, so I'm just doing "savety" copy in case the file already exist so I dont overwrite it.

I will try decodeURI, thanks!

Votes

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
Guide ,
Oct 20, 2021 Oct 20, 2021

Copy link to clipboard

Copied

I checked - your code works for me. I create a directory z:/test/some/folder/lala/ on the network drive, put the compName.psb file there, call the script with the command getUniqueSavePath ("z:/test/some/folder/lala/", "compName"), script returns the name compName_1

 

Can you formulate more precisely what the problem is?

 

Perhaps you are trying to specify a non-existent directory in the path? In this case, you must first check its presence and, if necessary, create -

 

Folder('z:/test/some/folder/lala/').create()

 

Problems with access are also possible if Photoshop is run with a user who does not have access rights to a network drive.

Votes

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
Engaged ,
Oct 20, 2021 Oct 20, 2021

Copy link to clipboard

Copied

@jazz-y Nope I it does not work for me, here is my debug >

 

Dariusz1989_1-1634755098300.png

 

 

Updated function

 

function getUniqueSavePath(path, name) {
    var filePath = decodeURI(path) + name + ".psb"
    //filePath = filePath.replace("//", "/")
    //filePath = filePath.replace(/^.*[\\\/]/, '\\')
    var docFile = Folder(filePath)
    var inx = 0
    while (docFile.exists) { // docFile.exists// docFile.constructor == File
        inx = inx + 1
        filePath = decodeURI(path) + name + "_" + String(inx) + ".psb"
        //filePath = filePath.replace("//", "/")
        //filePath = filePath.replace(/^.*[\\\/]/, '\\')
        docFile = Folder(filePath)
    }
    if (inx > 0) {
        return name + "_" + String(inx)
    }
    return name
}

 

On Windows hes simply not doing the check properly :- ( 

 

 

Ehhhhhhhhhhhhh I got it.. he never saved PSB! He always saved PSD! £%$^%£Q... yeah, why would he care about extension I give him >.>

 

Now I'm off to google how to save PSB... 

Thanks for help, I'm sorry for dramma! :C I cant read... I saw PS icon, I was happy. I didnt notice extension...

Votes

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
Guide ,
Oct 20, 2021 Oct 20, 2021

Copy link to clipboard

Copied

LATEST

Folder and File - are different objects. You check now folder name...

 

var docFile = Folder(filePath)

 

quote

Now I'm off to google how to save PSB... 


By @Dariusz1989

you can simply rename file after saving

Votes

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