Skip to main content
Inspiring
June 24, 2024
Question

Return results of AppleScript (or Shell command) called from Illustrator ExtendScript

  • June 24, 2024
  • 1 reply
  • 465 views

I'm running an Illustrator ExtendScript on MacOS and am trying to get the user initials without having to ask for them from the user.  I thought I could try using the long user name from the system as one option...

 

I can use this Shell command to return the initials:

 

dscl . -read /Users/$(whoami) RealName | tail -n 1 | awk '{ for(i=1;i<=NF;i++) printf "%s", tolower(substr($i,1,1)) }'

 

Or this AppleScript:

 

tell application "System Events"
	set fullName to words of (long user name of (system info))
	set initials to ""
	repeat with namePart in fullName
		set initials to initials & character 1 of namePart
	end repeat
	return initials
end tell

 

How do I run either of these from the Illustrator .jsx and get the results returned to it?

 

I tried saving the Applescript code above in a .app on the Desktop and called it with the following code, but can't figure out how to get the results returned back into the original .jsx:

 

var applescript = new File("~/Desktop/Test.app");
var results = applescript.execute();
alert(results);

 

This topic has been closed for replies.

1 reply

Inspiring
June 24, 2024

From the ExtendScript documentation:

 

18.5.5 execute()

fileObj.execute()

Opens this file using the appropriate application, as if it had been double-clicked in a file browser. You can use this method to run scripts, launch applications, and so on.

Returns true immediately if the application launch was successful.

 

Using JSX’s `execute` to run external scripts is limited and fragile. What you really want is an `app.doScript()` method as in InDesign, but AI doesn’t provide that. You could use `execute` to run an AppleScript app that writes the information to a temporary file at a known location; the JSX script can then wait until the file exists to read the information from it, then delete it.

 

Alternatively, if you can settle for using the current user’s login name, you can read the OS’s environment variables via JSX’s global “dollar” object:

 

 

$.getenv('USER')

 

 

Chris.SAuthor
Inspiring
July 1, 2024

Thank you. I'll try the temporary file method. The method suggested below only gives the short name unfortunately.

$.getenv('USER')