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

Why does this code not work with a version of InDesign?

Community Beginner ,
Apr 13, 2025 Apr 13, 2025

Hello everyone, I'm trying to understand why the code I attach works perfectly with InDesign 19.x and does not work with Indesign 20.x (see image)

Screenshot 2025-04-14 alle 07.41.49.png

Both versions of InDesign are on MacOS Ventura 13.7.5. Is it perhaps a bug in the latest version of InDesign?

Is it a bug in the latest version of InDesign or a stupid mistake of mine? Thanks to all.

Here is the code:

 

function createFolder(folder) {
    var script = "";
    script += "try\r";
    script += "do shell script \"mkdir \" & quote & item 1 of arguments & quote\r";
    script += "tell application id \"com.adobe.InDesign\"\r";
    script += "tell script args\r";
    script += "set value name \"success\" value \"true\"\r";
    script += "end tell\r";
    script += "end tell\r";
    script += "on error\r";
    script += "tell application id \"com.adobe.InDesign\"\r";
    script += "tell script args\r";
    script += "set value name \"success\" value \"false\"\r";
    script += "end tell\r";
    script += "end tell\r";
    script += "end try\r";
    app.doScript(script, ScriptLanguage.applescriptLanguage, Array(folder.fsName));
    if(app.scriptArgs.getValue('success') == "false") {
        return false;
    } else {
        return true;
    }
}

if(document.saved) {
     var file = new File(document.filePath + "/New folder");
} else {
     var file = new File("~/Documents/New folder");
}
var saveFile = file.saveDlg(
    "Create folder",
    "All files:*"
);
if(saveFile) {
    var folder = new Folder(saveFile.fullName);
    if(folder.exists) throw {
        message: "The folder '" + folder.name + "' already exists.",
        level: "Warning"
    };
    if(!createFolder(folder)) throw {
        message: "Error creating folder.",
        level: "Error"
     };
};

 

 

TOPICS
Scripting
349
Translate
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 ,
Apr 13, 2025 Apr 13, 2025

I am not sure why the code does not work in one version and works in other. However, you don't need a shell script to create a new folder, you can use the folder object. Try the following

if(document.saved) {
     var file = new File(document.filePath + "/New folder");
} else {
     var file = new File("~/Documents/New folder");
}
var saveFile = file.saveDlg(
    "Create folder",
    "All files:*"
);
if(saveFile) {
    var folder = new Folder(saveFile.fullName);
    if(folder.exists) throw {
        message: "The folder '" + folder.name + "' already exists.",
        level: "Warning"
    };
    if(!folder.create()) throw {
        message: "Error creating folder.",
        level: "Error"
     };
};

-Manan

Translate
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 Beginner ,
Apr 13, 2025 Apr 13, 2025

Hi Manan, this code is taken from an application that performs other operations on folders, for example: move, copy, and delete (with all its content). For that I chose to use "do shell script" inside an Applescript script. It is complex (in my opinion) to perform operations on the file system using ExtendScript...

Translate
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 ,
Apr 14, 2025 Apr 14, 2025

Ok, I tried this on my MAC and it did not execute for me on 2025 or even 2022 version of InDesign. So I am guessing this is some MAC permission issue. Not able to figure out yet as to what that is

-Manan

Translate
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 ,
Apr 14, 2025 Apr 14, 2025

@rob day can ypu please help us with your expertise on Applescript.

-Manan

Translate
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 Beginner ,
Apr 14, 2025 Apr 14, 2025

Clarification: The code works fine up to InDesign version 20.1. It stops working with version 20.2.

Translate
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 ,
Apr 14, 2025 Apr 14, 2025

Just guessing - HFS vs POSIX path problem? 

 

Translate
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
New Here ,
Apr 14, 2025 Apr 14, 2025

It looks to me like Folder.fsName returns a POSIX path and that  do shell script uses a POSIX path...

Translate
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
Guide ,
Apr 15, 2025 Apr 15, 2025
quote

It looks to me like Folder.fsName returns a POSIX path and that  do shell script uses a POSIX path...


By @AndreaParaboni


Better let us see some output.

I hope the following AppleScript survives the forum software.

tell application id "com.adobe.indesign"
	set f1 to full name of active document
	log class of f1
	-- class furl
	set p1 to POSIX path of f1
	set h1 to do shell script "/bin/echo -n " & quoted form of p1 & " | hexdump -C"
	
	tell application "Finder" to set f2 to container of (f1 as alias)
	log class of f2
	-- folder
	set p2 to POSIX path of (f2 as alias)
	set h2 to do shell script "/bin/echo -n " & quoted form of p2 & " | hexdump -C"
	
	set f3 to do script "app.activeDocument.fullName" language javascript
	log class of f3
	set p3 to POSIX path of f3
	set h3 to do shell script "/bin/echo -n '" & p3 & "' | hexdump -C"
	
	set p4 to do script "app.activeDocument.fullName.fsName" language javascript
	set h4 to do shell script "/bin/echo -n '" & p4 & "' | hexdump -C"
	
	set p5 to do script "app.activeDocument.fullName.parent.fsName" language javascript
	set h5 to do shell script "/bin/echo -n '" & p5 & "' | hexdump -C"
	
	log return & h1 & return & h2 & return & h3 & return & h4 & return & h5 & return
	display dialog h5
end tell

 

Launch AppleScript Editor

Create a new script, paste the code

At the bottom, switch to last choice "Responses" or whatever localization made from it to see intermediate values and log output.

Run the script with InDesign having an active document.

The most interesting part is the final output of the hexdumps.

 

If your original script fails only with specific folders, adjust the javascript for p5 accordingly to look at them, or add the h5 related lines including the display dialog line to the applescript part of your own script.

 

If you are thrill seeker, re-run the script with a document  or parent folder using some perfectly allowed characters

DirkBecker_0-1744704140622.png

(Screenshot from Finder)

and watch your approach of passing around fsName as posix path fail.

 

Translate
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
Guide ,
Apr 15, 2025 Apr 15, 2025

Here some ideas for error handling:

// jsx multi-line string
script = """
set errStr to ""
set errNum to 0
try
	tell application "Finder" to get path to desktop
	set d to the result
	fail
on error errorStr number errorNum
	set errStr to errorStr
	set errNum to errorNum
end try
return {d, POSIX path of d, 1, "Hello, world", errStr, errNum}
""";
Translate
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
New Here ,
Apr 14, 2025 Apr 14, 2025

Same result for Folder.fullName...

Translate
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
Guide ,
Apr 14, 2025 Apr 14, 2025

Yep, a POSIX issue, we had that recently. I could not find where it was discussed, though.

 

In theory, for the AppleScript side, you should be able to pass a file rather than the .fsName string.

AppleScript itself then has some convoluted conversion "quoted form of POSIX path of myFile" for passing to the shell script, rather than manually adding quotes. Mac folder names may contain quotes …

In practice you better test what comes along in your particular InDesign version, and I won't install 20.2 for that.

 

@Manan Joshi The AppleScript part could be a workaround attempt for a crash.

Better rewrite that to a simple function rather than change the prototype function as Marc did.

 

Translate
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 ,
Apr 14, 2025 Apr 14, 2025

Do I deserve a correct answer? 😄 

 

Translate
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 Beginner ,
Apr 14, 2025 Apr 14, 2025

Thanks everyone for the suggestions. What concerns me is that the code has been working fine in InDesign for many years... until version 20.1. From the next version (20.2) it stops working. If there were any errors in the code they would have emerged. Or not?

Translate
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 ,
Apr 14, 2025 Apr 14, 2025
quote

Thanks everyone for the suggestions. What concerns me is that the code has been working fine in InDesign for many years... until version 20.1. From the next version (20.2) it stops working. If there were any errors in the code they would have emerged. Or not?


By @andrea.paraboni

 

https://helpx.adobe.com/uk/indesign/kb/user-dictionaries-missing-hfs-path-dropped.html

 

HFS to POSIX Migration History

  • By default, InDesign 19.3 supported HFS Path Styles but developers could enable POSIX Support through Cookies.
  • Since InDesign 19.4, POSIX Path Styles was enabled by default. Developers/partners can still choose to work with HFS Path through Cookies.
  • From InDesign 20.0 onwards, POSIX Path Styles will be enabled by default, and HFS Path Support will be stopped.

 

Translate
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 ,
Apr 14, 2025 Apr 14, 2025

As far as I can see, there are only POSIX paths in the original script.

Translate
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 ,
Apr 14, 2025 Apr 14, 2025

Thanks for the article @Dirk Becker the interesting thing is I tried this code on InDesign version 2022 and it also gave the same error. So I am not sure that this is limited to 20.2 and beyond.

-Manan

Translate
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
Guide ,
Apr 14, 2025 Apr 14, 2025
quote

@Manan Joshi The AppleScript part could be a workaround attempt for a crash.

Better rewrite that to a simple function rather than change the prototype function as Marc did.

 

By @Dirk Becker

 

I do agree with Dirk. The 'prototype' approach only makes sense in a framework (like IdExtenso) or if you're troubleshooting a client script (or jsxbin!) with a hundred thousand lines of code that may call folder.create() anywhere 😉

Translate
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 ,
Apr 14, 2025 Apr 14, 2025

leor_0-1744663572263.png

 

Log the folder value, then try to run the same function with this value in a separate script and see if it works.

Translate
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 Beginner ,
Apr 22, 2025 Apr 22, 2025
LATEST

Upgrading to 20.3 fixes everything! This was a bug in InDesign 20.2. Thanks everyone.

Translate
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