Solved: Use variable from CSV as canvas size

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

floriske.nl

Solved: Use variable from CSV as canvas size

Post by floriske.nl »

I'm trying to use a value from a CSV file to set a file's canvas size, but it keeps resizing the canvas to 1x1px.

Here's the base script:
Code: Select all// Set counter
var counter = 0

// Open the CSV file
var txtFileRef = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv")
if (txtFileRef.exists)
{
    txtFileRef.open('r')
    while (!txtFileRef.eof) {
   
        // Skip first line for header info
        if (counter==0) {
            var line = txtFileRef.readln()
            counter++
        }
   
        // Read line
        var line = txtFileRef.readln()
        var arrLine = new Array()

        // Split line, use semicolon as delimiter
        arrLine = line.split(";")

        // Assign variables to field values
        var buttonName = arrLine[0]
        var buttonColor = arrLine[1]
        var buttonText = arrLine[2]
        var buttonTextSize = arrLine[3]
        var buttonWidth = arrLine[4]
        var buttonHeight = arrLine[5]       
       
        // Get the path of the script and open the base file from the same folder
        var scriptPath = File($.fileName).path
        var fileRef = File(scriptPath + "/testfile.psd")
        var docRef = app.open(fileRef)
       
        // Remember current unit settings and then set units to
        // the value expected by this script
        var originalUnit = preferences.rulerUnits
        preferences.rulerUnits = Units.PIXELS

        // Resize the canvas
        docRef.resizeCanvas(buttonWidth,buttonHeight)
       
    } // end while !txtFileRef.eof
   
} // end if file exists
 
// Restore original ruler unit setting
app.preferences.rulerUnits = originalUnit


As mentioned the canvas gets resized to 1x1px instead of the buttonWidth and buttonHeight from the array.

If I have the script write buttonWidth and buttonHeight to a textlayer it does show the correct values (136 and 82) in case of the first line of the csv.

What am I doing wrong here?

Edit solved: had to use parseFloat to convert the variable to a usable number:

Code: Select all// Set counter
var counter = 0

// Open the CSV file
var txtFileRef = File.openDialog("Open Comma-delimited File","comma-delimited(*.csv):*.csv")
if (txtFileRef.exists)
{
    txtFileRef.open('r')
    while (!txtFileRef.eof) {
   
        // Skip first line for header info
        if (counter==0) {
            var line = txtFileRef.readln()
            counter++
        }
   
        // Read line
        var line = txtFileRef.readln()
        var arrLine = new Array()

        // Split line, use semicolon as delimiter
        arrLine = line.split(";")

        // Assign variables to field values
        var buttonName = arrLine[0]
        var buttonColor = arrLine[1]
        var buttonText = arrLine[2]
        var buttonTextSize = arrLine[3] 
        var buttonWidth = parseFloat(arrLine[4])
        var buttonHeight = parseFloat(arrLine[5])     
       
        // Get the path of the script and open the base file from the same folder
        var scriptPath = File($.fileName).path
        var fileRef = File(scriptPath + "/testfile.psd")
        var docRef = app.open(fileRef)
       
        // Remember current unit settings and then set units to
        // the value expected by this script
        var originalUnit = preferences.rulerUnits
        preferences.rulerUnits = Units.PIXELS

        // Resize the canvas
        docRef.resizeCanvas(buttonWidth,buttonHeight)
       
    } // end while !txtFileRef.eof
   
} // end if file exists
 
// Restore original ruler unit setting
app.preferences.rulerUnits = originalUnit