Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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')
Copy link to clipboard
Copied
Thank you. I'll try the temporary file method. The method suggested below only gives the short name unfortunately.
$.getenv('USER')