Skip to main content
Participating Frequently
March 21, 2024
Question

javascript for automation vs applescript - dimensions

  • March 21, 2024
  • 3 replies
  • 1211 views

Hello,

I wrote two scripts. jxa script works very slowly compared to ap. the scripts are supposed to count the dimensions of selected files with a resolution of 2540. There may be about 30 selected files and then it feels that jxa works several times slower, but I need jxa for windows. How can I speed up this script?

jxa:

var suma = 0;

var app = Application.currentApplication();
	app.includeStandardAdditions = true;

var finder = Application("Finder");
var imageEvents = Application("Image Events");

for(var i=0; i<finder.selection().length; i++) {
	var path = finder.selection()[i].url();
	var newPath = path.substring(7);
	var myPath = decodeURI(newPath);
	var img = imageEvents.open(myPath);

	var w = img.dimensions()[0];
	var h = img.dimensions()[1];
	var wRes = img.resolution()[0];
	var hRes = img.resolution()[1];

	var m2 = (w/wRes*0.0254) * (h/hRes*0.0254);
	var suma = suma+m2; 
};

app.displayDialog(suma.toFixed(3));

ap:

tell application "Finder"
	set lista to selection
	repeat with i from 1 to (count lista)
		set {item i of lista} to {item i of lista as string}
	end repeat
end tell

set m2 to 0

tell application "Image Events"
	repeat with i from 1 to (count lista)
		launch
		set plik to open (item i of lista)
		copy the dimensions of plik to {x, y}
		close plik
		set suma to ((x * 0.01) / 1000) * ((y * 0.01) / 1000)
		set m2 to m2 + suma
	end repeat
end tell

display alert m2

 

thanks for the answers!

This topic has been closed for replies.

3 replies

Legend
March 22, 2024

For getting information about image files, ExternalObject('lib:AdobeXMPScript') may be used in ExtendScript. This can get/set XMP metadata for external files via the File object, not just documents opened in Illustrator.

 

If you are not familiar with it, using such a library is also a good idea.
https://github.com/ten-A/Extend_Script_experimentals/blob/master/XMPtool.jsx

m1b
Community Expert
Community Expert
March 22, 2024

Thanks @sttk3, this is excellent info! I had not considered XMP in relation to this question.

 

I got this to work on a .psd file, for example:

 

var f = File('/Users/mark/Desktop/test.psd');
var x = Number(XMPTool.read('exif:PixelXDimension', 'http://ns.adobe.com/exif/1.0/', XMPConst.NS_XMP_MM, f));

 

 

But it won't work on non-adobe saved files I guess—only files with XMP?

- Mark

Legend
March 22, 2024

XMP is a popular metadata standard for image formats. I personally consider it safe to rely on. We often see exif:PixelXDimension and tiff:ImageWidth and tiff:XResolution.

Inspiring
March 21, 2024

This isn’t an Illustrator question. For bitmap image processing, use Photoshop. PS also has UXP (ES6), which is vastly more modern than JSX (ES3).

 

https://developer.adobe.com/photoshop/uxp

 

You might also look into NodeJS+NPM or PowerShell, where you can find libraries to extract the image data you want without the overheads of calling into PS. (Not great languages but highly capable and extremely well-supported, unlike Apple’s moribund tech.)

severus83Author
Participating Frequently
March 28, 2024

Muszę obliczyć metry kwadratowe dla plików z rozszerzeniem tif bez użycia Photoshopa, więc pierwsze co mi przyszło do głowy to applescript (ap) lub skrypt java do automatyzacji (jxa). Pracuję dla Macos. Tam, gdzie pracuję, wszystkie inne instalacje na tym komputerze są zablokowane, więc nic nie mogę z tym zrobić, więc nadal mogę spróbować bash. Mimo to UXP jest świetny, ale tylko na PS. Muszę sprawdzić możliwości AI (tj. CEP i ZXP)? - to chyba inny temat

severus83Author
Participating Frequently
March 28, 2024

I need to calculate square meters for files with the tif extension without using Photoshop, so the first thing that came to my mind was applescript (ap) or java script for automation (jxa). I work for macos. Where I work, all other installations on this computer are blocked, so there's nothing I can do about it, so I can still try bash. Still, UXP is great, but only for PS. I need to check the possibilities for AI (i.e. CEP and ZXP)? - that's probably another topic

m1b
Community Expert
Community Expert
March 21, 2024

Hi @severus83, you have a slight misconception. JXA is a MacOS only technology (a javascript flavour of appleScript?) that only works on MacOS

 

I suggest you look at learning ExtendScript (basically Javascript ES3). You can edit ExtendScript code with free IDE VSCode and use the ExtendScript Debugger extension to debug your code. So you can do some research if you like. Look up how to set up VSCode for ExtendScript, etc.

 

By the way, your JXA code isn't how it would be done in ExtendScript. When someone has time, maybe they can convert for you.

- Mark

severus83Author
Participating Frequently
March 21, 2024

Yes. I wrote it wrong, it's for windows. I know that jxa is an alternative language only for macos that can work on the file system and that's what I mean. I write scripts for AI in jsx, including scriptUI, but I need to have access to the file system (for example, creating or deleting directories or files). It's very possible that all this can be done in ExtendScript, but I don't have enough knowledge

I don't want someone to write the entire script for me, it's no fun. I just need direction

m1b
Community Expert
Community Expert
March 21, 2024

Ah! Okay, well you can do very basic file operations in ExtendScript and it works pretty well as far as it goes. Have a look at File and Folder. You don't get any access to the file metadata except for the bare bones (eg. creation/modification dates, size), so you wouldn't be able to achieve the equivalent of your Finder script. You could open the files in Photoshop and get their dimensions that way, but it obviously isn't a quick way to do it. Hopefully an expert will chime in with some more useful info for you.

 

If you're using applescript/JXA on MacOS, then I guess... Powershell on Windows? Sorry I'm out of my depth. Good luck!

- Mark