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

VBA to JavaScript

New Here ,
Mar 05, 2020 Mar 05, 2020

Copy link to clipboard

Copied

I have a VB script I did for CorelDraw. It utilizes a .txt file. On that file is a number (a Doc Number). When I run my VB script, it would open a New Document, Name it with the "Doc Number" on the .txt File + 1 and store the new document file name (not including the extension) back to the text file. 

 

So, lets say the txt file had 102 in it. When I ran my script a new document would open and be named 103. 103 (which is the active doc name) will then be stored back to the txt file (with no extension). The next person who runs the script gets a new document of 104. You get the picture. It is done this way for a purpose. We utilize one txt file and name our document this way to avoid duplicate documents or overwriting.

 

It's basically creating new document ID Numbers in a numeric fashion. The number will count up by 1 every time the script is run by 1.

 

I'm transitioning to JavaScript / Photoshop. How would this VB Script look converted over?

 

I think it would work the same.

1. Create a New Photoshop Document

2. Name it with the number on the txt file and add 1 to it. 

3. Store Newly Created Document Name back to txt file (minus the extension). 

 

CODE BELOW:

 

Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String

'----------------------------------------------------------------- Folder Path

On Error Resume Next
FilePath = "C:\Users\KoolKATZDesigns\Desktop\Script Tests\" & "Names" & ".txt"
'----------------------------------------------------------------- Get String (Proof Number) From Text File.

TextFile = FreeFile

'On Error GoTo ErrHandler

Open FilePath For Input As TextFile
Line Input #TextFile, FileContent
Close #TextFile

'------------------------------------------------- Creates New Document 

Dim doc1 As Document
Set doc1 = CreateDocument()
doc1.Name = FileContent + 1

'------------------------------------------------- Stores New Proof Number

Open FilePath For Output As TextFile
On Error Resume Next
Print #TextFile, ActiveDocument.Name
Close TextFile

 

ExitSub:
Exit Sub

ErrHandler:
MsgBox "I'm Misbehaving. Tell Chieri." & vbCr & "Error occured: " & Err.Description
Resume ExitSub

End Sub

Views

1.3K

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

correct answers 1 Correct answer

People's Champ , Mar 06, 2020 Mar 06, 2020
You can use such a script as a basis. Make changes to the name and path of the file and the options for creating a new document.
try {
    while (1)
        {
        var _file = new File("C:\\Users\\KoolKATZDesigns\\Desktop\\Script Tests\\Names.txt");

        var number = 0;

        if (_file.exists) 
            {
            _file.open("r");
            number = Number(_file.read());
            
            if (_file.error) { alert("Params read error!", "Error", true); _file.close(); br
...

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 06, 2020 Mar 06, 2020

Copy link to clipboard

Copied

The incremental numbering can be done like this:

 

 

var startNumber = 999;
startNumber++;
alert(startNumber);

 

 

After another quick search, this works too and is probably better:

 

 

var count = 999;
function newCount() {
    return ++count;
}
alert(newCount());

 

 

While this may be easier to read:

 

var countStart = 999;

incrementCounter();

function incrementCounter(){
  countStart += 1;
}

alert(countStart);

 

Writing to a text file has been recently covered in the post where you commented:

https://community.adobe.com/t5/photoshop/script-action-to-append-filename-to-text-document/td-p/9334...

 

However, you would probably wish to replace (edit) rather than append:

r for read mode - w for write mode - a for append - e for edit

 

Of course, it is not that easy, hopefully, somebody can step in as this is beyond my current coding abilities.

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
People's Champ ,
Mar 06, 2020 Mar 06, 2020

Copy link to clipboard

Copied

You can use such a script as a basis. Make changes to the name and path of the file and the options for creating a new document.
try {
    while (1)
        {
        var _file = new File("C:\\Users\\KoolKATZDesigns\\Desktop\\Script Tests\\Names.txt");

        var number = 0;

        if (_file.exists) 
            {
            _file.open("r");
            number = Number(_file.read());
            
            if (_file.error) { alert("Params read error!", "Error", true); _file.close(); break; }

            _file.close();    
            }
                    
        ++number;

        _file.open("w");
        _file.write(number);

        if (_file.error) { alert("Params save error!", "Error", true); _file.close(); break; }
    
        _file.close();    

        var _name = "DOC_" + number;
            
        app.documents.add(UnitValue(1920, "px"), UnitValue(1080, "px"), 72, _name, NewDocumentMode.RGB, DocumentFill.WHITE, 1, BitsPerChannelType.EIGHT, "sRGB IEC61966-2.1");

        break;
        }
    }
catch (e) { alert(e); }

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
New Here ,
Mar 07, 2020 Mar 07, 2020

Copy link to clipboard

Copied

Thank you. 

 

This helped me created another version by adding var doc =  app.activedocument , doc.name as another way to go about this. 

 

I admit, jsx is a little more confusing. Took me years just to learn VB. Im grateful for the help. 

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
New Here ,
Mar 07, 2020 Mar 07, 2020

Copy link to clipboard

Copied

Just one question...

On this part:

   app.documents.add(UnitValue(1920, "px"), UnitValue(1080, "px"), 72, _name, NewDocumentMode.RGB, DocumentFill.WHITE, 1, BitsPerChannelType.EIGHT, "sRGB IEC61966-2.1");

How would it look to (instead) "duplicate" instead of creating a new document? 

Example: The customer likes Doc_5 but wants a color change. With Doc_5 open, we run the script, it will duplicate it to Doc_6 and keep all the contents and leaving Doc_5 as it was?

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
People's Champ ,
Mar 07, 2020 Mar 07, 2020

Copy link to clipboard

Copied

You can read the documentation for the script for Photoshop
 
For duplication use
app.activeDocument.duplicate( _name); 

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
New Here ,
Mar 07, 2020 Mar 07, 2020

Copy link to clipboard

Copied

LATEST

Perfect! Thank you.

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