MAC - Read Image Dimensions without opening for any extension
I was looking for a surefire method to read image dimensions and information without opening them in Photoshop on mac, and came across app.system. Using the sips command, and writing a temp file to get the variables over to Photoshop ended up working better than trying to read metadata from files, as it varies by extension. Here's the solution I came up with:
function dateInt() {
var theDate = new Date();
return theDate.getTime();
}
function readTextFile(file) {
file = File(file);
file.open('r');
var str = file.read();
file.close();
return str;
}
var tgtImg = 'IMAGE_PATH/IMAGE_NAME.ext'; //Edit this line
var dateFile = "sipsInfo_" + dateInt() + ".txt";
var dateFilePath = $.getenv("TMPDIR") + dateFile;
var osascriptCommand = 'sips -1 -g pixelWidth -g pixelHeight "' + tgtImg + '" > ' + dateFilePath;
app.system(osascriptCommand);
var textDims = readTextFile(dateFilePath);
var sipW = Number(textDims.match(/pixelWidth\: ([0-9]+)/g)[0].split(": ")[1]);
var sipH = Number(textDims.match(/pixelHeight\: ([0-9]+)/g)[0].split(": ")[1]);
File(dateFilePath).remove();
alert("Dimensions:\nW:" + sipW + "\nH:" + sipH)Tested with PSD, PSB, TIF,JPG, PNG, which are all read consistently by sips.
