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

javascript for automation vs applescript - dimensions

Explorer ,
Mar 21, 2024 Mar 21, 2024

Copy link to clipboard

Copied

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!

TOPICS
How-to , Scripting

Views

475

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
Community Expert ,
Mar 21, 2024 Mar 21, 2024

Copy link to clipboard

Copied

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

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
Explorer ,
Mar 21, 2024 Mar 21, 2024

Copy link to clipboard

Copied

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

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
Community Expert ,
Mar 21, 2024 Mar 21, 2024

Copy link to clipboard

Copied

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

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 ,
Mar 21, 2024 Mar 21, 2024

Copy link to clipboard

Copied

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.)

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
Explorer ,
Mar 28, 2024 Mar 28, 2024

Copy link to clipboard

Copied

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

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
Explorer ,
Mar 28, 2024 Mar 28, 2024

Copy link to clipboard

Copied

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

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 ,
Mar 28, 2024 Mar 28, 2024

Copy link to clipboard

Copied

 

man sips

 

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
Community Expert ,
Mar 28, 2024 Mar 28, 2024

Copy link to clipboard

Copied

LATEST

Hi @severus83, maybe best thing to do now is to set up a small demo with one or two files or folders or Illustrator documents—whatever you need—to explain exactly what you want to do. Don't tell us you just want the pixel dimensions because that isn't true—eg. you could use `file` on the command line to get them—you need them somewhere that you can do the next part, but you haven't explained that yet.

 

For example, if you post a demo document or a folder and tell us "I want to <insert your complete expectations from a script here>." Otherwise, we can't recommend anything useful.

 

The choice will probably be between Applescript and ExtendScript (edit: or command line, of course, but you may prefer to call that from Applescript anyway). (CEP uses ExtendScript anyway and is harder to implement, and ZXP is nothing to do with this topic, and Illustrator doesn't have UXP yet).

- Mark

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
Community Expert ,
Mar 21, 2024 Mar 21, 2024

Copy link to clipboard

Copied

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

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
Community Expert ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

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

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
Community Expert ,
Mar 22, 2024 Mar 22, 2024

Copy link to clipboard

Copied

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.

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